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

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

#include <STK_Stat_Functors.h>

Public Types

typedef Derived::Type Type
 

Public Member Functions

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

Compute the mean of the variable V.

Definition at line 297 of file STK_Stat_Functors.h.

Member Typedef Documentation

◆ Type

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

Definition at line 299 of file STK_Stat_Functors.h.

Constructor & Destructor Documentation

◆ MeanOp()

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

constructor

Definition at line 301 of file STK_Stat_Functors.h.

301 : 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::MeanOp< Derived >::operator() ( ) const
inline
Returns
the mean of the variable V

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

Definition at line 306 of file STK_Stat_Functors.h.

307 {
308 // no samples
309 if (V_.empty()) { return Type(0);}
310 // sum the samples
311 Type sum = 0.;
312 for (int i=V_.begin(); i<V_.end(); i++) { sum += V_[i];}
313 return sum /= (Type)(V_.size());
314 }
hidden::FunctorTraits< Derived, SumOp >::Row sum(Derived const &A)
Compute the sum of A.

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

◆ operator()() [2/2]

template<class Derived >
template<class Weights >
Type const STK::Stat::MeanOp< Derived >::operator() ( ExprBase< Weights > const w) const
inline
Returns
the weighted mean value of the variable V

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

Parameters
wthe weights

Definition at line 320 of file STK_Stat_Functors.h.

321 {
323 // no samples
324 if (V_.empty()) { return Type(0);}
325 // sum the weighted samples
326 Type sum = 0.0, sumweights= 0.0;
327 for (int i=V_.begin(); i<V_.end(); i++)
328 {
329 Type const weight = std::abs(w[i]);
330 sumweights += weight;
331 sum += weight * V_[i];
332 }
333 // compute the weighted mean. If all weights are 0, we get 0
334 return (sumweights) ? sum /= sumweights: Type(0);
335 }

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

Member Data Documentation

◆ V_

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

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