STK++ 1.0

STK::Stat Namespace Reference

this is the namespace for the statistical treatment. More...

Classes

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

Typedefs

typedef Multivariate< Real,
Point, Vector, Matrix
MultivariateMatrix

Functions

template<class TContainer1D >
Real 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 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 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 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 mean (ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &V)
 Compute the mean of the container V.
template<class TContainerHo , class TContainerVe , class TContainer2D >
TContainerHo 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 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 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 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 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 TContainerHo , class TContainerVe , class TContainer2D >
void center (ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > &V, Vector &mean)
 Compute the mean of the variables in the container V and center V.
template<class TContainerHo , class TContainerVe , class TContainer2D >
void center (ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &V, Vector const &W, Vector &mean)
 Compute the mean of the variables in the container V and center V.
template<class TContainerHo , class TContainerVe , class TContainer2D >
void standardize (ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > &V, Vector &mean, Vector &std)
 Compute the mean and the variance of the variable V and standardize it.
template<class TContainerHo , class TContainerVe , class TContainer2D >
void standardize (ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > &V, Vector const &W, Vector &mean, Vector &std)
 Compute the mean and the variance of the variable V and standardize it.
template<class TContainerHo , class TContainerVe , class TContainer2D >
void decenter (ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > &V, Vector const &mean)
 Add the mean of the centered variables in the container V.
template<class TContainerHo , class TContainerVe , class TContainer2D >
void destandardize (ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > &V, Vector const &mean, Vector const &std)
 add the mean and dilate the standardized variable V.
template<class TContainer1D >
Real mean (ITContainer1D< Real, TContainer1D > const &V)
 Compute the mean of the variable V.
template<class TContainer1D >
Real 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 variance (ITContainer1D< Real, TContainer1D > const &V, bool unbiased=false)
 Compute the variance of the variable V.
template<class TContainer1D >
Real 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 varianceWithFixedMean (ITContainer1D< Real, TContainer1D > const &V, Real const &mu, bool unbiased=false)
 Compute the variance of the variable V with fixed mean.
template<class TContainer1D >
Real varianceWithFixedMean (ITContainer1D< Real, TContainer1D > const &V, ITContainer1D< Real, TContainer1D > const &W, Real const &mu, bool unbiased=false)
 Compute the weighted variance of the variable V with fixed mean.

Detailed Description

this is the namespace for the statistical treatment.

The namespace Stat is the domain space for the usual statistical treatment of the variable like mean, variance, covariance, ...


Typedef Documentation


Function Documentation

template<class TContainerHo , class TContainerVe , class TContainer2D >
void STK::Stat::center ( ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > &  V,
Vector &  mean 
)

Compute the mean of the variables in the container V and center V.

Parameters:
Vthe container with the Data
meanthe Vector of the means

Definition at line 55 of file STK_Stat_Transform.h.

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

Referenced by standardize().

{
  mean.resize(V.rangeHo());
  // get dimensions
  const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
  for (Integer j= firstVar; j<= lastVar; j++)
  {
    // compute mean
    Real mu = Stat::mean<TContainerVe>(V[j]);
    // save current mean
    mean[j] = Arithmetic<Real>::isFinite(mu) ?  0. : mu;
    // translate data
    V[j] -= mu;
  }
}
template<class TContainerHo , class TContainerVe , class TContainer2D >
void STK::Stat::center ( ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const &  V,
Vector const &  W,
Vector &  mean 
)

Compute the mean of the variables in the container V and center V.

Parameters:
Vthe container with the Data
Wthe Vector of the weights
meanthe Vector of the means

Definition at line 77 of file STK_Stat_Transform.h.

References STK::IContainer2D::firstCol(), STK::IContainer2D::lastCol(), STK::ITContainer1D< TYPE, TContainer1D >::range(), STK::IContainer2D::rangeHo(), STK::IContainer2D::rangeVe(), and STK::IContainer1D::resize().

{
#ifdef STK_DEBUG
  if (W.range() != V.rangeVe())
    throw std::runtime_error("In center(V, W, mean) "
                             "W.range() != V.rangeVe()");
#endif
  // create result
  mean.resize(V.rangeHo());
  // get dimensions
  const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
  for (Integer j= firstVar; j<= lastVar; j++)
  {
    // compute mean
    Real mu = Stat::mean<TContainerVe>(V[j], W);
    // save current mean
    mean[j] = Arithmetic<Real>::isFinite(mu) ?  0. : mu;
    // translate data
    V[j] -= mu;
  }
}
template<class TContainerHo , class TContainerVe , class TContainer2D >
void STK::Stat::standardize ( ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > &  V,
Vector &  mean,
Vector &  std 
)

Compute the mean and the variance of the variable V and standardize it.

Parameters:
Vthe container with the Data
meanthe Vector of the means
stdthe Vector of the standard deviation

Definition at line 108 of file STK_Stat_Transform.h.

References center(), STK::IContainer2D::firstCol(), STK::IContainer2D::lastCol(), STK::IContainer2D::rangeHo(), STK::IContainer1D::resize(), and variance().

{
    // create result
    mean.resize(V.rangeHo());
    std.resize(V.rangeHo());
    // center
    center(V, mean);
    // get dimensions
    const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
    for (Integer j= firstVar; j<= lastVar; j++)
    {
      // compute standard deviation
      Real dev = Stat::variance(V[j]);
      // take square root
      dev = Arithmetic<Real>::isFinite(dev) ?  0. : sqrt((double)dev);
      // save result
      std[j] = dev;
      // standardize
      if (dev) V[j] /= dev;
    }
}
template<class TContainerHo , class TContainerVe , class TContainer2D >
void STK::Stat::standardize ( ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > &  V,
Vector const &  W,
Vector &  mean,
Vector &  std 
)

Compute the mean and the variance of the variable V and standardize it.

Parameters:
Vthe container with the Data
Wthe Vector of the weights
meanthe Vector of the means
stdthe Vector of the standard deviation

Definition at line 140 of file STK_Stat_Transform.h.

References center(), STK::IContainer2D::firstCol(), STK::IContainer2D::lastCol(), STK::ITContainer1D< TYPE, TContainer1D >::range(), STK::IContainer2D::rangeHo(), STK::IContainer2D::rangeVe(), STK::IContainer1D::resize(), and variance().

{
#ifdef STK_DEBUG
  if (W.range() != V.rangeVe())
    throw std::runtime_error("In standardize(V, W, mean) "
                             "W.range() != V.rangeVe()");
#endif
  // create result
  mean.resize(V.rangeHo());
  std.resize(V.rangeHo());
  // center
  center(V, W, mean);
  // get dimensions
  const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
  for (Integer j= firstVar; j<= lastVar; j++)
  {
    // compute standard deviation
    Real dev = Stat::variance(V[j], W);
    // take square root
    dev = Arithmetic<Real>::isFinite(dev) ?  0. : sqrt((double)dev);
    // save result
    std[j] = dev;
    // standardize
    if (dev) V /= dev;
  }
}
template<class TContainerHo , class TContainerVe , class TContainer2D >
void STK::Stat::decenter ( ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > &  V,
Vector const &  mean 
)

Add the mean of the centered variables in the container V.

Parameters:
Vthe container with the data
meanthe Vector of the means

Definition at line 176 of file STK_Stat_Transform.h.

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

Referenced by destandardize(), and STK::AAModelFactory::run().

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

add the mean and dilate the standardized variable V.

Parameters:
Vthe container with the Data
meanthe Vector of the means
stdthe Vector of the standard deviation

Definition at line 193 of file STK_Stat_Transform.h.

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

Referenced by STK::AAModelFactory::run().

{
  // get dimensions
  const Integer  firstVar = V.firstCol(), lastVar = V.lastCol();
  for (Integer j= firstVar; j<= lastVar; j++)
  {
    // dilate
    if (std[j]) V[j] *= std[j];
  }
  // decenter
  decenter(V, mean);
}
template<class TContainer1D >
Real STK::Stat::varianceWithFixedMean ( ITContainer1D< Real, TContainer1D > const &  V,
ITContainer1D< Real, TContainer1D > const &  W,
Real const &  mu,
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:
Vvariable
Wweights
muthe mean
unbiasedtrue if we want an unbiased estimator of the variance, false otherwise (default is false)

Definition at line 942 of file STK_Stat_UnivariateReal.h.

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

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

  // 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();
}