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

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

#include <STK_Stat_Functors.h>

Public Types

typedef Derived::Type Type
 

Public Member Functions

 VarianceSafeOp (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::VarianceSafeOp< Derived >

Compute safely the variance of the variable V.

Definition at line 473 of file STK_Stat_Functors.h.

Member Typedef Documentation

◆ Type

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

Definition at line 475 of file STK_Stat_Functors.h.

Constructor & Destructor Documentation

◆ VarianceSafeOp()

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

constructor

Definition at line 477 of file STK_Stat_Functors.h.

477 : 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::VarianceSafeOp< Derived >::operator() ( bool  unbiased) const
inline
Returns
the variance of the variable V discarding all missing values.

\[ \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 484 of file STK_Stat_Functors.h.

485 {
486 // no samples
487 if (V_.empty()) { return Type(0);}
488 int nobs = V_.size();
489 // Compute the mean
490 Type mu = MeanSafeOp<Derived>(V_)();
491 // sum
492 Type sum = 0.0, var = 0.0, dev;
493 for (int i=V_.begin(); i<V_.end(); i++)
494 {
495 if (!Arithmetic<Type>::isNA(V_[i]))
496 {
497 sum += (dev = V_[i] - mu); // deviation from the mean
498 var += (dev*dev); // squared value
499 }
500 else nobs--;
501 }
502 // compute the variance
503 return (unbiased) ?
504 (nobs > 1) ? (var - (sum*sum)/Type(nobs))/Type(nobs -1)
505 : Arithmetic<Type>::infinity()
506 :
507 (nobs > 0) ? (var - (sum*sum)/(Type)nobs)/(Type)(nobs)
508 : Type(0);
509 }
hidden::FunctorTraits< Derived, SumOp >::Row sum(Derived const &A)
Compute the sum of A.
static bool isNA(Type const &x)

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

◆ operator()() [2/2]

template<class Derived >
template<class Weights >
Type const STK::Stat::VarianceSafeOp< Derived >::operator() ( ExprBase< Weights > const w,
bool  unbiased 
) const
inline
Returns
the safely computed 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 522 of file STK_Stat_Functors.h.

523 {
525 // no samples
526 if (V_.empty()) { return Type(0);}
527 // Compute the mean
528 Type mu = MeanSafeOp<Derived>(V_)(w);
529#ifdef STK_BOUNDS_CHECK
530 if (V_.range() != w.range())
531 STKRUNTIME_ERROR_NO_ARG(VarianceSafeOp,V.range()!=w.range());
532#endif
533 // sum the weighted samples
534 Type sum = 0.0, sum1weights= 0.0, sum2weights = 0.0;
535 for (int i=V_.begin(); i<V_.end(); i++)
536 { if ( !Arithmetic<Type>::isNA(V_[i]) && !Arithmetic<Type>::isNA(w[i]) )
537 {
538 Type weight = std::abs(w[i]);
539 sum1weights += weight;
540 sum2weights += weight * weight;
541 sum += weight*(V_[i]-mu)*(V_[i]-mu); // deviation from the mean
542 }
543 }
544 // compute the variance
545 return (unbiased) ?
546 (sum1weights) ? (sum)/(sum1weights- sum2weights/sum1weights) : 0.
547 :
548 (sum1weights) ? (sum/sum1weights) : 0.;
549 }
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138

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

Member Data Documentation

◆ V_

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

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