STK++ 1.0

The descriptive statistics sub-project.

In this sub-project, we compute the usual descriptive statistics of variables. More...

Collaboration diagram for The descriptive statistics sub-project.:

Classes

class  STK::Stat::Bivariate< XTYPE, YTYPE, TContainer1D >
 Computation of the bivariate Statistics of two Variables. More...
class  STK::Stat::Bivariate< Real, Real, TContainer1D >
 Computation of the bivariate Statistics of two Variables. More...
class  STK::Stat::Multivariate< TYPE, TContainerHo, TContainerVe, TContainer2D >
 Computation of the multivariate statistics of a Variable. More...
class  STK::Stat::Multivariate< Real, TContainerHo, TContainerVe, TContainer2D >
 Computation of the Multivariate Statistics of a 2D Container of Real. More...
class  STK::Stat::Univariate< TYPE, TContainer1D >
 Computation of the univariate Statistics of a Variable. More...
class  STK::Stat::Univariate< Real, TContainer1D >
 Computation of the univariate Statistics of a Real Variable. More...

Functions

template<class TContainer1D >
Real STK::Stat::covariance (ITContainer1D< Real, TContainer1D > const &X, ITContainer1D< Real, TContainer1D > const &Y, bool unbiased=false)
 Compute the covariance between the variable X and Y.
template<class TContainer1D >
Real STK::Stat::covariance (ITContainer1D< Real, TContainer1D > const &X, ITContainer1D< Real, TContainer1D > const &Y, ITContainer1D< Real, TContainer1D > const &W, bool unbiased=false)
 Compute the weighted variance of the variable V.
template<class TContainer1D >
Real STK::Stat::covarianceWithFixedMean (ITContainer1D< Real, TContainer1D > const &X, ITContainer1D< Real, TContainer1D > const &Y, Real const &xMean, Real const &yMean, bool unbiased=false)
 Compute the variance of the variable V with fixed mean.
template<class TContainer1D >
Real STK::Stat::covarianceWithFixedMean (ITContainer1D< Real, TContainer1D > const &X, ITContainer1D< Real, TContainer1D > const &Y, ITContainer1D< Real, TContainer1D > const &W, Real const &xMean, Real const &yMean, bool unbiased=false)
 Compute the weighted variance of the variable V with fixed mean.
template<class TContainerHo , class TContainerVe , class TContainer2D >
TContainerHo STK::Stat::mean (ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &V)
 Compute the mean of the container V.
template<class TContainerHo , class TContainerVe , class TContainer2D >
TContainerHo STK::Stat::mean (ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &V, TContainerVe const &W)
 Compute the (weighted) mean of the variable V

\[ \hat{\mu} = \frac{1}{\sum_{i=1}^n W(i)} \sum_{i=1}^n W(i) V(i) \]

if there is weights.

template<class TContainerHo , class TContainerVe , class TContainer2D >
TContainerHo STK::Stat::variance (ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &V, bool unbiased=false)
 Compute the variance of the variable V.
template<class TContainerHo , class TContainerVe , class TContainer2D >
TContainerHo STK::Stat::variance (ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &V, TContainerVe const &W, bool unbiased=false)
 Compute the weighted variance of the variable V.
template<class TContainerHo , class TContainerVe , class TContainer2D >
TContainerHo STK::Stat::varianceWithFixedMean (ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &V, TContainerHo const &mu, bool unbiased=false)
 Compute the variance of the variable V with fixed mean.
template<class TContainerHo , class TContainerVe , class TContainer2D >
TContainerHo STK::Stat::varianceWithFixedMean (ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &V, TContainerVe const &W, TContainerHo const &mu, bool unbiased=false)
 Compute the weighted variance of the variables V with fixed mean.
template<class TContainer1D >
Real STK::Stat::mean (ITContainer1D< Real, TContainer1D > const &V)
 Compute the mean of the variable V.
template<class TContainer1D >
Real STK::Stat::mean (ITContainer1D< Real, TContainer1D > const &V, ITContainer1D< Real, TContainer1D > const &W)
 Compute the (weighted) mean of the variable V

\[ \hat{\mu} = \frac{1}{\sum_{i=1}^n W(i)} \sum_{i=1}^n W(i) V(i). \]

If the range of the weights does not match the range of the variable the method return the usual mean.

template<class TContainer1D >
Real STK::Stat::variance (ITContainer1D< Real, TContainer1D > const &V, bool unbiased=false)
 Compute the variance of the variable V.
template<class TContainer1D >
Real STK::Stat::variance (ITContainer1D< Real, TContainer1D > const &V, ITContainer1D< Real, TContainer1D > const &W, bool unbiased=false)
 Compute the weighted variance of the variable V.
template<class TContainer1D >
Real STK::Stat::varianceWithFixedMean (ITContainer1D< Real, TContainer1D > const &V, Real const &mu, bool unbiased=false)
 Compute the variance of the variable V with fixed mean.

Detailed Description

In this sub-project, we compute the usual descriptive statistics of variables.


Function Documentation

template<class TContainer1D >
Real STK::Stat::covariance ( ITContainer1D< Real, TContainer1D > const &  X,
ITContainer1D< Real, TContainer1D > const &  Y,
bool  unbiased = false 
)

Compute the covariance between the variable X and Y.

\[ \hat{\sigma}^2 = \frac{1}{n} \sum_{i=1}^n (V(i)-\hat{\mu})^2. \]

Parameters:
Xfirst variable
Ysecond variable
unbiasedtrue if we want an unbiased estimator of the variance, false otherwise (default is false)

Definition at line 343 of file STK_Stat_BivariateRealReal.h.

References STK::Range::empty(), STK::Range::first(), STK::Range::inf(), STK::Range::last(), STK::ITContainer1D< TYPE, TContainer1D >::range(), and STK::Range::size().

{
  // get dimensions
  Range com = STK::Range::inf(X.range(),Y.range());
  // no samples
  if (com.empty()) { return Arithmetic<Real>::NA();}

  // Compute the mean
  Real xMean = mean<TContainer1D>(X), yMean = mean<TContainer1D>(Y);

  // get dimensions
  const Integer first = com.first(), last = com.last();
  Integer nobs = com.size();
  // compute covariance
  Real xsum  = 0.0, ysum = 0.0, cov  = 0.0, xdev, ydev;
  for (Integer i=first; i<=last; i++)
  {
    if (Arithmetic<Real>::isFinite(X[i]) && Arithmetic<Real>::isFinite(Y[i]))
    {
      xsum += (xdev = X[i] - xMean); // deviation from the mean
      ysum += (ydev = Y[i] - yMean); // deviation from the mean
      cov += (xdev*ydev);         // squared value
    }
    else nobs--;
  }
  // compute the variance
  if (unbiased)
  {
    return (nobs > 1) ? ((cov - (xsum*ysum)/(Real)nobs)/(Real)(nobs -1))
                      : Arithmetic<Real>::NA();

  }
  return (nobs > 0) ? ((cov - (xsum*ysum)/(Real)nobs)/(Real)(nobs))
                    : Arithmetic<Real>::NA();
}
template<class TContainer1D >
Real STK::Stat::covariance ( ITContainer1D< Real, TContainer1D > const &  X,
ITContainer1D< Real, TContainer1D > const &  Y,
ITContainer1D< Real, TContainer1D > const &  W,
bool  unbiased = false 
)

Compute the weighted variance of the variable V.

\[ \hat{\sigma}^2 = \frac{\sum_{i=1}^n W(i)}{\left( \sum_{i=1}^n W(i))\right)^2-\sum_{i=1}^n W(i)^2} \sum_{i=1}^n W(i) (V(i)-\hat{\mu})^2. \]

If there is no weights, this definition reduces to the usual definition of the variance with factor 1/(n-1). If the range of the weights is not equal to the range of the varaible, the usual varaince is computed.

Parameters:
Xfirst variable
Ysecond variable
Wweights
unbiasedtrue if we want an unbiased estimator of the variance, false otherwise (default is false)

Definition at line 398 of file STK_Stat_BivariateRealReal.h.

References STK::abs(), STK::Stat::covarianceWithFixedMean(), STK::Range::empty(), STK::Range::first(), STK::Range::inf(), STK::Range::isIn(), STK::Range::last(), STK::Stat::mean(), and STK::ITContainer1D< TYPE, TContainer1D >::range().

{
  // get dimensions
  Range com = STK::Range::inf(X.range(),Y.range());
  // no samples
  if (com.empty()) { return Arithmetic<Real>::NA();}

  // Compute the mean
  Real xMean = mean(X, W), yMean= mean(Y, W);

  // if the weight are not of the same size, ignore them
  if (!com.isIn(W.range()))
    return covarianceWithFixedMean(X, Y, xMean, yMean, unbiased);

  // get dimensions
  Integer first = com.first(), last = com.last();
  // compute covariance
  Real xsum  = 0.0, ysum = 0.0, xdev, ydev, sumWeights = 0.0, sum2Weights = 0.0, cov = 0.0;
  for (Integer i=first; i<=last; i++)
  {
    if (Arithmetic<Real>::isFinite(X[i]) && Arithmetic<Real>::isFinite(Y[i]))
    {
      xsum += (xdev = X[i] - xMean); // deviation from the mean
      ysum += (ydev = Y[i] - yMean); // deviation from the mean
      Real Wi = abs((Real)W[i]);
      cov += Wi * (xdev*ydev);         // cross product
      sumWeights  += Wi;           // sum absolute weights
      sum2Weights += Wi * Wi;     // sum squared weights
    }
  }
  // compute the variance
  if (unbiased)
  {
    if (sumWeights*sumWeights > sum2Weights)
    {
      return (cov - xsum*ysum/sumWeights)/(sumWeights - sum2Weights/sumWeights);
    }
    else
    {
      if (sumWeights) return 0.;
    }
    return Arithmetic<Real>::NA();
  }
  return (sumWeights) ? (cov - xsum*ysum)/(sumWeights) : Arithmetic<Real>::NA();
}
template<class TContainer1D >
Real STK::Stat::covarianceWithFixedMean ( ITContainer1D< Real, TContainer1D > const &  X,
ITContainer1D< Real, TContainer1D > const &  Y,
Real const &  xMean,
Real const &  yMean,
bool  unbiased = false 
)

Compute the variance of the variable V with fixed mean.

\[ \hat{\mu} = \frac{1}{n} \sum_{i=1}^n (V(i) - \mu)^2. \]

Parameters:
Xfirst variable
Ysecond variable
xMeanthe mean of X
yMeanthe mean of Y
unbiasedtrue if we want an unbiased estimator of the variance, false otherwise (default is false)

Definition at line 461 of file STK_Stat_BivariateRealReal.h.

References STK::Range::empty(), STK::Range::first(), STK::Range::inf(), STK::Range::last(), STK::ITContainer1D< TYPE, TContainer1D >::range(), and STK::Range::size().

Referenced by STK::Stat::covariance(), STK::Stat::covarianceWithFixedMean(), and STK::Stat::Multivariate< Real, TContainerHo, TContainerVe, TContainer2D >::run().

{
    // get dimensions
    Range com = STK::Range::inf(X.range(),Y.range());
    // no samples
    if (com.empty()) { return Arithmetic<Real>::NA();}

    // get dimensions
    const Integer first = com.first(), last = com.last();
    Integer nobs = com.size();
    // compute covariance
    Real xsum  = 0.0, ysum = 0.0, cov  = 0.0, xdev, ydev;
    for (Integer i=first; i<=last; i++)
    {
      if (Arithmetic<Real>::isFinite(X[i]) && Arithmetic<Real>::isFinite(Y[i]))
      {
        xsum += (xdev = X[i] - xMean); // deviation from the mean
        ysum += (ydev = Y[i] - yMean); // deviation from the mean
        cov += (xdev*ydev);         // squared value
      }
      else nobs--;
    }
    // compute the variance
    if (unbiased)
    {
      return (nobs > 1) ? ((cov - (xsum*ysum)/(Real)nobs)/(Real)(nobs -1))
                        : Arithmetic<Real>::NA();

    }
    return (nobs > 0) ? (cov - (xsum*ysum)/(Real)nobs)/(Real)(nobs)
                      : Arithmetic<Real>::NA();
}
template<class TContainer1D >
Real STK::Stat::covarianceWithFixedMean ( ITContainer1D< Real, TContainer1D > const &  X,
ITContainer1D< Real, TContainer1D > const &  Y,
ITContainer1D< Real, TContainer1D > const &  W,
Real const &  xMean,
Real const &  yMean,
bool  unbiased = false 
)

Compute the weighted variance of the variable V with fixed mean.

\[ \hat{\mu} = \frac{1}{\sum_{i=1}^n W(i)} \sum_{i=1}^n W(i) (V(i) - \mu)^2 \]

Parameters:
Xfirst variable
Ysecond variable
Wweights
xMeanthe mean of X
yMeanthe mean of Y
unbiasedtrue if we want an unbiased estimator of the variance, false otherwise (default is false)

Definition at line 513 of file STK_Stat_BivariateRealReal.h.

References STK::abs(), STK::Stat::covarianceWithFixedMean(), STK::Range::empty(), STK::Range::first(), STK::Range::inf(), STK::Range::isIn(), STK::Range::last(), and STK::ITContainer1D< TYPE, TContainer1D >::range().

{
    // get dimensions
    Range com = STK::Range::inf(X.range(),Y.range());
    // no samples
    if (com.empty()) { return Arithmetic<Real>::NA();}

    // if the weight are not of the same size, ignore them
    if (!com.isIn(W.range()))
      return covarianceWithFixedMean(X, Y, xMean, yMean, unbiased);

    // get dimensions
    Integer first = com.first(), last = com.last();
    // compute covariance
    Real xsum  = 0.0, ysum = 0.0, xdev, ydev, sumWeights = 0.0, sum2Weights = 0.0, cov = 0.0;
    for (Integer i=first; i<=last; i++)
    {
      if (Arithmetic<Real>::isFinite(X[i]) && Arithmetic<Real>::isFinite(Y[i]))
      {
        xsum += (xdev = X[i] - xMean); // deviation from the mean
        ysum += (ydev = Y[i] - yMean); // deviation from the mean
        Real Wi = abs((Real)W[i]);
        cov += Wi * (xdev*ydev);         // cross product
        sumWeights  += Wi;           // sum absolute weights
        sum2Weights += Wi * Wi;     // sum squared weights
      }
    }
    // compute the variance
    if (unbiased)
    {
      if (sumWeights*sumWeights > sum2Weights)
      {
        return (cov - xsum*ysum/sumWeights)/(sumWeights - sum2Weights/sumWeights);
      }
      else
      {
        if (sumWeights) return 0.;
      }
      return Arithmetic<Real>::NA();
    }
    return (sumWeights) ? (cov - xsum*ysum)/(sumWeights) : Arithmetic<Real>::NA();
}
template<class TContainerHo , class TContainerVe , class TContainer2D >
TContainerHo STK::Stat::mean ( ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &  V)

Compute the mean of the container V.

Parameters:
Vthe container with the Data

Definition at line 177 of file STK_Stat_MultivariateReal.h.

References STK::IContainer2D::firstCol(), STK::IContainer2D::lastCol(), and STK::IContainer2D::rangeHo().

Referenced by STK::Stat::covariance(), STK::Stat::mean(), STK::Stat::Multivariate< Real, TContainerHo, TContainerVe, TContainer2D >::run(), and STK::Stat::variance().

{
  TContainerHo mean(V.rangeHo());
  // get dimensions
  const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
  for (Integer j= firstVar; j<= lastVar; j++)
    mean[j] = Stat::mean<TContainerVe>(V[j]);
  return mean;
}
template<class TContainerHo , class TContainerVe , class TContainer2D >
TContainerHo STK::Stat::mean ( ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &  V,
TContainerVe const &  W 
)

Compute the (weighted) mean of the variable V

\[ \hat{\mu} = \frac{1}{\sum_{i=1}^n W(i)} \sum_{i=1}^n W(i) V(i) \]

if there is weights.

Parameters:
Vthe variable
Wthe weights

Definition at line 197 of file STK_Stat_MultivariateReal.h.

References STK::IContainer2D::firstCol(), STK::IContainer2D::lastCol(), STK::Stat::mean(), and STK::IContainer2D::rangeHo().

{
  // create result
  TContainerHo mean(V.rangeHo());
  // get dimensions
  const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
  for (Integer j= firstVar; j<= lastVar; j++)
    mean[j] = Stat::mean<TContainerVe>(V[j], W);
  return mean;
}
template<class TContainerHo , class TContainerVe , class TContainer2D >
TContainerHo STK::Stat::variance ( ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &  V,
bool  unbiased = false 
)

Compute the variance of the variable V.

\[ \hat{\sigma}^2 = \frac{1}{n-1} \sum_{i=1}^n (V(i)-\hat{\mu})^2. \]

Parameters:
Vvariable
unbiasedtrue if we want an unbiased estimator of the variance, false otherwise (default is false)

Definition at line 220 of file STK_Stat_MultivariateReal.h.

References STK::IContainer2D::firstCol(), STK::IContainer2D::lastCol(), and STK::IContainer2D::rangeHo().

Referenced by MTRand::randNorm(), and STK::Stat::standardize().

{
  // create result
  TContainerHo var(V.rangeHo());
  // get dimensions
  const Integer firstVar = V.firstCol(), lastVar = V.lastCol();
  for (Integer j= firstVar; j<= lastVar; j++)
  {
    var[j] = Stat::variance(V[j], unbiased);
  }
  // return variance
  return var;
}
template<class TContainerHo , class TContainerVe , class TContainer2D >
TContainerHo STK::Stat::variance ( ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &  V,
TContainerVe const &  W,
bool  unbiased = false 
)

Compute the weighted variance of the variable V.

\[ \hat{\sigma}^2 = \frac{\sum_{i=1}^n W(i)} {\left( \sum_{i=1}^n W(i))\right)^2 - \sum_{i=1}^n W(i)^2} \sum_{i=1}^n W(i) (V(i)-\hat{\mu})^2. \]

If there is no weights, this definition reduces to the usual definition of the variance with factor 1/(n-1).

Parameters:
Vvariable
Wweights
unbiasedtrue if we want an unbiased estimator of the variance, false otherwise (default is false)

Definition at line 251 of file STK_Stat_MultivariateReal.h.

References STK::IContainer2D::firstCol(), STK::IContainer2D::lastCol(), and STK::IContainer2D::rangeHo().

{
  // create result
  TContainerHo var(V.rangeHo());
  // get dimensions
  const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
  // check if there exist a mean vector
  for (Integer j= firstVar; j<= lastVar; j++)
    var[j] = Stat::variance<TContainerVe>(V[j], W, unbiased);
  // return variance
  return var;
}
template<class TContainerHo , class TContainerVe , class TContainer2D >
TContainerHo STK::Stat::varianceWithFixedMean ( ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &  V,
TContainerHo const &  mu,
bool  unbiased = false 
)

Compute the variance of the variable V with fixed mean.

\[ \hat{\mu} = \frac{1}{n} \sum_{i=1}^n (V(i) - \mu)^2. \]

Parameters:
Vvariable
mumean of the variable V
unbiasedtrue if we want an unbiased estimator of the variance, false otherwise (default is false)

Definition at line 278 of file STK_Stat_MultivariateReal.h.

References STK::IContainer2D::firstCol(), STK::IContainer2D::lastCol(), and STK::IContainer2D::rangeHo().

Referenced by STK::Stat::Multivariate< Real, TContainerHo, TContainerVe, TContainer2D >::run(), STK::Stat::variance(), and STK::Stat::varianceWithFixedMean().

{
  if (mu.range() != V.rangeHo())
    throw std::runtime_error("In varianceWithFixedMean(V, mu, unbiased) "
                             "mu.range() != V.rangeHo()");
  // create result
  TContainerHo var(V.rangeHo());
  // get dimensions
  const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
  for (Integer j= firstVar; j<= lastVar; j++)
    var[j] = Stat::varianceWithFixedMean<TContainerVe>(V[j], mu[j], unbiased);
  // return variance
  return var;
}
template<class TContainerHo , class TContainerVe , class TContainer2D >
TContainerHo STK::Stat::varianceWithFixedMean ( ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &  V,
TContainerVe const &  W,
TContainerHo const &  mu,
bool  unbiased = false 
)

Compute the weighted variance of the variables V with fixed mean.

\[ \hat{\mu} = \frac{1}{\sum_{i=1}^n W(i)} \sum_{i=1}^n W(i) (V(i) - \mu)^2 \]

Parameters:
Vvariable
Wweights
muweighted mean of V
unbiasedtrue if we want an unbiased estimator of the variance, false otherwise (default is false)

Definition at line 308 of file STK_Stat_MultivariateReal.h.

References STK::IContainer2D::firstCol(), STK::IContainer2D::lastCol(), and STK::IContainer2D::rangeHo().

{
#ifdef STK_DEBUG
  if (mu.range() != V.rangeHo())
    throw std::runtime_error("In varianceWithFixedMean(V, W, mu) "
                             "mu.range() != V.rangeHo()");
#endif
  // create result
  TContainerHo var(V.rangeHo());
  // get dimensions
  const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
  for (Integer j= firstVar; j<= lastVar; j++)
    var[j] = Stat::varianceWithFixedMean<TContainerVe>(V[j], W, mu[j], unbiased);
  // return variance
  return var;
}
template<class TContainer1D >
Real STK::Stat::mean ( ITContainer1D< Real, TContainer1D > const &  V)

Compute the mean of the variable V.

\[ \hat{\mu} = \frac{1}{n} \sum_{i=1}^n V(i) \]

Parameters:
Vthe variable

Definition at line 730 of file STK_Stat_UnivariateReal.h.

References STK::IContainer1D::empty(), STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), STK::ITContainer1D< TYPE, TContainer1D >::size(), and STK::sum().

{
  // no samples
  if (V.empty())
  { return Arithmetic<Real>::NA();}

  // get dimensions
  Integer nobs = V.size();
  // sum the samples
  Real sum  = 0.0;
  for (Integer i=V.first(); i<=V.last(); i++)
  { if (Arithmetic<Real>::isFinite(V[i]))
      sum += V[i];
    else
      nobs--;
  }
  // compute the mean
  if (nobs) return sum /= (Real)nobs;
  else      return Arithmetic<Real>::NA();
}
template<class TContainer1D >
Real STK::Stat::mean ( ITContainer1D< Real, TContainer1D > const &  V,
ITContainer1D< Real, TContainer1D > const &  W 
)

Compute the (weighted) mean of the variable V

\[ \hat{\mu} = \frac{1}{\sum_{i=1}^n W(i)} \sum_{i=1}^n W(i) V(i). \]

If the range of the weights does not match the range of the variable the method return the usual mean.

Parameters:
Vvariable
Wweights

Definition at line 762 of file STK_Stat_UnivariateReal.h.

References STK::abs(), STK::IContainer1D::empty(), STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), STK::Stat::mean(), STK::ITContainer1D< TYPE, TContainer1D >::range(), and STK::sum().

{
  // no samples
  if (V.empty()) { return Arithmetic<Real>::NA();}

  // get dimensions
  Real sum  = 0.0;
  // if the weight are not of the same size, ignore them
  if (V.range() != W.range()) return mean(V);

  // sum the weighted samples
  Real nweight = 0.0;
  for (Integer i=V.first(); i<=V.last(); i++)
  { if ( (Arithmetic<Real>::isFinite(V[i]))
       &&(Arithmetic<Real>::isFinite(W[i]))
       )
    {
      Real weight  = abs(W[i]);
      nweight  += weight;
      sum      += weight * V[i];
    }
  }
  // compute the mean
  return (nweight) ? sum /= nweight : 0.;
}
template<class TContainer1D >
Real STK::Stat::variance ( ITContainer1D< Real, TContainer1D > const &  V,
bool  unbiased = false 
)

Compute the variance of the variable V.

\[ \hat{\sigma}^2 = \frac{1}{n} \sum_{i=1}^n (V(i)-\hat{\mu})^2. \]

Parameters:
Vvariable
unbiasedtrue if we want an unbiased estimator of the variance, false otherwise (default is false)

Definition at line 800 of file STK_Stat_UnivariateReal.h.

References STK::IContainer1D::empty(), STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), STK::ITContainer1D< TYPE, TContainer1D >::size(), and STK::sum().

{
  // no samples
  if (V.empty()) { return Arithmetic<Real>::NA();}

  // Compute the mean
  Real mu = mean<TContainer1D>(V);

  // get dimensions
  const Integer  first = V.first(), last = V.last();
  Integer nobs = V.size();
  // sum
  Real sum  = 0.0, var  = 0.0, dev;
  for (Integer i=first; i<=last; i++)
  {
    if (Arithmetic<Real>::isFinite(V[i]))
    {
      sum += (dev = V[i] - mu); // deviation from the mean
      var += (dev*dev);         // squared value
    }
    else nobs--;
  }
  // compute the variance
  if (unbiased)
  {
    return (nobs > 1) ? (var - (sum*sum)/(Real)nobs)/(Real)(nobs -1)
                      : Arithmetic<Real>::NA();

  }
  return (nobs > 0) ? (var - (sum*sum)/(Real)nobs)/(Real)(nobs)
                    : Arithmetic<Real>::NA();
}
template<class TContainer1D >
Real STK::Stat::variance ( ITContainer1D< Real, TContainer1D > const &  V,
ITContainer1D< Real, TContainer1D > const &  W,
bool  unbiased = false 
)

Compute the weighted variance of the variable V.

\[ \hat{\sigma}^2 = \frac{\sum_{i=1}^n W(i)}{\left( \sum_{i=1}^n W(i))\right)^2-\sum_{i=1}^n W(i)^2} \sum_{i=1}^n W(i) (V(i)-\hat{\mu})^2. \]

If there is no weights, this definition reduces to the usual definition of the variance with factor 1/(n-1). If the range of the weights is not equal to the range of the varaible, the usual varaince is computed.

Parameters:
Vvariable
Wweights
unbiasedtrue if we want an unbiased estimator of the variance, false otherwise (default is false)

Definition at line 848 of file STK_Stat_UnivariateReal.h.

References STK::abs(), STK::IContainer1D::empty(), STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), STK::Stat::mean(), STK::ITContainer1D< TYPE, TContainer1D >::range(), STK::sum(), and STK::Stat::varianceWithFixedMean().

{
  // no samples
  if (V.empty()) { return Arithmetic<Real>::NA();}

  // Compute the mean if necessary
  Real mu = mean(V, W);

  // if the weight are not of the same size, ignore them
  if (V.range() != W.range()) return varianceWithFixedMean(V, mu);

  // get dimensions
  const Integer  first = V.first(), last = V.last();
  // sum
  Real dev, sum = 0.0, var = 0.0, nweight = 0.0, nweight2 = 0.0;
  for (Integer i=first; i<=last; i++)
  { if ( Arithmetic<Real>::isFinite(V[i]) && Arithmetic<Real>::isFinite(W[i]) )
    {
      Real weight = abs(W[i]);
      nweight    += weight;
      nweight2   += weight * weight;
      sum        += weight*(dev = V[i]-mu); // deviation from the mean
      var        += weight*(dev*dev);       // squared value
    }
  }
  // compute the variance
  if (unbiased)
  {
    return (nweight*nweight - nweight2 > 0.) ? (var - sum*sum/nweight)/(nweight - nweight2/nweight)
                                             : Arithmetic<Real>::NA();

  }
  return (nweight) ? (var - sum*sum)/(nweight) : Arithmetic<Real>::NA();
}
template<class TContainer1D >
Real STK::Stat::varianceWithFixedMean ( ITContainer1D< Real, TContainer1D > const &  V,
Real const &  mu,
bool  unbiased = false 
)

Compute the variance of the variable V with fixed mean.

\[ \hat{\mu} = \frac{1}{n} \sum_{i=1}^n (V(i) - \mu)^2. \]

Parameters:
Vvariable
muthe mean
unbiasedtrue if we want an unbiased estimator of the variance, false otherwise (default is false)

Definition at line 897 of file STK_Stat_UnivariateReal.h.

References STK::IContainer1D::empty(), STK::ITContainer1D< TYPE, TContainer1D >::first(), STK::ITContainer1D< TYPE, TContainer1D >::last(), STK::ITContainer1D< TYPE, TContainer1D >::size(), and STK::sum().

{
  // no samples
  if (V.empty()) { return Arithmetic<Real>::NA();}

  // get dimensions
  const Integer  first = V.first(), last = V.last();
  Integer nobs = V.size();
  // sum
  Real sum  = 0.0, var  = 0.0, dev;
  for (Integer i=first; i<=last; i++)
  {
    if (Arithmetic<Real>::isFinite(V[i]))
    {
      sum += (dev = V[i] - mu); // deviation from the mean
      var += (dev*dev);         // squared value
    }
    else nobs--;
  }
  // compute the variance
  if (unbiased)
  {
    return (nobs > 1) ? (var - (sum*sum)/(Real)nobs)/(Real)(nobs -1)
                      : Arithmetic<Real>::NA();

  }
  // variance
  return (nobs > 0) ? (var - (sum*sum)/(Real)nobs)/(Real)(nobs)
                    : Arithmetic<Real>::NA();
}