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

Compute safely 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

 VarianceWithFixedMeanSafeOp (ExprBase< Derived > const &V)
 constructor
 
Type const operator() (Type 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::VarianceWithFixedMeanSafeOp< Derived >

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

Definition at line 635 of file STK_Stat_Functors.h.

Member Typedef Documentation

◆ Type

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

Definition at line 637 of file STK_Stat_Functors.h.

Constructor & Destructor Documentation

◆ VarianceWithFixedMeanSafeOp()

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

constructor

Definition at line 639 of file STK_Stat_Functors.h.

639 : 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::VarianceWithFixedMeanSafeOp< Derived >::operator() ( ExprBase< Weights > const w,
Type const mu,
bool  unbiased 
) const
inline
Returns
the safely computed 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
\]

Parameters
wweights
muthe mean
unbiasedtrue if we want an unbiased estimate of the variance, false otherwise

Definition at line 687 of file STK_Stat_Functors.h.

688 {
690 // no samples
691 if (V_.empty()) { return Type(0);}
692 if (V_.range() != w.range())
693 STKRUNTIME_ERROR_NO_ARG(wmeanSafe,V.range()!=w.range());
694 // sum the weighted samples
695 Type sum = 0.0, sum1weights = 0.0, sum2weights = 0.0;
696 for (int i=V_.begin(); i<V_.end(); i++)
697 {
699 { Type weight = std::abs(w[i]);
700 sum1weights += weight;
701 sum2weights += weight * weight;
702 sum += weight*(V_[i]-mu)*(V_[i]-mu); // deviation from the mean
703 }
704 }
705 // compute the variance
706 return (unbiased) ?
707 (sum1weights) ? (sum)/(sum1weights - sum2weights/sum1weights)
708 : Arithmetic<Type>::infinity()
709 :
710 (sum1weights) ? (sum/sum1weights) : Type(0);
711 }
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
hidden::FunctorTraits< Derived, SumOp >::Row sum(Derived const &A)
Compute the sum of A.
static bool isNA(Type const &x)

References STK::Arithmetic< Type >::isNA(), STK_STATIC_ASSERT_ONE_DIMENSION_ONLY, STKRUNTIME_ERROR_NO_ARG, STK::Stat::sum(), and STK::Stat::VarianceWithFixedMeanSafeOp< Derived >::V_.

◆ operator()() [2/2]

template<class Derived >
Type const STK::Stat::VarianceWithFixedMeanSafeOp< Derived >::operator() ( Type  mu,
bool  unbiased 
) const
inline
Returns
the safely computed 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 and discarding the missing values.
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 652 of file STK_Stat_Functors.h.

653 {
654 // no samples
655 if (V_.empty()) { return Type(0);}
656 int nobs = V_.size();
657 // sum
658 Type sum = 0.0, var = 0.0, dev;
659 for (int i=V_.begin(); i<V_.end(); i++)
660 {
661 if (!Arithmetic<Type>::isNA(V_[i]))
662 {
663 sum += (dev = V_[i] - mu); // deviation from the mean
664 var += (dev*dev); // squared value
665 }
666 else nobs--;
667 }
668 // compute the variance
669 return (unbiased) ?
670 (nobs > 1) ? (var - (sum*sum)/Type(nobs))/Type(nobs -1)
671 : Arithmetic<Type>::infinity()
672 :
673 (nobs > 0) ? (var - (sum*sum)/(Type)nobs)/(Type)(nobs)
674 : Type(0.);
675 }

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

Member Data Documentation

◆ V_


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