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

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

#include <STK_Stat_Functors.h>

Public Types

typedef Derived::Type Type
 

Public Member Functions

 MeanSafeOp (ExprBase< Derived > const &V)
 constructor
 
Type const operator() () const
 
template<class Weights >
Type const operator() (ExprBase< Weights > const &w) const
 

Protected Attributes

Derived constV_
 

Detailed Description

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

Compute safely mean of the variable V.

Definition at line 344 of file STK_Stat_Functors.h.

Member Typedef Documentation

◆ Type

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

Definition at line 346 of file STK_Stat_Functors.h.

Constructor & Destructor Documentation

◆ MeanSafeOp()

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

constructor

Definition at line 348 of file STK_Stat_Functors.h.

348 : 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::MeanSafeOp< Derived >::operator() ( ) const
inline
Returns
the safely computed mean of the variable V discarding all missing values.

Definition at line 351 of file STK_Stat_Functors.h.

352 {
353 // no samples
354 if (V_.empty()) { return Type(0);}
355 // get dimensions
356 int nobs = V_.size();
357 // sum the samples
358 Type sum = 0.0;
359 for (int i=V_.begin(); i<V_.end(); i++)
360 { (!Arithmetic<Type>::isNA(V_[i])) ? sum += V_[i] : nobs--;}
361 // compute the mean
362 return nobs ? (sum /= Type(nobs)) : Type(0);
363 }
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::Stat::sum(), and STK::Stat::MeanSafeOp< Derived >::V_.

◆ operator()() [2/2]

template<class Derived >
template<class Weights >
Type const STK::Stat::MeanSafeOp< Derived >::operator() ( ExprBase< Weights > const w) const
inline
Returns
the safely computed weighted mean of the variable V
Parameters
wthe weights

Definition at line 368 of file STK_Stat_Functors.h.

369 {
371 // no samples
372 if (V_.empty()) { return Type(0);}
373#ifdef STK_BOUNDS_CHECK
374 if (V_.range() != w.range())
375 STKRUNTIME_ERROR_NO_ARG(wmeanSafe,V.range()!=w.range());
376#endif
377 // sum the weighted samples
378 Type sum = 0.0, sumweights= 0.0;
379 for (int i=V_.begin(); i<V_.end(); i++)
380 { if ( (!Arithmetic<Type>::isNA(V_[i])) && (!Arithmetic<Type>::isNA(w[i])))
381 {
382 Type weight = std::abs(w[i]);
383 sumweights += weight;
384 sum += weight * V_[i];
385 }
386 }
387 // compute the weighted mean. If all weights are 0, we get 0
388 return (sumweights) ? (sum / sumweights) : Type(0);
389 }
#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::MeanSafeOp< Derived >::V_.

Member Data Documentation

◆ V_

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

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