STK++ 0.9.13
STK_Stat_Covariance.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::StatistiK::StatDesc
27 * Purpose: Compute elementary statistics for two variables.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
35#ifndef STK_STAT_COVARIANCE_H
36#define STK_STAT_COVARIANCE_H
37
38#include "STK_Stat_Functors.h"
39
40namespace STK
41{
42namespace Stat
43{
44
54template<class XArray, class YArray>
56 , ExprBase<YArray> const& Y
57 , bool unbiased = false
58 )
59{
62 typedef typename hidden::Traits<XArray>::Type XType;
63 typedef typename hidden::Traits<YArray>::Type YType;
65
66#ifdef STK_BOUNDS_CHECK
67 if (X.range() != Y.range())
69#endif
70 Type xMean = mean(X.asDerived()), yMean = mean(Y.asDerived());
71 int nobs = X.size();
72 Type xsum = 0.0, ysum = 0.0, cov = 0.0, xdev, ydev;
73 for (int i=X.begin(); i<X.end(); i++)
74 {
75 xsum += (xdev = X[i] - xMean);
76 ysum += (ydev = Y[i] - yMean);
77 cov += (xdev*ydev);
78 }
79 // compute the covariance with corrected bias
80 return (unbiased) ?
81 (nobs > 1) ? ((cov - (xsum*ysum)/Type(nobs))/Type(nobs -1)) : 0.
82
83 :
84 (nobs > 0) ? (cov - (xsum*ysum)/Type(nobs))/Type(nobs) : 0.;
85}
86
87
97template<class XArray, class YArray>
99 , ExprBase<YArray> const& Y
100 , bool unbiased = false
101 )
102{
105 typedef typename hidden::Traits<XArray>::Type XType;
106 typedef typename hidden::Traits<YArray>::Type YType;
107 typedef typename hidden::Promote<XType, YType>::result_type Type;
108
109 #ifdef STK_BOUNDS_CHECK
110 if (X.range() != Y.range())
112 #endif
113
114 int nobs = X.size();
115 Type xMean = mean(X.asDerived()), yMean = mean(Y.asDerived());
116 Type xsum = 0.0, ysum = 0.0, cov = 0.0, xdev, ydev;
117 for (int i=X.begin(); i<X.end(); i++)
118 {
120 {
121 xsum += (xdev = X[i] - xMean);
122 ysum += (ydev = Y[i] - yMean);
123 cov += (xdev*ydev);
124 }
125 else nobs--;
126 }
127 // compute the covariance with corrected bias
128 return (unbiased) ?
129 (nobs > 1) ? ((cov - (xsum*ysum)/Type(nobs))/Type(nobs -1)) : 0.
130
131 :
132 (nobs > 0) ? (cov - (xsum*ysum)/Type(nobs))/Type(nobs) : 0.;
133}
134
152template<class XArray, class YArray, class Weights>
154 , ExprBase<YArray> const& Y
155 , ExprBase<Weights> const& W
156 , bool unbiased = false
157 )
158{
162 typedef typename hidden::Traits<XArray>::Type XType;
163 typedef typename hidden::Traits<YArray>::Type YType;
164 typedef typename hidden::Promote<XType, YType>::result_type Type;
165
166#ifdef STK_BOUNDS_CHECK
167 if (X.range() != Y.range())
169#endif
170
171 Type xMean = mean(X.asDerived(),W.asDerived()), yMean = mean(Y.asDerived(),W.asDerived());
172 Real xsum = 0.0, ysum = 0.0, xdev, ydev, sumWeights = 0.0, sum2Weights = 0.0, cov = 0.0;
173 for (int i=X.begin(); i<X.end(); i++)
174 {
175 xsum += (xdev = X[i] - xMean);
176 ysum += (ydev = Y[i] - yMean);
177 Real Wi = std::abs((Real)W[i]);
178 cov += Wi * (xdev*ydev);
179 sumWeights += Wi;
180 sum2Weights += Wi * Wi;
181 }
182 // compute the variance
183 return (unbiased) ?
186// (cov - xsum*ysum/sumWeights)/(sumWeights - sum2Weights/sumWeights) : 0.
187 : (sumWeights) ? (cov - xsum*ysum)/(sumWeights) : 0.;
188}
189
207template<class XArray, class YArray, class Weights>
209 , ExprBase<YArray> const& Y
210 , ExprBase<Weights> const& W
211 , bool unbiased = false
212 )
213{
217 typedef typename hidden::Traits<XArray>::Type XType;
218 typedef typename hidden::Traits<YArray>::Type YType;
219 typedef typename hidden::Promote<XType, YType>::result_type Type;
220
221#ifdef STK_BOUNDS_CHECK
222 if (X.range() != Y.range())
224#endif
225
226 Type xMean = mean(X.asDerived(),W.asDerived()), yMean = mean(Y.asDerived(),W.asDerived());
227 // compute covariance
228 Type xsum = 0.0, ysum = 0.0, xdev, ydev, sumWeights = 0.0, sum2Weights = 0.0, cov = 0.0;
229 for (int i=X.begin(); i<X.end(); i++)
230 {
234 )
235 {
236 xsum += (xdev = X[i] - xMean); // deviation from the mean
237 ysum += (ydev = Y[i] - yMean); // deviation from the mean
238 Type Wi = std::abs((Real)W[i]);
239 cov += Wi * (xdev*ydev); // cross product
240 sumWeights += Wi; // sum absolute weights
241 sum2Weights += Wi * Wi; // sum squared weights
242 }
243 }
244 // compute the variance
245 return (unbiased) ?
248// (cov - xsum*ysum/sumWeights)/(sumWeights - sum2Weights/sumWeights) : 0.
249 : (sumWeights) ? (cov - xsum*ysum)/(sumWeights) : 0.;
250}
251
263template<class XArray, class YArray>
265 , ExprBase<YArray> const& Y
266 , typename hidden::Traits<XArray>::Type const& xMu
267 , typename hidden::Traits<YArray>::Type const& yMu
268 , bool unbiased = false
269 )
270{
273 typedef typename hidden::Traits<XArray>::Type XType;
274 typedef typename hidden::Traits<YArray>::Type YType;
275 typedef typename hidden::Promote<XType, YType>::result_type Type;
276
277#ifdef STK_BOUNDS_CHECK
278 if (X.range() != Y.range())
280#endif
281
282 int nobs = X.size();
283 Type xsum = 0.0, ysum = 0.0, cov = 0.0, xdev, ydev;
284 for (int i=X.begin(); i<X.end(); i++)
285 {
286 xsum += (xdev = X[i] - xMu);
287 ysum += (ydev = Y[i] - yMu);
288 cov += (xdev*ydev);
289 }
290 // compute the covariance with corrected bias
291 return (unbiased) ?
292 (nobs > 1) ? ((cov - (xsum*ysum)/Type(nobs))/Type(nobs -1))
294
295 :
296 (nobs > 0) ? (cov - (xsum*ysum)/Type(nobs))/Type(nobs)
298}
299
311template<class XArray, class YArray>
313 , ExprBase<YArray> const& Y
314 , typename hidden::Traits<XArray>::Type const& xMu
315 , typename hidden::Traits<YArray>::Type const& yMu
316 , bool unbiased = false
317 )
318{
321 typedef typename hidden::Traits<XArray>::Type XType;
322 typedef typename hidden::Traits<YArray>::Type YType;
323 typedef typename hidden::Promote<XType, YType>::result_type Type;
324
325 #ifdef STK_BOUNDS_CHECK
326 if (X.range() != Y.range())
328 #endif
329
330 int nobs = X.size();
331 Type xsum = 0.0, ysum = 0.0, cov = 0.0, xdev, ydev;
332 for (int i=X.begin(); i<X.end(); i++)
333 {
335 {
336 xsum += (xdev = X[i] - xMu); // deviation from the mean
337 ysum += (ydev = Y[i] - yMu); // deviation from the mean
338 cov += (xdev*ydev); // squared value
339 }
340 else nobs--;
341 }
342 // compute the covariance with corrected bias
343 return (unbiased) ?
344 (nobs > 1) ? ((cov - (xsum*ysum)/Type(nobs))/Type(nobs -1))
346
347 :
348 (nobs > 0) ? (cov - (xsum*ysum)/Type(nobs))/Type(nobs)
350}
351
370template<class XArray, class YArray, class Weights>
372 , ExprBase<YArray> const& Y
373 , ExprBase<Weights> const& W
374 , typename hidden::Traits<XArray>::Type const& xMean
375 , typename hidden::Traits<YArray>::Type const& yMean
376 , bool unbiased = false
377 )
378{
382 typedef typename hidden::Traits<XArray>::Type XType;
383 typedef typename hidden::Traits<YArray>::Type YType;
384 typedef typename hidden::Promote<XType, YType>::result_type Type;
385
386#ifdef STK_BOUNDS_CHECK
387 if (X.range() != Y.range())
389#endif
390
391 Type xsum = 0.0, ysum = 0.0, xdev, ydev, sumWeights = 0.0, sum2Weights = 0.0, cov = 0.0;
392 for (int i=X.begin(); i<X.end(); i++)
393 {
394 xsum += (xdev = X[i] - xMean);
395 ysum += (ydev = Y[i] - yMean);
396 Type Wi = std::abs((Real)W[i]);
397 cov += Wi * (xdev*ydev);
398 sumWeights += Wi;
399 sum2Weights += Wi * Wi;
400 }
401 // compute the variance
402 return (unbiased) ?
405// (cov - xsum*ysum/sumWeights)/(sumWeights - sum2Weights/sumWeights) : 0.
406 : (sumWeights) ? (cov - xsum*ysum)/(sumWeights) : 0.;
407}
408
427template<class XArray, class YArray, class Weights>
429 , ExprBase<YArray> const& Y
430 , ExprBase<Weights> const& W
431 , typename hidden::Traits<XArray>::Type const& xMu
432 , typename hidden::Traits<YArray>::Type const& yMu
433 , bool unbiased = false
434 )
435{
439 typedef typename hidden::Traits<XArray>::Type XType;
440 typedef typename hidden::Traits<YArray>::Type YType;
441 typedef typename hidden::Promote<XType, YType>::result_type Type;
442
443#ifdef STK_BOUNDS_CHECK
444 if (X.range() != Y.range())
446#endif
447
448 // compute covariance
449 Type xsum = 0.0, ysum = 0.0, xdev, ydev, sumWeights = 0.0, sum2Weights = 0.0, cov = 0.0;
450 for (int i=X.begin(); i<X.end(); i++)
451 {
455 )
456 {
457 xsum += (xdev = X[i] - xMu); // deviation from the mean
458 ysum += (ydev = Y[i] - yMu); // deviation from the mean
459 Type Wi = std::abs((Real)W[i]);
460 cov += Wi * (xdev*ydev); // cross product
461 sumWeights += Wi; // sum absolute weights
462 sum2Weights += Wi * Wi; // sum squared weights
463 }
464 }
465 // compute the variance
466 return (unbiased) ?
469// (cov - xsum*ysum/sumWeights)/(sumWeights - sum2Weights/sumWeights) : 0.
470 : (sumWeights) ? (cov - xsum*ysum)/(sumWeights) : 0.;
471}
472
482template < class Array >
484covariance( ExprBase<Array> const& V, bool unbiased = false)
485{
487 // typename Array::Row mean;
489 // compute the mean
490 mean.move(Stat::mean(V.asDerived()));
491 for (int j= cov_.begin(); j< cov_.end(); j++)
492 {
493 cov_(j, j) = varianceWithFixedMean(V.col(j), mean[j], unbiased);
494 for (int i= cov_.begin(); i<j; i++)
495 { cov_(j,i) = ( cov_(i, j) = covarianceWithFixedMean(V.col(i), V.col(j), mean[i], mean[j], unbiased));}
496 }
497 return cov_;
498}
499
509template < class Array >
512{
515 // compute the mean
516 mean.move(Stat::meanByRow(V.asDerived()));
517 for (int j= cov_.begin(); j< cov_.end(); j++)
518 {
519 cov_(j, j) = varianceWithFixedMean(V.row(j), mean[j], unbiased);
520 for (int i= cov_.begin(); i<j; i++)
521 { cov_(j,i) = ( cov_(i, j) = covarianceWithFixedMean(V.row(i), V.row(j), mean[i], mean[j], unbiased));}
522 }
523 return cov_;
524}
525
535template <class Array, class Weights >
537covariance( ExprBase<Array> const& V, Weights const& W, bool unbiased = false)
538{
541 // compute the mean
542 mean.move(Stat::mean(V.asDerived(), W.asDerived()));
543 for (int j= cov_.begin(); j< cov_.end(); j++)
544 {
545 cov_(j, j) = varianceWithFixedMean(V.col(j), W, mean[j], unbiased);
546 for (int i= cov_.begin(); i<j; i++)
547 { cov_(j,i) = ( cov_(i, j) = covarianceWithFixedMean(V.col(i), V.col(j), W, mean[i], mean[j], unbiased));}
548 }
549 return cov_;
550}
551
561template <class Array, class Weights >
563covarianceByRow( ExprBase<Array> const& V, Weights const& W, bool unbiased = false)
564{
567 mean.move(Stat::meanByRow(V.asDerived(), W.asDerived()));
568 for (int j= cov_.begin(); j< cov_.end(); j++)
569 {
570 cov_(j, j) = varianceWithFixedMean(V.row(j), W, mean[j], unbiased);
571 for (int i= cov_.begin(); i<j; i++)
572 { cov_(j,i) = ( cov_(i, j) = covarianceWithFixedMean(V.row(i), V.row(j), W, mean[i], mean[j], unbiased));}
573 }
574 return cov_;
575}
576
587template < class Array, class Mean >
590{
591#ifdef STK_BOUNDS_CHECK
592 if (V.cols()!=mean.range()) STKRUNTIME_ERROR_NO_ARG(covarianceWithFixedMean,V.cols()!=mean.range());
593#endif
595 for (int j= cov_.begin(); j< cov_.end(); j++)
596 {
597 cov_(j, j) = varianceWithFixedMean(V.col(j), mean[j], unbiased);
598 for (int i= cov_.begin(); i<j; i++)
599 { cov_(j,i) = ( cov_(i, j) = covarianceWithFixedMean(V.col(i), V.col(j), mean[i], mean[j], unbiased));}
600 }
601 return cov_;
602}
603
614template < class Array, class Mean >
617{
618#ifdef STK_BOUNDS_CHECK
619 if (V.rows()!=mean.range()) STKRUNTIME_ERROR_NO_ARG(covarianceWithFixedMean,V.rows()!=mean.range());
620#endif
622 for (int j= cov_.begin(); j< cov_.end(); j++)
623 {
624 cov_(j, j) = varianceWithFixedMean(V.row(j), mean[j], unbiased);
625 for (int i= cov_.begin(); i<j; i++)
626 { cov_(j,i) = ( cov_(i, j) = covarianceWithFixedMean(V.row(i), V.row(j), mean[i], mean[j], unbiased));}
627 }
628 return cov_;
629}
630
641template <class Array, class Weights, class Mean >
644{
645#ifdef STK_BOUNDS_CHECK
646 if (V.cols()!=mean.range()) STKRUNTIME_ERROR_NO_ARG(covarianceWithFixedMean,V.cols()!=mean.range());
647 if (W.range()!=mean.range()) STKRUNTIME_ERROR_NO_ARG(covarianceWithFixedMean,W.range()!=mean.range());
648#endif
649
651 for (int j= cov_.begin(); j< cov_.end(); j++)
652 {
653 cov_(j, j) = varianceWithFixedMean(V.col(j), W, mean[j], unbiased);
654 for (int i= cov_.begin(); i<j; i++)
655 { cov_(j,i) = ( cov_(i, j) = covarianceWithFixedMean(V.col(i), V.col(j), W, mean[i], mean[j], unbiased));}
656 }
657 return cov_;
658}
659
670template <class Array, class Weights, class Mean >
673{
674#ifdef STK_BOUNDS_CHECK
675 if (V.rows()!=mean.range()) STKRUNTIME_ERROR_NO_ARG(covarianceWithFixedMean,V.cols()!=mean.range());
676 if (W.range()!=mean.range()) STKRUNTIME_ERROR_NO_ARG(covarianceWithFixedMean,W.range()!=mean.range());
677#endif
678
680 for (int j= cov_.begin(); j< cov_.end(); j++)
681 {
682 cov_(j, j) = varianceWithFixedMean(V.row(j), W, mean[j], unbiased);
683 for (int i= cov_.begin(); i<j; i++)
684 { cov_(j,i) = ( cov_(i, j) = covarianceWithFixedMean(V.row(i), V.row(j), W, mean[i], mean[j], unbiased));}
685 }
686 return cov_;
687}
688
689} // namespace Stat
690
691} // namespace STK
692
693#endif /*STK_STAT_COVARIANCE_H */
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
This file contain the functors computings statistics.
#define STK_STATIC_ASSERT_ONE_DIMENSION_ONLY(EXPR)
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
double Real
STK fundamental type of Real values.
CArraySquare< typename Array::Type, Array::sizeRows_, Array::orient_ > covarianceByRow(ExprBase< Array > const &V, bool unbiased=false)
Compute the covariance matrix using the rows of the data set V.
hidden::FunctorTraits< Derived, VarianceWithFixedMeanOp >::Row varianceWithFixedMean(Derived const &A, MeanType const &mean, bool unbiased)
Compute the VarianceWithFixedMean(s) value(s) of A.
Real covarianceSafe(ExprBase< XArray > const &X, ExprBase< YArray > const &Y, bool unbiased=false)
Compute the covariance of the variables X and Y.
CArraySquare< typename Array::Type, Array::sizeRows_, Array::orient_ > covarianceWithFixedMeanByRow(ExprBase< Array > const &V, ExprBase< Mean > const &mean, bool unbiased=false)
Compute the covariance matrix using the rows of the data set V.
hidden::FunctorTraits< Derived, MeanOp >::Row mean(Derived const &A)
Compute the mean(s) value(s) of A.
Real covariance(ExprBase< XArray > const &X, ExprBase< YArray > const &Y, bool unbiased=false)
Compute the covariance of the variables X and Y.
Real covarianceWithFixedMean(ExprBase< XArray > const &X, ExprBase< YArray > const &Y, typename hidden::Traits< XArray >::Type const &xMu, typename hidden::Traits< YArray >::Type const &yMu, bool unbiased=false)
Compute the covariance of the variables X and Y with fixed means.
Real covarianceWithFixedMeanSafe(ExprBase< XArray > const &X, ExprBase< YArray > const &Y, typename hidden::Traits< XArray >::Type const &xMu, typename hidden::Traits< YArray >::Type const &yMu, bool unbiased=false)
Compute the covariance of the variables X and Y with fixed means.
hidden::FunctorTraits< Derived, MeanOp >::Col meanByRow(Derived const &A)
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
static bool isFinite(Type const &x)
If<(sizeof(Type1)>sizeof(Type2)), Type1, Type2 >::Result result_type