STK++ 1.0
STK::Stat::Bivariate< Real, Real, TContainer1D > Class Template Reference

Computation of the bivariate Statistics of two Variables. More...

#include <STK_Stat_BivariateRealReal.h>

Collaboration diagram for STK::Stat::Bivariate< Real, Real, TContainer1D >:

List of all members.

Public Member Functions

 Bivariate (ITContainer1D< Real, TContainer1D > const &X, ITContainer1D< Real, TContainer1D > const &Y)
 Default constructor.
 Bivariate (ITContainer1D< Real, TContainer1D > const &X, ITContainer1D< Real, TContainer1D > const &Y, TContainer1D const &W)
 Default constructor.
 Bivariate (Bivariate< Real, Real, TContainer1D > const &stat)
 Copy constructor.
virtual ~Bivariate ()
 virtual destructor.
Bivariateoperator= (Bivariate< Real, Real, TContainer1D > const &stat)
 Operator = : overwrite the BivariateRealReal with stat.
void setData (ITContainer1D< Real, TContainer1D > const &X, ITContainer1D< Real, TContainer1D > const &Y)
 set a new data sets
void setData (ITContainer1D< Real, TContainer1D > const &X, ITContainer1D< Real, TContainer1D > const &Y, TContainer1D const &W)
 set a new data sets
Univariate< Real, TContainer1D >
const & 
xStat () const
 get statistics of the first variable
Univariate< Real, TContainer1D >
const & 
yStat () const
 get statistics of the first variable
Real const & covariance () const
 get covariance between the variables
Real const & correlation () const
 get correlation between the variables
Real const & unbiasedCovariance () const
 get the unbiased covariance between the variables
Real const & unbiasedCorrelation () const
 get the unbiased correlation between the variables

Protected Member Functions

void compCovariance ()
 compute the covariance
void compWeightedCovariance ()
 compute the weighted covariance

Protected Attributes

TContainer1D X_
 reference on the X data set.
TContainer1D Y_
 reference on the Y data set.
TContainer1D W_
 local copy of the weights
Univariate< Real, TContainer1D > xStat_
 Statistics of the X variable.
Univariate< Real, TContainer1D > yStat_
 Statistics of the Y variable.
Real cov_
 covariance between X and Y
Real ucov_
 unbiased covariance between X and Y
Real cor_
 correlation between X and Y
Real ucor_
 unbiased correlation between X and Y
Real sumWeights_
 Sum of the weights.
Real sum2Weights_
 Sum of the square of the weights.

Detailed Description

template<class TContainer1D>
class STK::Stat::Bivariate< Real, Real, TContainer1D >

Computation of the bivariate Statistics of two Variables.

The template parameter TContainer1D is the type of container used for storing the data : It should derive from ITContainer1D and contain elements of type Real.

This specialized class compute the Real univariate descriptive statistics of X and Y and the covariance between X and Y.

The covariance between X and Y will be computed using the formula

See also:
Mark Galassi, Jim Davies, James Theiler, Brian Gough, Gerard Jungman, Michael Booth, and Fabrice Rossi. http://www.gnu.org/software/gsl/manual GNU Scientific Library - Reference manual, Version 1.15, 2011. http://www.gnu.org/software/gsl/manual/html_node/Weighted-Samples.html Sec. 21.7 Weighted Samples

$ cov(X,Y)=\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 \left( x_{i}-\bar{x} \right) \left( y_{i}-\bar{y} \right). $

Definition at line 68 of file STK_Stat_BivariateRealReal.h.


Constructor & Destructor Documentation

template<class TContainer1D >
STK::Stat::Bivariate< Real, Real, TContainer1D >::Bivariate ( ITContainer1D< Real, TContainer1D > const &  X,
ITContainer1D< Real, TContainer1D > const &  Y 
) [inline]

Default constructor.

Copy locally the vectors X and Y and compute the univariate and bivariate statistics.

Parameters:
Xthe first variable
Ythe second variable

Definition at line 76 of file STK_Stat_BivariateRealReal.h.

             : X_(X.asLeaf())
             , Y_(Y.asLeaf())
             , W_()
             , xStat_(X)
             , yStat_(Y)
             , cov_(0.)
             , ucov_(0.)
             , cor_(0.)
             , ucor_(0.)
             , sumWeights_(0.0)
             , sum2Weights_(0.0)
    {
      compCovariance();
    }
template<class TContainer1D >
STK::Stat::Bivariate< Real, Real, TContainer1D >::Bivariate ( ITContainer1D< Real, TContainer1D > const &  X,
ITContainer1D< Real, TContainer1D > const &  Y,
TContainer1D const &  W 
) [inline]

Default constructor.

Compute the univariate statistics of the two variables. The

Parameters:
Xthe first variable
Ythe second variable
Wthe weights

Definition at line 100 of file STK_Stat_BivariateRealReal.h.

             : X_(X.asLeaf())
             , Y_(Y.asLeaf())
             , W_(W)
             , xStat_(X, W)
             , yStat_(Y, W)
             , cov_(0.)
             , ucov_(0.)
             , cor_(0.)
             , ucor_(0.)
             , sumWeights_(0.0)
             , sum2Weights_(0.0)
    {
      compWeightedCovariance();
    }
template<class TContainer1D >
STK::Stat::Bivariate< Real, Real, TContainer1D >::Bivariate ( Bivariate< Real, Real, TContainer1D > const &  stat) [inline]

Copy constructor.

Parameters:
statthe statistics to copy

Definition at line 121 of file STK_Stat_BivariateRealReal.h.

              : xStat_(stat.xStat_)
              , yStat_(stat.yStat_)
              , cov_(stat.cov_)
              , ucov_(stat.ucov_)
              , cor_(stat.cor_)
              , ucor_(stat.ucor_)
              , sumWeights_(stat.nweight_)
              , sum2Weights_(stat.nweight2_)
    { }
template<class TContainer1D >
virtual STK::Stat::Bivariate< Real, Real, TContainer1D >::~Bivariate ( ) [inline, virtual]

virtual destructor.

Definition at line 133 of file STK_Stat_BivariateRealReal.h.

{ ;}

Member Function Documentation

template<class TContainer1D >
Bivariate& STK::Stat::Bivariate< Real, Real, TContainer1D >::operator= ( Bivariate< Real, Real, TContainer1D > const &  stat) [inline]

Operator = : overwrite the BivariateRealReal with stat.

Parameters:
statthe statistics to copy
Returns:
this object

Definition at line 139 of file STK_Stat_BivariateRealReal.h.

References cor_, cov_, ucor_, ucov_, W_, xStat_, STK::Stat::Bivariate< XTYPE, YTYPE, TContainer1D >::xStat_, yStat_, and STK::Stat::Bivariate< XTYPE, YTYPE, TContainer1D >::yStat_.

    {
      xStat_ = stat.xStat_;
      yStat_ = stat.yStat_;
      W_     = stat.W_;
      cov_   = stat.cov_;
      ucov_  = stat.ucov_;
      cor_   = stat.cor_;
      ucor_  = stat.ucor_;
      return *this;
    }
template<class TContainer1D >
void STK::Stat::Bivariate< Real, Real, TContainer1D >::setData ( ITContainer1D< Real, TContainer1D > const &  X,
ITContainer1D< Real, TContainer1D > const &  Y 
) [inline]

set a new data sets

Parameters:
Xthe first variable
Ythe second variable

Definition at line 155 of file STK_Stat_BivariateRealReal.h.

References STK::IRecursiveTemplate< TContainer1D >::asLeaf(), STK::Stat::Univariate< TYPE, TContainer1D >::setData(), STK::Stat::Bivariate< XTYPE, YTYPE, TContainer1D >::xStat_, and STK::Stat::Bivariate< XTYPE, YTYPE, TContainer1D >::yStat_.

    {
      X_ = X.asLeaf();
      Y_ = Y.asLeaf();
      W_.clear();
      xStat_.setData(X);
      yStat_.setData(Y);
      compCovariance();
    }

Here is the call graph for this function:

template<class TContainer1D >
void STK::Stat::Bivariate< Real, Real, TContainer1D >::setData ( ITContainer1D< Real, TContainer1D > const &  X,
ITContainer1D< Real, TContainer1D > const &  Y,
TContainer1D const &  W 
) [inline]

set a new data sets

Parameters:
Xthe first variable
Ythe second variable
Wthe weights to apply

Definition at line 172 of file STK_Stat_BivariateRealReal.h.

References STK::IRecursiveTemplate< TContainer1D >::asLeaf(), STK::Stat::Univariate< TYPE, TContainer1D >::setData(), STK::Stat::Bivariate< XTYPE, YTYPE, TContainer1D >::xStat_, and STK::Stat::Bivariate< XTYPE, YTYPE, TContainer1D >::yStat_.

    {
      X_ = X.asLeaf();
      Y_ = Y.asLeaf();
      W_ = W;
      xStat_.setData(X, W);
      yStat_.setData(Y, W);
      compWeightedCovariance();
    }

Here is the call graph for this function:

template<class TContainer1D >
Univariate<Real, TContainer1D> const& STK::Stat::Bivariate< Real, Real, TContainer1D >::xStat ( ) const [inline]

get statistics of the first variable

Returns:
the statistics of X

Definition at line 188 of file STK_Stat_BivariateRealReal.h.

References STK::Stat::Bivariate< XTYPE, YTYPE, TContainer1D >::xStat_.

{return xStat_;}
template<class TContainer1D >
Univariate<Real, TContainer1D> const& STK::Stat::Bivariate< Real, Real, TContainer1D >::yStat ( ) const [inline]

get statistics of the first variable

Returns:
the statistics of Y

Definition at line 192 of file STK_Stat_BivariateRealReal.h.

References STK::Stat::Bivariate< XTYPE, YTYPE, TContainer1D >::yStat_.

{return yStat_;}
template<class TContainer1D >
Real const& STK::Stat::Bivariate< Real, Real, TContainer1D >::covariance ( ) const [inline]

get covariance between the variables

Returns:
the covariance between X and Y

Definition at line 196 of file STK_Stat_BivariateRealReal.h.

{ return cov_;}
template<class TContainer1D >
Real const& STK::Stat::Bivariate< Real, Real, TContainer1D >::correlation ( ) const [inline]

get correlation between the variables

Returns:
the correlation between X and Y

Definition at line 200 of file STK_Stat_BivariateRealReal.h.

{ return cor_;}
template<class TContainer1D >
Real const& STK::Stat::Bivariate< Real, Real, TContainer1D >::unbiasedCovariance ( ) const [inline]

get the unbiased covariance between the variables

Returns:
the unbiased covariance between X and Y

Definition at line 204 of file STK_Stat_BivariateRealReal.h.

{ return ucov_;}
template<class TContainer1D >
Real const& STK::Stat::Bivariate< Real, Real, TContainer1D >::unbiasedCorrelation ( ) const [inline]

get the unbiased correlation between the variables

Returns:
the unbiased correlation between X and Y

Definition at line 208 of file STK_Stat_BivariateRealReal.h.

{ return ucor_;}
template<class TContainer1D >
void STK::Stat::Bivariate< Real, Real, TContainer1D >::compCovariance ( ) [inline, protected]

compute the covariance

Definition at line 235 of file STK_Stat_BivariateRealReal.h.

References STK::Range::first(), STK::Range::inf(), STK::Range::last(), STK::Range::size(), STK::Stat::Bivariate< XTYPE, YTYPE, TContainer1D >::xStat_, and STK::Stat::Bivariate< XTYPE, YTYPE, TContainer1D >::yStat_.

    {
      // get dimensions
      Range com = STK::Range::inf(X_.range(),Y_.range());
      const Integer first = com.first(), last = com.last();
      Integer nobs = com.size();
      // get means
      Real xMean = xStat_.mean(), yMean= yStat_.mean();
      // compute covariance
      Real xsum  = 0.0, ysum = 0.0, xdev, ydev;
      cov_ = 0.;
      for (Integer i=first; i<=last; i++)
      {
        if (Arithmetic<Real>::isFinite(X_[i]) && Arithmetic<Real>::isFinite(Y_[i]))
        {
          xsum += (xdev = X_[i] - xMean); // deviation from the mean
          ysum += (ydev = Y_[i] - yMean); // deviation from the mean
          cov_ += (xdev*ydev);         // squared value
        }
        else nobs--;
      }
      if (nobs > 1)
      {
        ucov_ = (cov_ - (xsum*ysum)/(Real)nobs)/(Real)(nobs -1);
        ucor_ = ucov_/(xStat_.unbiasedStd()*yStat_.unbiasedStd());
        cov_  = (cov_ - (xsum*ysum)/(Real)nobs)/(Real)(nobs);
        cor_  = cov_/(xStat_.std()*yStat_.std());
      }
      else
      {
        if (nobs == 1)
        {
          cov_ = 0.;
          cor_ = 0.;
          ucov_ = Arithmetic<Real>::NA();
          ucor_ = Arithmetic<Real>::NA();
        }
        // no observations
        cov_ = Arithmetic<Real>::NA();
        cor_ = Arithmetic<Real>::NA();
        ucov_ = Arithmetic<Real>::NA();
        ucor_ = Arithmetic<Real>::NA();
      }
    }

Here is the call graph for this function:

template<class TContainer1D >
void STK::Stat::Bivariate< Real, Real, TContainer1D >::compWeightedCovariance ( ) [inline, protected]

compute the weighted covariance

Definition at line 281 of file STK_Stat_BivariateRealReal.h.

References STK::abs(), STK::Range::first(), STK::Range::inf(), STK::Range::last(), STK::Stat::Bivariate< XTYPE, YTYPE, TContainer1D >::xStat_, and STK::Stat::Bivariate< XTYPE, YTYPE, TContainer1D >::yStat_.

    {
      // get dimensions
      Range com = STK::Range::inf(X_.range(),Y_.range());
      Integer first = com.first(), last = com.last();
      // get means
      Real xMean = xStat_.mean(), yMean= yStat_.mean();
      // compute covariance
      Real xsum  = 0.0, ysum = 0.0, xdev, ydev;
      sumWeights_ = 0.0;
      sum2Weights_ = 0.0;
      // sum
      cov_ = 0.0;
      for (Integer i=first; i<=last; i++)
      {
        if (Arithmetic<Real>::isFinite(X_[i]) && Arithmetic<Real>::isFinite(Y_[i]))
        {
          xsum += (xdev = X_[i] - xMean); // deviation from the mean
          ysum += (ydev = Y_[i] - yMean); // deviation from the mean
          Real Wi = abs((Real)W_[i]);
          cov_ += Wi * (xdev*ydev);         // cross product
          sumWeights_  += Wi;           // sum absolute weights
          sum2Weights_ += Wi * Wi;     // sum squared weights
       }
     }
     // not pathological weights
     if (sumWeights_*sumWeights_ > sum2Weights_)
     {
       ucov_ = (cov_ - xsum*ysum/sumWeights_)/(sumWeights_ - sum2Weights_/sumWeights_);
       ucor_ = ucov_/(xStat_.unbiasedStd()*yStat_.unbiasedStd());
       cov_ = (cov_ - xsum*ysum)/(sumWeights_);
       cor_ = cov_/(xStat_.std()*yStat_.std());
     }
     else
     {
       if (sumWeights_) // if sum of the weights is not 0
       {
         cov_ = 0.;
         cor_ = 0.;
         ucov_ = Arithmetic<Real>::NA();
         ucor_ = Arithmetic<Real>::NA();
       }
       // no weigths
       cov_ = Arithmetic<Real>::NA();
       cor_ = Arithmetic<Real>::NA();
       ucov_ = Arithmetic<Real>::NA();
       ucor_ = Arithmetic<Real>::NA();
     }
   }

Here is the call graph for this function:


Member Data Documentation

template<class TContainer1D >
TContainer1D STK::Stat::Bivariate< Real, Real, TContainer1D >::X_ [protected]

reference on the X data set.

Definition at line 212 of file STK_Stat_BivariateRealReal.h.

template<class TContainer1D >
TContainer1D STK::Stat::Bivariate< Real, Real, TContainer1D >::Y_ [protected]

reference on the Y data set.

Definition at line 214 of file STK_Stat_BivariateRealReal.h.

template<class TContainer1D >
TContainer1D STK::Stat::Bivariate< Real, Real, TContainer1D >::W_ [protected]

local copy of the weights

Definition at line 216 of file STK_Stat_BivariateRealReal.h.

Referenced by operator=().

template<class TContainer1D >
Univariate<Real, TContainer1D> STK::Stat::Bivariate< Real, Real, TContainer1D >::xStat_ [protected]

Statistics of the X variable.

Definition at line 218 of file STK_Stat_BivariateRealReal.h.

Referenced by operator=().

template<class TContainer1D >
Univariate<Real, TContainer1D> STK::Stat::Bivariate< Real, Real, TContainer1D >::yStat_ [protected]

Statistics of the Y variable.

Definition at line 220 of file STK_Stat_BivariateRealReal.h.

Referenced by operator=().

template<class TContainer1D >
Real STK::Stat::Bivariate< Real, Real, TContainer1D >::cov_ [protected]

covariance between X and Y

Definition at line 222 of file STK_Stat_BivariateRealReal.h.

Referenced by operator=().

template<class TContainer1D >
Real STK::Stat::Bivariate< Real, Real, TContainer1D >::ucov_ [protected]

unbiased covariance between X and Y

Definition at line 224 of file STK_Stat_BivariateRealReal.h.

Referenced by operator=().

template<class TContainer1D >
Real STK::Stat::Bivariate< Real, Real, TContainer1D >::cor_ [protected]

correlation between X and Y

Definition at line 226 of file STK_Stat_BivariateRealReal.h.

Referenced by operator=().

template<class TContainer1D >
Real STK::Stat::Bivariate< Real, Real, TContainer1D >::ucor_ [protected]

unbiased correlation between X and Y

Definition at line 228 of file STK_Stat_BivariateRealReal.h.

Referenced by operator=().

template<class TContainer1D >
Real STK::Stat::Bivariate< Real, Real, TContainer1D >::sumWeights_ [protected]

Sum of the weights.

Definition at line 230 of file STK_Stat_BivariateRealReal.h.

template<class TContainer1D >
Real STK::Stat::Bivariate< Real, Real, TContainer1D >::sum2Weights_ [protected]

Sum of the square of the weights.

Definition at line 232 of file STK_Stat_BivariateRealReal.h.


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