|
STK++ 1.0
|
In this sub-project, we compute the usual descriptive statistics of variables. More...
|
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
| |
| 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
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. | |
In this sub-project, we compute the usual descriptive statistics of variables.
| 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.
| X | first variable |
| Y | second variable |
| unbiased | true 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();
}
| 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.
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.
| X | first variable |
| Y | second variable |
| W | weights |
| unbiased | true 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();
}
| 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.
| X | first variable |
| Y | second variable |
| xMean | the mean of X |
| yMean | the mean of Y |
| unbiased | true 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();
}
| 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.
| X | first variable |
| Y | second variable |
| W | weights |
| xMean | the mean of X |
| yMean | the mean of Y |
| unbiased | true 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();
}
| TContainerHo STK::Stat::mean | ( | ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const & | V | ) |
Compute the mean of the container V.
| V | the 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 STK::Stat::mean | ( | ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const & | V, |
| TContainerVe const & | W | ||
| ) |
Compute the (weighted) mean of the variable V
if there is weights.
| V | the variable |
| W | the 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().
| TContainerHo STK::Stat::variance | ( | ITContainer2D< Real, TContainerHo, TContainerVe, TContainer2D > const & | V, |
| bool | unbiased = false |
||
| ) |
Compute the variance of the variable V.
| V | variable |
| unbiased | true 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;
}
| 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.
If there is no weights, this definition reduces to the usual definition of the variance with factor 1/(n-1).
| V | variable |
| W | weights |
| unbiased | true 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;
}
| 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.
| V | variable |
| mu | mean of the variable V |
| unbiased | true 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;
}
| 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.
| V | variable |
| W | weights |
| mu | weighted mean of V |
| unbiased | true 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;
}
| Real STK::Stat::mean | ( | ITContainer1D< Real, TContainer1D > const & | V | ) |
Compute the mean of the variable V.
| V | the 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();
}
| Real STK::Stat::mean | ( | ITContainer1D< Real, TContainer1D > const & | V, |
| ITContainer1D< Real, TContainer1D > const & | W | ||
| ) |
Compute the (weighted) mean of the variable V
If the range of the weights does not match the range of the variable the method return the usual mean.
| V | variable |
| W | weights |
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.;
}
| Real STK::Stat::variance | ( | ITContainer1D< Real, TContainer1D > const & | V, |
| bool | unbiased = false |
||
| ) |
Compute the variance of the variable V.
| V | variable |
| unbiased | true 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();
}
| 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.
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.
| V | variable |
| W | weights |
| unbiased | true 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();
}
| 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.
| V | variable |
| mu | the mean |
| unbiased | true 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();
}