00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, 2009, Andrew W. Steiner 00005 00006 This file is part of O2scl. 00007 00008 O2scl is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 O2scl is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with O2scl. If not, see <http://www.gnu.org/licenses/>. 00020 00021 ------------------------------------------------------------------- 00022 */ 00023 #ifndef O2SCL_VEC_STATS_H 00024 #define O2SCL_VEC_STATS_H 00025 00026 /** \file vec_stats.h 00027 \brief File containing statistics template functions 00028 00029 \future Add chi squared test? 00030 \future Consider generalizing to other data types 00031 */ 00032 00033 #include <iostream> 00034 #include <cmath> 00035 #include <string> 00036 #include <fstream> 00037 #include <sstream> 00038 #include <o2scl/err_hnd.h> 00039 #include <gsl/gsl_ieee_utils.h> 00040 #include <gsl/gsl_sort.h> 00041 00042 #ifndef DOXYGENP 00043 namespace o2scl { 00044 #endif 00045 00046 /** 00047 \brief Compute the mean of the first \c n elements of a vector 00048 00049 If \c n is zero, this will return zero. 00050 */ 00051 template<class vec_t> 00052 double vector_mean(size_t n, vec_t &data) { 00053 00054 long double mean=0.0; 00055 for(size_t i=0;i<n;i++) { 00056 mean+=(data[i]-mean)/(i+1); 00057 } 00058 return mean; 00059 } 00060 00061 /** \brief Compute variance with specified mean known in advance 00062 00063 This function computes 00064 \f[ 00065 \frac{1}{N} \sum_{i} \left( x_i - \mu \right)^2 00066 \f] 00067 where the value of \f$ \mu \f$ is given in \c mean. 00068 */ 00069 template<class vec_t> 00070 double vector_variance_fmean(size_t n, vec_t &data, 00071 double mean) { 00072 long double var=0.0; 00073 for(size_t i=0;i<n;i++) { 00074 long double delta=(data[i]-mean); 00075 var+=(delta*delta-var)/(i+1); 00076 } 00077 return var; 00078 } 00079 00080 /** \brief Compute the variance with specified mean 00081 00082 This function computes 00083 \f[ 00084 \frac{1}{N-1} \sum_{i} \left( x_i - \mu \right)^2 00085 \f] 00086 where the value of \f$ \mu \f$ is given in \c mean. 00087 00088 If \c n is 0 or 1, this function will call the error 00089 handler. 00090 */ 00091 template<class vec_t> 00092 double vector_variance(size_t n, vec_t &data, double mean) { 00093 00094 if (n<2) { 00095 O2SCL_ERR2_RET("Cannot compute variance with less than 2 elements", 00096 " in vector_variance().",gsl_einval); 00097 } 00098 00099 double var=vector_variance_fmean<vec_t>(n,data,mean); 00100 return var*n/(n-1); 00101 } 00102 00103 /** \brief Compute the variance 00104 00105 This function computes 00106 \f[ 00107 \frac{1}{N-1} \sum_{i} \left( x_i - \mu \right)n^2 00108 \f] 00109 where \f$ \mu \f$ is the mean computed with \ref vector_mean(). 00110 00111 If \c n is 0 or 1, this function will call the error 00112 handler. 00113 */ 00114 template<class vec_t> 00115 double vector_variance(size_t n, vec_t &data) { 00116 00117 if (n<2) { 00118 O2SCL_ERR2_RET("Cannot compute variance with less than 2 elements", 00119 " in vector_variance().",gsl_einval); 00120 } 00121 00122 double mean=vector_mean<vec_t>(n,data); 00123 double var=vector_variance_fmean<vec_t>(n,data,mean); 00124 return var*n/(n-1); 00125 } 00126 00127 /** \brief Standard deviation with specified mean known in advance 00128 00129 This function computes 00130 \f[ 00131 \sqrt{\frac{1}{N} \sum_{i} \left( x_i - \mu \right)^2} 00132 \f] 00133 where the value of \f$ \mu \f$ is given in \c mean. 00134 00135 If \c n is zero, this function will return zero 00136 without calling the error handler. 00137 */ 00138 template<class vec_t> 00139 double vector_stddev_fmean(size_t n, vec_t &data, 00140 double mean) { 00141 double sd=vector_variance_fmean<vec_t>(n,data,mean); 00142 return std::sqrt(sd); 00143 } 00144 00145 /** \brief Standard deviation with specified mean 00146 00147 This function computes 00148 \f[ 00149 \sqrt{\frac{1}{N-1} \sum_{i} \left( x_i - \mu \right)^2} 00150 \f] 00151 where \f$ \mu \f$ is the mean computed with \ref vector_mean(). 00152 00153 If \c n is 0 or 1, this function will call the error 00154 handler. 00155 */ 00156 template<class vec_t> 00157 double vector_stddev(size_t n, vec_t &data) { 00158 00159 if (n<2) { 00160 O2SCL_ERR2_RET("Cannot compute std. dev. with less than 2 elements", 00161 " in vector_stddev().",gsl_einval); 00162 } 00163 00164 double mean=vector_mean<vec_t>(n,data); 00165 double var=vector_variance_fmean<vec_t>(n,data,mean); 00166 return std::sqrt(var*n/(n-1)); 00167 } 00168 00169 /** \brief Standard deviation with specified mean 00170 00171 This function computes 00172 \f[ 00173 \sqrt{\frac{1}{N-1} \sum_{i} \left( x_i - \mu \right)^2} 00174 \f] 00175 where the value of \f$ \mu \f$ is given in \c mean. 00176 00177 If \c n is 0 or 1, this function will call the error 00178 handler. 00179 */ 00180 template<class vec_t> 00181 double vector_stddev(size_t n, vec_t &data, 00182 double mean) { 00183 00184 if (n<2) { 00185 O2SCL_ERR2_RET("Cannot compute std. dev. with less than 2 elements", 00186 " in vector_stddev().",gsl_einval); 00187 } 00188 00189 double sd=vector_variance_fmean<vec_t>(n,data,mean); 00190 return std::sqrt(sd*n/(n-1)); 00191 } 00192 00193 /** \brief Absolute deviation from the specified mean 00194 00195 This function computes 00196 \f[ 00197 \sum_i | x_i - \mu | 00198 \f] 00199 where the value of \f$ \mu \f$ is given in \c mean. 00200 00201 If \c n is zero, this function will return zero 00202 without calling the error handler. 00203 */ 00204 template<class vec_t> 00205 double vector_absdev(size_t n, vec_t &data, 00206 double mean) { 00207 00208 if (n==0) return 0.0; 00209 00210 long double sum=0.0; 00211 for(size_t i=0;i<n;i++) { 00212 sum+=fabs(data[i]-mean); 00213 } 00214 return sum/n; 00215 } 00216 00217 /** \brief Absolute deviation from the computed mean 00218 00219 This function computes 00220 \f[ 00221 \sum_i \left| x_i - \frac{1}{N} \left(\sum_j x_j\right) \right| 00222 \f] 00223 where the value of \f$ \mu \f$ is given in \c mean. 00224 00225 If \c n is zero, this function will return zero 00226 without calling the error handler. 00227 */ 00228 template<class vec_t> 00229 double vector_absdev(size_t n, vec_t &data) { 00230 double mean=vector_mean<vec_t>(n,data); 00231 return vector_absdev(n,data,mean); 00232 } 00233 00234 /** \brief Skewness with specified mean and standard deviation 00235 00236 This function computes 00237 \f[ 00238 \frac{1}{N} \sum_i \left[ 00239 \frac{ \left(x_i - \mu \right)}{ \sigma }\right]^3 00240 \f] 00241 where the values of \f$ \mu \f$ and \f$ \sigma \f$ 00242 are given in \c mean and \c stddev. 00243 00244 If \c n is zero, this function will return zero 00245 without calling the error handler. 00246 */ 00247 template<class vec_t> 00248 double vector_skew(size_t n, vec_t &data, double mean, 00249 double stddev) { 00250 long double skew=0.0; 00251 for(size_t i=0;i<n;i++) { 00252 long double x=(data[i]-mean)/stddev; 00253 skew+=(x*x*x-skew)/(i+1); 00254 } 00255 return skew; 00256 } 00257 00258 /** \brief Skewness with computed mean and standard deviation 00259 00260 This function computes 00261 \f[ 00262 \frac{1}{N} \sum_i \left[ 00263 \frac{ \left(x_i - \mu \right)}{ \sigma }\right]^3 00264 \f] 00265 where the values of \f$ \mu \f$ and \f$ \sigma \f$ 00266 are computed using \ref vector_mean() and \ref vector_stddev(). 00267 00268 If \c n is zero, this function will return zero 00269 without calling the error handler. 00270 */ 00271 template<class vec_t> 00272 double vector_skew(size_t n, vec_t &data) { 00273 double mean=vector_mean<vec_t>(n,data); 00274 double sd=vector_stddev<vec_t>(n,data,mean); 00275 return vector_skew(n,data,mean,sd); 00276 } 00277 00278 /** \brief Kurtosis with specified mean and standard deviation 00279 00280 This function computes 00281 \f[ 00282 -3 + \frac{1}{N} \sum_i \left[ 00283 \frac{ \left(x_i - \mu \right)}{ \sigma }\right]^4 00284 \f] 00285 where the values of \f$ \mu \f$ and \f$ \sigma \f$ 00286 are given in \c mean and \c stddev. 00287 00288 If \c n is zero, this function will return zero 00289 without calling the error handler. 00290 */ 00291 template<class vec_t> 00292 double vector_kurtosis(size_t n, vec_t &data, double mean, 00293 double stddev) { 00294 long double avg=0.0; 00295 for(size_t i=0;i<n;i++) { 00296 long double x=(data[i]-mean)/stddev; 00297 avg+=(x*x*x*x-avg)/(i+1); 00298 } 00299 return avg-3.0; 00300 } 00301 00302 /** \brief Kurtosis with computed mean and standard deviation 00303 00304 This function computes 00305 \f[ 00306 -3 + \frac{1}{N} \sum_i \left[ 00307 \frac{ \left(x_i - \mu \right)}{ \sigma }\right]^4 00308 \f] 00309 where the values of \f$ \mu \f$ and \f$ \sigma \f$ 00310 are computed using \ref vector_mean() and \ref vector_stddev(). 00311 00312 If \c n is zero, this function will return zero 00313 without calling the error handler. 00314 */ 00315 template<class vec_t> 00316 double vector_kurtosis(size_t n, vec_t &data) { 00317 double mean=vector_mean<vec_t>(n,data); 00318 double sd=vector_stddev<vec_t>(n,data,mean); 00319 return vector_kurtosis(n,data,mean,sd); 00320 } 00321 00322 /** \brief Lag-1 autocorrelation 00323 00324 This function computes 00325 \f[ 00326 \left[ 00327 \sum_i \left(x_i - \mu\right) \left(x_{i-1} - \mu \right) 00328 \right] \left[ 00329 \sum_i \left(x_i - \mu\right)^2 00330 \right]^{-1} 00331 \f] 00332 00333 If \c n is zero, this function will call the error handler. 00334 */ 00335 template<class vec_t> 00336 double vector_lag1_autocorr(size_t n, vec_t &data, double mean) { 00337 00338 if (n<1) { 00339 O2SCL_ERR2_RET("Cannot compute lag1 with no elements", 00340 " in vector_lag1_autocorr().",gsl_einval); 00341 } 00342 00343 long double q=0.0; 00344 long double v=(data[0]-mean)*(data[0]-mean); 00345 for(size_t i=1;i<n;i++) { 00346 long double delta0=data[i-1]-mean; 00347 long double delta1=data[i]-mean; 00348 q+=(delta0*delta1-q)/(i+1); 00349 v+=(delta1*delta1-v)/(i+1); 00350 } 00351 return q/v; 00352 } 00353 00354 /** \brief Lag-1 autocorrelation 00355 00356 This function computes 00357 \f[ 00358 \left[ 00359 \sum_i \left(x_i - \mu\right) \left(x_{i-1} - \mu \right) 00360 \right] \left[ 00361 \sum_i \left(x_i - \mu\right)^2 00362 \right]^{-1} 00363 \f] 00364 00365 If \c n is zero, this function will call the error handler. 00366 */ 00367 template<class vec_t> 00368 double vector_lag1_autocorr(size_t n, vec_t &data) { 00369 double mean=vector_mean<vec_t>(n,data); 00370 return vector_lag1_autocorr(n,data,mean); 00371 } 00372 00373 /** \brief Covariance 00374 00375 This function computes 00376 \f[ 00377 \frac{1}{n-1} \sum_i \left(x_i - {\bar{x}}\right) 00378 \left(y_i - {\bar{y}}\right) 00379 \f] 00380 where \f$ {\bar{x}} \f$ and \f$ {\bar{y}} \f$ are specified 00381 in \c mean1 and \c mean2, respectively. 00382 00383 If \c n is zero, this function will return zero 00384 without calling the error handler. 00385 */ 00386 template<class vec_t> 00387 double vector_covariance(size_t n, vec_t &data1, vec_t &data2, 00388 double mean1, double mean2) { 00389 double covar=0.0; 00390 for(size_t i=0;i<n;i++) { 00391 double delta1=(data1[i]-mean1); 00392 double delta2=(data2[i]-mean2); 00393 covar+=(delta1*delta2-covar)/(i+1); 00394 } 00395 return covar; 00396 } 00397 00398 /** \brief Covariance 00399 00400 This function computes 00401 \f[ 00402 \frac{1}{n-1} \sum_i \left(x_i - {\bar{x}}\right) 00403 \left(y_i - {\bar{y}}\right) 00404 \f] 00405 where \f$ {\bar{x}} \f$ and \f$ {\bar{y}} \f$ are 00406 the averages of \c data1 and \c data2 and are computed 00407 automatically. 00408 00409 If \c n is zero, this function will return zero 00410 without calling the error handler. 00411 */ 00412 template<class vec_t> 00413 double vector_covariance(size_t n, vec_t &data1, vec_t &data2) { 00414 double covar=0.0; 00415 double mean1=vector_mean<vec_t>(n,data1); 00416 double mean2=vector_mean<vec_t>(n,data2); 00417 for(size_t i=0;i<n;i++) { 00418 long double delta1=(data1[i]-mean1); 00419 long double delta2=(data2[i]-mean2); 00420 covar+=(delta1*delta2-covar)/(i+1); 00421 } 00422 return covar; 00423 } 00424 00425 /** \brief Pearson's correlation 00426 00427 \comment 00428 r = cov(x, y) / (\Hat\sigma_x \Hat\sigma_y) 00429 = {1/(n-1) \sum (x_i - \Hat x) (y_i - \Hat y) 00430 \over 00431 \sqrt{1/(n-1) \sum (x_i - \Hat x)^2} \sqrt{1/(n-1) 00432 \sum (y_i - \Hat y)^2} 00433 } 00434 \endcomment 00435 00436 If \c n is zero, this function will call the error handler. 00437 */ 00438 template<class vec_t> 00439 double vector_correlation(size_t n, vec_t &data1, vec_t &data2) { 00440 size_t i; 00441 00442 if (n<1) { 00443 O2SCL_ERR2_RET("Cannot compute correlation with no elements", 00444 " in vector_correlation().",gsl_einval); 00445 } 00446 00447 double sum_xsq = 0.0; 00448 double sum_ysq = 0.0; 00449 double sum_cross = 0.0; 00450 double ratio; 00451 double delta_x, delta_y; 00452 double mean_x, mean_y; 00453 double r; 00454 00455 /* 00456 * Compute: 00457 * sum_xsq = Sum [ (x_i - mu_x)^2 ], 00458 * sum_ysq = Sum [ (y_i - mu_y)^2 ] and 00459 * sum_cross = Sum [ (x_i - mu_x) * (y_i - mu_y) ] 00460 * using the above relation from Welford's paper 00461 */ 00462 00463 mean_x = data1[0]; 00464 mean_y = data2[0]; 00465 00466 for (i = 1; i < n; ++i) { 00467 ratio = i / (i + 1.0); 00468 delta_x = data1[i] - mean_x; 00469 delta_y = data2[i] - mean_y; 00470 sum_xsq += delta_x * delta_x * ratio; 00471 sum_ysq += delta_y * delta_y * ratio; 00472 sum_cross += delta_x * delta_y * ratio; 00473 mean_x += delta_x / (i + 1.0); 00474 mean_y += delta_y / (i + 1.0); 00475 } 00476 00477 r = sum_cross / (std::sqrt(sum_xsq) * std::sqrt(sum_ysq)); 00478 00479 return r; 00480 } 00481 00482 /** \brief Pooled variance 00483 00484 \todo Document this 00485 00486 If \c n is zero, this function will return zero 00487 without calling the error handler. 00488 */ 00489 template<class vec_t> 00490 double vector_pvariance(size_t n1, vec_t &data1, 00491 size_t n2, vec_t &data2) { 00492 double var1=vector_variance<vec_t>(n1,data1); 00493 double var2=vector_variance<vec_t>(n2,data2); 00494 return (((n1-1)*var1)+((n2-1)*var2))/(n1+n2-2); 00495 } 00496 00497 /** \brief Quantile 00498 00499 \todo Document this 00500 00501 If \c n is zero, this function will return zero 00502 without calling the error handler. 00503 */ 00504 template<class vec_t> 00505 double vector_quantile_sorted(size_t n, vec_t &data, 00506 const double f) { 00507 00508 double index=f*(n-1); 00509 size_t lhs=((size_t)index); 00510 double delta=index-lhs; 00511 if (n==0) return 0.0; 00512 if (lhs==n-1) return data[lhs]; 00513 return (1-delta)*data[lhs]+delta*data[lhs+1]; 00514 } 00515 00516 /** \brief Median of sorted data 00517 00518 \todo Document this 00519 00520 If \c n is zero, this function will return zero 00521 without calling the error handler. 00522 */ 00523 template<class vec_t> 00524 double vector_median_sorted(size_t n, vec_t &data) { 00525 00526 if (n==0) return 0.0; 00527 00528 size_t lhs=(n-1)/2; 00529 size_t rhs=n/2; 00530 00531 if (lhs==rhs) return data[lhs]; 00532 00533 return (data[lhs]+data[rhs])/2.0; 00534 } 00535 00536 /** \brief Compute the mean of weighted data 00537 00538 \comment 00539 M(n) = M(n-1) + (data[n] - M(n-1)) (w(n)/(W(n-1) + w(n))) 00540 W(n) = W(n-1) + w(n) 00541 \endcomment 00542 */ 00543 template<class vec_t> 00544 double wvector_mean(size_t n, vec_t &data, vec_t &weights) { 00545 00546 long double wmean=0.0; 00547 long double W=0.0; 00548 for(size_t i=0;i<n;i++) { 00549 double wi=weights[i]; 00550 if (wi>0.0) { 00551 W+=wi; 00552 wmean+=(data[i]-wmean)*(wi/W); 00553 } 00554 } 00555 00556 return wmean; 00557 } 00558 00559 /** \brief Compute a factor for weighted data 00560 00561 This function is used in \ref wvector_variance(size_t n, 00562 vec_t &data, vec2_t &weights, double wmean) and \ref 00563 wvector_stddev(size_t n, vec_t &data, vec2_t &weights, double 00564 wmean) . 00565 */ 00566 template<class vec_t> 00567 double wvector_factor(size_t n, vec_t &weights) { 00568 00569 long double a=0.0; 00570 long double b=0.0; 00571 long double factor; 00572 for(size_t i=0;i<n;i++) { 00573 double wi=weights[i]; 00574 if (wi>0.0) { 00575 a+=wi; 00576 b+=wi*wi; 00577 } 00578 } 00579 factor=a*a/(a*a-b); 00580 return factor; 00581 } 00582 00583 /** \brief Compute the variance of a weighted vector 00584 with a mean known in advance 00585 */ 00586 template<class vec_t, class vec2_t> 00587 double wvector_variance_fmean(size_t n, vec_t &data, 00588 vec_t &weights, double wmean) { 00589 long double wvariance=0.0; 00590 long double W=0.0; 00591 for(size_t i=0;i<n;i++) { 00592 double wi=weights[i]; 00593 if (wi>0.0) { 00594 const long double delta=data[i]-wmean; 00595 W+=wi; 00596 wvariance+=(delta*delta-wvariance)*(wi/W); 00597 } 00598 } 00599 00600 return wvariance; 00601 } 00602 00603 /** \brief Compute the variance of a weighted vector where mean 00604 is computed automatically 00605 */ 00606 template<class vec_t, class vec2_t> 00607 double wvector_variance(size_t n, vec_t &data, 00608 vec2_t &weights) { 00609 00610 double wmean=wvector_mean(n,data,weights); 00611 return wvector_variance_fmean(n,data,weights,wmean); 00612 } 00613 00614 /** \brief Compute the variance of a weighted vector with 00615 specified mean 00616 */ 00617 template<class vec_t, class vec2_t> 00618 double wvector_variance(size_t n, vec_t &data, 00619 vec2_t &weights, double wmean) { 00620 00621 const double variance=wvector_variance_fmean 00622 (n,data,weights,wmean); 00623 const double scale=wvector_factor(n,weights); 00624 const double wvar=scale*variance; 00625 return wvar; 00626 } 00627 00628 /** \brief Compute the standard deviation of a weighted vector 00629 with a mean known in advance 00630 */ 00631 template<class vec_t, class vec2_t> 00632 double wvector_stddev_fmean(size_t n, vec_t &data, 00633 vec2_t &weights, double wmean) { 00634 return sqrt(wvector_variance_fmean(n,data,weights,wmean)); 00635 } 00636 00637 /** \brief Compute the standard deviation of a weighted vector where mean 00638 is computed automatically 00639 */ 00640 template<class vec_t, class vec2_t> 00641 double wvector_stddev(size_t n, vec_t &data, 00642 vec2_t &weights) { 00643 double wmean=wvector_mean(n,data,weights); 00644 return sqrt(wvector_variance_fmean(n,data,weights,wmean)); 00645 } 00646 00647 /** \brief Compute the standard deviation of a weighted vector with 00648 specified mean 00649 */ 00650 template<class vec_t, class vec2_t> 00651 double wvector_stddev(size_t n, vec_t &data, 00652 vec2_t &weights, double wmean) { 00653 const double variance=wvector_variance_fmean 00654 (n,data,weights,wmean); 00655 const double scale=wvector_factor(n,weights); 00656 const double wvar=scale*variance; 00657 return sqrt(wvar); 00658 } 00659 00660 /// Desc 00661 template<class vec_t, class vec2_t> 00662 double wvector_sumsq_fmean(size_t n, vec_t &data, 00663 vec2_t &weights, double wmean) { 00664 long double wtss=0.0; 00665 for(size_t i=0;i<n;i++) { 00666 double wi=weights[i]; 00667 if (wi>0.0) { 00668 const long double delta=data[i]-wmean; 00669 wtss+=wi*delta*delta; 00670 } 00671 } 00672 00673 return wtss; 00674 } 00675 00676 /// Desc 00677 template<class vec_t, class vec2_t> 00678 double wvector_sumsq(size_t n, vec_t &data, 00679 vec2_t &weights) { 00680 00681 double wmean=wvector_mean(n,data,weights); 00682 return wvector_sumsq(n,data,weights,wmean); 00683 } 00684 00685 /// Desc 00686 template<class vec_t, class vec2_t> 00687 double wvector_absdev(size_t n, vec_t &data, vec2_t &weights, 00688 double wmean) { 00689 long double wabsdev=0.0; 00690 long double W=0.0; 00691 for(size_t i=0;i<n;i++) { 00692 double wi=weights[i]; 00693 if (wi>0.0) { 00694 const long double delta=fabs(data[i]-wmean); 00695 W+=wi; 00696 wabsdev+=(delta-wabsdev)*(wi/W); 00697 } 00698 } 00699 return wabsdev; 00700 } 00701 00702 /// Desc 00703 template<class vec_t, class vec2_t> 00704 double wvector_skew(size_t n, vec_t &data, vec2_t &weights, 00705 double wmean, double wsd) { 00706 long double wskew=0.0; 00707 long double W=0.0; 00708 for(size_t i=0;i<n;i++) { 00709 double wi=weights[i]; 00710 if (wi>0.0) { 00711 const long double x=(data[i]-wmean)/wsd; 00712 W+=wi; 00713 wskew+=(x*x*x-wskew)*(wi/W); 00714 } 00715 } 00716 return wskew; 00717 } 00718 00719 /// Desc 00720 template<class vec_t, class vec2_t> 00721 double wvector_skew(size_t n, vec_t &data, vec2_t &weights) { 00722 double wmean=wvector_mean(n,data,weights); 00723 double wsd=wvector_stddev(n,data,weights,wmean); 00724 return wvector_skew(n,data,weights,wmean,wsd); 00725 } 00726 00727 /// Desc 00728 template<class vec_t, class vec2_t> 00729 double wvector_kurtosis(size_t n, vec_t &data, vec2_t &weights, 00730 double wmean, double wsd) { 00731 long double wavg=0.0; 00732 long double W=0.0; 00733 for(size_t i=0;i<n;i++) { 00734 double wi=weights[i]; 00735 if (wi>0.0) { 00736 const long double x=(data[i]-wmean)/wsd; 00737 W+=wi; 00738 wavg+=(x*x*x*x-wavg)*(wi/W); 00739 } 00740 } 00741 return wavg-3.0; 00742 } 00743 00744 /// Desc 00745 template<class vec_t, class vec2_t> 00746 double wvector_kurtosis(size_t n, vec_t &data, vec2_t &weights) { 00747 double wmean=wvector_mean(n,data,weights); 00748 double wsd=wvector_stddev(n,data,weights,wmean); 00749 return wvector_kurtosis(n,data,weights,wmean,wsd); 00750 } 00751 00752 #ifdef O2SCL_NEVER_DEFINED 00753 00754 // These need to be redone similar to the GSL functions 00755 00756 /// Weighted mean 00757 template<class vec_t, class vec2_t> 00758 double wvector_mean(size_t n, vec_t &data, vec_t &stddev) { 00759 double sum1=0.0, err=0.0; 00760 for(size_t i=0;i<n;i++) { 00761 sum1+=data[i]/stddev[i]/stddev[i]; 00762 err+=1.0/stddev[i]/stddev[i]; 00763 } 00764 return sum1/err; 00765 } 00766 00767 /// Weighted mean with error 00768 template<class vec_t, class vec2_t> 00769 int wvector_mean_err(size_t n, vec_t &data, vec_t &stddev, 00770 double &mean, double &err) { 00771 mean=0.0; 00772 err=0.0; 00773 for(size_t i=0;i<n;i++) { 00774 mean+=data[i]/stddev[i]/stddev[i]; 00775 err+=1.0/stddev[i]/stddev[i]; 00776 } 00777 mean=mean/err; 00778 err=sqrt(1.0/err); 00779 return 0; 00780 } 00781 00782 #endif 00783 00784 #ifndef DOXYGENP 00785 } 00786 #endif 00787 00788 #endif
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page