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

Compute the variance of the variable V. More...

#include <STK_Stat_Functors.h>

Public Types

typedef Derived::Type Type
 

Public Member Functions

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

Protected Attributes

Derived constV_
 

Detailed Description

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

Compute the variance of the variable V.

Definition at line 398 of file STK_Stat_Functors.h.

Member Typedef Documentation

◆ Type

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

Definition at line 400 of file STK_Stat_Functors.h.

Constructor & Destructor Documentation

◆ VarianceOp()

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

constructor

Definition at line 402 of file STK_Stat_Functors.h.

402 : 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 >
Type const STK::Stat::VarianceOp< Derived >::operator() ( bool  unbiased) const
inline
Returns
the variance of the variable V.

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

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

Definition at line 409 of file STK_Stat_Functors.h.

410 {
411 // no samples
412 if (V_.empty()) { return Type(0);}
413
414 int nobs = V_.size();
415 // Compute the mean and sum
416 Type mu = MeanOp<Derived>(V_)();
417 Type sum = 0.0, var = 0.0, dev;
418 for (int i=V_.begin(); i<V_.end(); i++)
419 {
420 sum += (dev = V_[i] - mu); // deviation from the mean
421 var += (dev*dev); // squared value
422 }
423 // compute the variance
424 return (unbiased) ?
425 (nobs > 1) ? (var - (sum*sum)/Type(nobs))/Type(nobs -1)
426 : Arithmetic<Type>::infinity()
427 :
428 (nobs > 0) ? (var - (sum*sum)/(Type)nobs)/(Type)(nobs)
429 : Type(0);
430 }
hidden::FunctorTraits< Derived, SumOp >::Row sum(Derived const &A)
Compute the sum of A.

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

◆ operator()() [2/2]

template<class Derived >
template<class Weights >
Type const STK::Stat::VarianceOp< Derived >::operator() ( ExprBase< Weights > const w,
bool  unbiased 
) const
inline
Returns
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
wweights
unbiasedtrue if we want an unbiased estimate of the variance, false otherwise

Definition at line 443 of file STK_Stat_Functors.h.

444 {
446 // no samples
447 if (V_.empty()) { return Type(0);}
448 // Compute the mean
449 Type mu = MeanOp<Derived>(V_)(w);
450 // sum the weighted samples
451 Type sum = 0.0, sum1weights= 0.0, sum2weights = 0.0;
452 for (int i=V_.begin(); i<V_.end(); i++)
453 {
454 Type const weight = w[i];
455 sum1weights += weight;
456 sum2weights += weight * weight;
457 sum += weight*(V_[i]-mu)*(V_[i]-mu); // deviation from the mean
458 }
459 // compute the variance
460 return(unbiased) ?
461 (sum1weights) ? (sum)/(sum1weights- sum2weights/sum1weights) : Type(0)
462 :
463 (sum1weights) ? (sum/sum1weights) : Type(0);
464 }

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

Member Data Documentation

◆ V_

template<class Derived >
Derived const& STK::Stat::VarianceOp< Derived >::V_
protected

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