/* -*- C++ -*- */ /************************************************************************* * Copyright(c) 1995~2005 Masaharu Goto (root-cint@cern.ch) * * For the licensing terms see the file COPYING * ************************************************************************/ //*** Class Sample to perform statistics on various multi-dim. data samples //*** NVE 30-mar-1996 CERN Geneva // A data sample can be filled using the "enter" and/or "remove" functions, // whereas the "reset" function resets the complete sample to 'empty'. // The info which can be extracted from a certain data sample are the // sum, mean, variance, sigma, covariance and correlation. // The "print" function provides all statistics data for a certain sample. // The variables for which these stat. parameters have to be calculated // are indicated by the index of the variable which is passed as an // argument to the various member functions. // The index convention for a data point (x,y) is : x=1 y=2 // // Example : // --------- // For a Sample s a data point (x,y) can be entered as s.enter(x,y) and // the mean_x can be obtained as s.mean(1) whereas the mean_y is obtained // via s.mean(2). The correlation between x and y is available via s.cor(1,2). // The x-statistics are obtained via s.print(1), y-statistics via s.print(2), // and the covariance and correlation between x and y via s.print(1,2). // All statistics of a sample are obtained via s.print(). // #ifdef __hpux #include #include #else #include #include using namespace std; #endif class Sample { public: Sample(); void reset(); void enter(float x); void remove(float x); void enter(float x, float y); void remove(float x, float y); void enter(float x, float y, float z); void remove(float x, float y, float z); int dim(); int n(); float sum(int i); float mean(int i); float var(int i); float sigma(int i); float cov(int i, int j); float cor(int i, int j); void print(); void print(int i); void print(int i, int j); private: int the_dim; // Dimension of the sample int the_n; // Number of entries of the sample enum {maxdim=3}; // Maximum supported dimension char the_names[maxdim]; // Variable names i.e. X,Y,Z float the_sum[maxdim]; // Total sum for each variable float the_sum2[maxdim][maxdim]; // Total sum**2 for each variable void calc(); float the_mean[maxdim]; // Mean for each variable float the_var[maxdim]; // Variation for each variable float the_sigma[maxdim]; // Standard deviation for each variable float the_cov[maxdim][maxdim]; // Covariances of pairs of variables float the_cor[maxdim][maxdim]; // Correlations of pairs of variables }; Sample::Sample() { //*** Creation of a Sample object and resetting the statistics values //*** The dimension is initialised to maximum the_dim=maxdim; the_names[0]='X'; the_names[1]='Y'; the_names[2]='Z'; the_n=0; for (int i=0; i 0.) the_cor[i][j]=the_cov[i][j]/test; } } } int Sample::dim() { //*** Provide the dimension of a certain sample return the_dim; } int Sample::n() { //*** Provide the number of entries of a certain sample return the_n; } float Sample::sum(int i) { //*** Provide the sum of a certain variable if (the_dim < i) { cout << " *Sample::sum* Error : Dimension less than " << i << endl; return the_sum[0]; } else { return the_sum[i-1]; } } float Sample::mean(int i) { //*** Provide the mean of a certain variable if (the_dim < i) { cout << " *Sample::mean* Error : Dimension less than " << i << endl; return the_mean[0]; } else { return the_mean[i-1]; } } float Sample::var(int i) { //*** Provide the variance of a certain variable if (the_dim < i) { cout << " *Sample::var* Error : Dimension less than " << i << endl; return the_var[0]; } else { return the_var[i-1]; } } float Sample::sigma(int i) { //*** Provide the standard deviation of a certain variable if (the_dim < i) { cout << " *Sample::sigma* Error : Dimension less than " << i << endl; return the_sigma[0]; } else { return the_sigma[i-1]; } } float Sample::cov(int i, int j) { //*** Provide the covariance between variables i and j if ((the_dim < i) || (the_dim < j)) { int k=i; if (j > i) k=j; cout << " *Sample::cov* Error : Dimension less than " << k << endl; return the_cov[0][0]; } else { return the_cov[i-1][j-1]; } } float Sample::cor(int i, int j) { //*** Provide the correlation between variables i and j if ((the_dim < i) || (the_dim < j)) { int k=i; if (j > i) k=j; cout << " *Sample::cor* Error : Dimension less than " << k << endl; return the_cor[0][0]; } else { return the_cor[i-1][j-1]; } } void Sample::print() { //*** Printing of statistics of all variables for (int i=0; i