STK++ 0.9.13
STK::Stat::VarianceWithFixedMeanOp< Derived > Struct Template Reference

Compute the variance of the variable V when the mean is given. More...

#include <STK_Stat_Functors.h>

Public Types

typedef Derived::Type Type
 

Public Member Functions

 VarianceWithFixedMeanOp (ExprBase< Derived > const &V)
 constructor
 
Type const operator() (Type const &mu, bool unbiased) const
 
template<class Weights >
Type const operator() (ExprBase< Weights > const &w, Type const &mu, bool unbiased) const
 

Protected Attributes

Derived constV_
 

Detailed Description

template<class Derived>
struct STK::Stat::VarianceWithFixedMeanOp< Derived >

Compute the variance of the variable V when the mean is given.

Definition at line 558 of file STK_Stat_Functors.h.

Member Typedef Documentation

◆ Type

template<class Derived >
typedef Derived::Type STK::Stat::VarianceWithFixedMeanOp< Derived >::Type

Definition at line 560 of file STK_Stat_Functors.h.

Constructor & Destructor Documentation

◆ VarianceWithFixedMeanOp()

template<class Derived >
STK::Stat::VarianceWithFixedMeanOp< Derived >::VarianceWithFixedMeanOp ( ExprBase< Derived > const V)
inline

constructor

Definition at line 562 of file STK_Stat_Functors.h.

562 : V_(V.asDerived())
#define STK_STATIC_ASSERT_ONE_DIMENSION_ONLY(EXPR)

References STK_STATIC_ASSERT_ONE_DIMENSION_ONLY.

Member Function Documentation

◆ operator()() [1/2]

template<class Derived >
template<class Weights >
Type const STK::Stat::VarianceWithFixedMeanOp< Derived >::operator() ( ExprBase< Weights > const w,
Type const mu,
bool  unbiased 
) const
inline
Returns
the weighted variance of the variable V with fixed mean.

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

In the unbiased case, the method use the compensated method described in http://en.wikipedia.org/wiki/Weighted_arithmetic_mean#Weighted_sample_variance
Parameters
wweights
muthe mean
unbiasedtrue if we want an unbiased estimate of the variance, false otherwise

Definition at line 606 of file STK_Stat_Functors.h.

607 {
609 // no samples
610 if (V_.empty()) { return Type(0);}
611 // sum the weighted samples
612 Type sum = 0.0, sum1weights = 0.0, sum2weights = 0.0;
613 for (int i=V_.begin(); i<V_.end(); i++)
614 {
615 Real weight = std::abs(w[i]); // only positive weights
616 sum1weights += weight;
617 sum2weights += weight * weight;
618 sum += weight * (V_[i]-mu)*(V_[i]-mu); // deviation from the mean
619 }
620 // compute the variance
621 return (unbiased) ?
622 (sum1weights) ? (sum)/(sum1weights - sum2weights/sum1weights)
623 : Arithmetic<Type>::infinity()
624 :
625 (sum1weights) ? (sum/sum1weights) : Type(0);
626 }
double Real
STK fundamental type of Real values.
hidden::FunctorTraits< Derived, SumOp >::Row sum(Derived const &A)
Compute the sum of A.

References STK_STATIC_ASSERT_ONE_DIMENSION_ONLY, STK::Stat::sum(), and STK::Stat::VarianceWithFixedMeanOp< Derived >::V_.

◆ operator()() [2/2]

template<class Derived >
Type const STK::Stat::VarianceWithFixedMeanOp< Derived >::operator() ( Type const mu,
bool  unbiased 
) const
inline
Returns
the variance of the variable V with fixed mean.

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

using a compensated algorithm.
Note
Chan, Tony F.; Golub, Gene H.; LeVeque, Randall J. (1983). Algorithms for Computing the Sample Variance: Analysis and Recommendations. The American Statistician 37, 242-247.
Parameters
muthe fixed mean
unbiasedtrue if we want an unbiased estimate of the variance, false otherwise

Definition at line 575 of file STK_Stat_Functors.h.

576 {
577 // no samples
578 if (V_.empty()) { return Type(0);}
579 int nobs = V_.size();
580 // sum
581 Type sum = 0., var = 0., dev;
582 for (int i=V_.begin(); i<V_.end(); i++)
583 {
584 sum += (dev = V_[i] - mu); // deviation from the mean
585 var += (dev*dev); // squared value
586 }
587 // unbiased variance
588 return (unbiased) ?
589 (nobs > 1) ? (var - (sum*sum)/(Type)(nobs))/((Type)nobs -1)
590 : Arithmetic<Type>::infinity()
591 :
592 (nobs > 0) ? (var - (sum*sum)/Type(nobs))/Type(nobs) : Type(0);
593 }

References STK::Stat::sum(), and STK::Stat::VarianceWithFixedMeanOp< Derived >::V_.

Member Data Documentation

◆ V_


The documentation for this struct was generated from the following file: