Mobile API Reference  MicroStrategy 2019
statistic.h
Go to the documentation of this file.
1 //
2 // statistic.h
3 // ReportServiceCore
4 //
5 // Created by He Tengfei on 3/8/12.
6 // Copyright 2012 MicroStrategy, Inc. All rights reserved.
7 //
8 #ifndef _STATISTIC_
9 #define _STATISTIC_
10 
11 #include "DSSFunctionUtility.h"
12 
13 #define PI 3.1415926535897932384626433
14 #define I_PI 0.3183098861837906715377675 // 1/pi
15 #define LOG_SQRT_PI 0.5723649429247000870717135 /* log (sqrt (pi)) */
16 #define I_SQRT_PI 0.5641895835477562869480795 /* 1 / sqrt (pi) */
17 #define I_SQRT_TWO_PI 0.39894228040143 /* 1/sqrt(2*pi) */
18 
19 
20 
21 #ifndef max
22 inline double max(double x, double y)
23 {
24  return (x > y)? x:y;
25 }
26 #endif
27 
28 #ifndef min
29 inline double min(double x, double y)
30 {
31  return (x < y)? x:y;
32 }
33 #endif
34 
36 {
37  HypUnkown = -100,
38  HypLess = -1,
39  HypEqual = 0,
42 };
43 
44 
45 // UMV estimation of the mean of a population
46 double Mean(enum MathError *error, Int32 n, double Vn[]);
47 
48 // UMV estimation of the variance of a population Vn with an unknown mean
49 double Variance(enum MathError *error, Int32 n, double Vn[]);
50 
51 // UMV estimation of standard deviation of a population Vn with an unknown mean
52 double StDev(enum MathError *error, Int32 n, double Vn[]);
53 
54 // UMV estimation of the variance of a population Vn with a known mean
55 double VarianceKM(enum MathError *error, Int32 n, double Vn[], double mean);
56 
57 // UMV estimation of the standard deviation of a population Vn with a known mean
58 double StDevKM(enum MathError *error, Int32 n, double Vn[], double mean);
59 
60 // Average absolute deviation from mean
61 double AvgDev(enum MathError *error, Int32 n, double Vn[]);
62 
63 // UMV estimation of the kurtosis of the population Vn
64 double Kurtosis(enum MathError *error, Int32 n, double Vn[]);
65 
66 // UMV estimation of the skew of the population
67 double Skew(enum MathError *error, Int32 n, double Vn[]);
68 
69 // UMV estimation of the covariance of two populations
70 double Covariance( enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[] );
71 
72 // UMV estimation of the correlation of two populations
73 double Correlation( enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[] );
74 
75 // UMV estimation of the R sqare coefficient of two populations
76 double RSquare( enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[] );
77 
78 // UMV estimation of the covariance of two populations with known means
79 double CovarianceKM( enum MathError *error, Int32 nX, double X[], double meanX, Int32 nY, double Y[], double meanY );
80 
81 // UMV estimation of the correlation of two populations with known means
82 double CorrelationKM( enum MathError *error, Int32 nX, double X[], double meanX, Int32 nY, double Y[], double meanY );
83 
84 // UMV estimation of the R sqare coefficient of two populations with known means
85 double RSquareKM( enum MathError *error, Int32 nX, double X[], double meanX, Int32 nY, double Y[], double meanY );
86 
87 // Tests the hypotesis Hyp on the mean of a population X with a known standard deviation stdevX against the value hypMean
88 double ZTest( enum MathError *error, Int32 nX, double X[], double stdevX, double hypMean, enum Hypothesis Hyp );
89 
90 // Tests the hypotesis Hyp on the mean of a population X with unknown standard deviation against the value hypMean
91 double MeanTTest( enum MathError *error, Int32 nX, double X[], double hypMean, enum Hypothesis Hyp );
92 
93 // Tests the hypotesis Hyp on the mean of two populations with paired samples
94 // The test is performed against a mean difference of offset
95 double PairedTTest( enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[], enum Hypothesis Hyp, double offset=0.0 );
96 
97 // Tests the hypotesis Hyp on the mean of two populations with the same unknown variance
98 // The test is performed against a mean difference of offset
99 double HomoscedasticTTest( enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[], enum Hypothesis Hyp, double offset=0.0 );
100 
101 // Tests the hypotesis Hyp on the mean of two populations with different unknown variances
102 // The test is performed against a mean difference of offset
103 double HeteroscedasticTTest( enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[], enum Hypothesis Hyp, double offset=0.0 );
104 
105 // Tests the hypotesis Hyp on the variance of a population X against the value hypVar
106 double VarTest( enum MathError *error, Int32 nX, double X[], double hypVar, enum Hypothesis Hyp );
107 
108 // Tests the hypotesis on the ratio of the variances of two populations
109 double FTest( enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[], enum Hypothesis Hyp, double ratio=1.0);
110 
111 // tests the hypotesis that X follows the distribution Y
112 double ChiSqTest( enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[] );
113 
114 // slope of the regression line
115 double Slope(enum MathError *error, Int32 nY, double Y[], Int32 nX, double X[]);
116 
117 // intercept of the regression line
118 double Intercept(enum MathError *error, Int32 nY, double Y[], Int32 nX, double X[]);
119 
120 double SteYX(enum MathError *error, Int32 nY, double Y[], Int32 nX, double X[]);
121 
122 
123 
124 double ALOGAM(enum MathError *error, double x);
125 double BETAIN(enum MathError *error, double x, double a, double b, double beta);
126 double BETADIST(enum MathError *error, double x, double alpha, double beta, double LB=0.0, double UB=1.0);
127 double BETAINVERSE(enum MathError *error, double p, double alpha, double beta, double LB=0.0, double UB=1.0);
128 double BETAINV(enum MathError *error, double p, double q, double alpha);
129 double BINOMDIST(enum MathError *error, Int32 x, Int32 n, double p, bool fCum=true);
130 double CRITBINOM(enum MathError *error, Int32 n, double p, double f);
131 double FDIST(enum MathError *error, double p, Int32 df1, Int32 df2);
132 double FINV(enum MathError *error, double f, Int32 df1, Int32 df2);
133 double COMBIN(enum MathError *error, Int32 x, Int32 n);
134 double CHIDIST(enum MathError *error, double x, Int32 df);
135 double CHIINV(enum MathError *error, double p, Int32 df);
136 double NORMSDIST(enum MathError *error, double z); //accurate up to six digits
137 double NORMSINV(enum MathError *error, double f);
138 double NORMDIST(enum MathError *error, double x, double mean, double standard_dev, bool fCum=true);
139 double NORMINV(enum MathError *error, double p, double mean, double standard_dev);
140 double EXPONDIST(enum MathError *error, double x, double lambda, bool fCum=true);
141 double GAMMADIST(enum MathError *error, double x, double alpha, double beta, bool fCum=true);
142 double GAMMAINV(enum MathError *error, double p, double alpha, double beta);
143 double HYPGEOMDIST(enum MathError *error, Int32 x, Int32 n, Int32 M, Int32 N);
144 double LOGNORMDIST(enum MathError *error, double x, double mean, double standard_dev);
145 double LOGINV(enum MathError *error, double p, double mean, double standard_dev);
146 double NEGBINOMDIST(enum MathError *error, Int32 f, Int32 s, double p);
147 double PERMUT(enum MathError *error, Int32 x, Int32 n);
148 double POISSON(enum MathError *error, Int32 x, double mean, bool fCum=true);
149 double TTAILINV(enum MathError *error, double p, Int32 nu);
150 double TINV(enum MathError *error, double p, Int32 nu);
151 double TTAIL(enum MathError *error, double x, Int32 nu, Int32 tails);
152 double TDIST(enum MathError *error, double x, Int32 nu);
153 double WEIBULL(enum MathError *error, double x, double alpha, double beta, bool fCum=true);
154 double Standardize(enum MathError *error, double x, double mean, double standard_dev);
155 double Confidence(enum MathError *error, double alpha, double standard_dev, Int32 size);
156 double Fisher(enum MathError *error, double x);
157 double FisherInv(enum MathError *error, double x);
158 
159 double oldTDIST(enum MathError *error, double x, Int32 nu);
160 double oldFDIST(enum MathError *error, double p, Int32 df1, Int32 df2);
161 double oldCHIDIST(enum MathError *error, double x, Int32 df);
162 
163 double gammln(enum MathError *error, double xx);
164 
165 // *** New statistic functions ***
166 // Helper functions
167 void LinearRegression (enum MathError * error, double py[], int n, double* m, double* b);
168 void LinearRegression (enum MathError * error, double px[], double py[], int n, double* m, double* b);
169 
170 double LinearTrend_S(enum MathError * error, double x, double py[], int n);
171 double Growth_S(enum MathError * error, double x, double py[], int n);
172 double Forecast_S(enum MathError * error, double x, double px[], double py[], int n);
173 
175 {
179 };
180 
181 
182 class Regression
183 {
184 public:
185 
186  double (*Function)(double);
187  double (*InverseFunction)(double);
189  double *Parameters;
190  double *Variances;
191  double *Pvalues;
192  double Rsquare, Ssquare;
193 
194  Regression(enum MathError *error, Int32 varNum, Int32 obsNum, double Y[], double **X, Int32 function=0);
195  ~Regression();
196 
197  double Predict(enum MathError *error, Int32 nX, double X[]);
198 };
199 
200 #endif // _STATISTIC_
double FisherInv(enum MathError *error, double x)
double min(double x, double y)
Definition: statistic.h:29
Definition: statistic.h:38
double Correlation(enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[])
double TTAIL(enum MathError *error, double x, Int32 nu, Int32 tails)
void LinearRegression(enum MathError *error, double py[], int n, double *m, double *b)
double ZTest(enum MathError *error, Int32 nX, double X[], double stdevX, double hypMean, enum Hypothesis Hyp)
Hypothesis
Definition: ReportServiceCore/Classes/CalculationEngine/statistic.h:35
Definition: statistic.h:176
#define s(x, c)
Definition: aesopt.h:408
double Covariance(enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[])
Definition: statistic.h:177
double max(double x, double y)
Definition: statistic.h:22
double Forecast_S(enum MathError *error, double x, double px[], double py[], int n)
Definition: statistic.h:178
double Kurtosis(enum MathError *error, Int32 n, double Vn[])
double CRITBINOM(enum MathError *error, Int32 n, double p, double f)
double NORMDIST(enum MathError *error, double x, double mean, double standard_dev, bool fCum=true)
double LOGINV(enum MathError *error, double p, double mean, double standard_dev)
double EXPONDIST(enum MathError *error, double x, double lambda, bool fCum=true)
Regression(enum MathError *error, Int32 varNum, Int32 obsNum, double Y[], double **X, Int32 function=0)
double Mean(enum MathError *error, Int32 n, double Vn[])
double * Pvalues
Definition: ReportServiceCore/Classes/CalculationEngine/statistic.h:191
MathError
Definition: DSSFunctionUtility.h:344
double PairedTTest(enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[], enum Hypothesis Hyp, double offset=0.0)
double Ssquare
Definition: ReportServiceCore/Classes/CalculationEngine/statistic.h:192
enum _Function Function
double Fisher(enum MathError *error, double x)
Definition: statistic.h:39
double StDevKM(enum MathError *error, Int32 n, double Vn[], double mean)
double Rsquare
Definition: ReportServiceCore/Classes/CalculationEngine/statistic.h:192
double HomoscedasticTTest(enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[], enum Hypothesis Hyp, double offset=0.0)
double CHIDIST(enum MathError *error, double x, Int32 df)
double Growth_S(enum MathError *error, double x, double py[], int n)
double * Variances
Definition: ReportServiceCore/Classes/CalculationEngine/statistic.h:190
Definition: statistic.h:37
double AvgDev(enum MathError *error, Int32 n, double Vn[])
double Standardize(enum MathError *error, double x, double mean, double standard_dev)
double SteYX(enum MathError *error, Int32 nY, double Y[], Int32 nX, double X[])
double ChiSqTest(enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[])
double oldCHIDIST(enum MathError *error, double x, Int32 df)
double TDIST(enum MathError *error, double x, Int32 nu)
double ALOGAM(enum MathError *error, double x)
double * Parameters
Definition: ReportServiceCore/Classes/CalculationEngine/statistic.h:189
double BETAINVERSE(enum MathError *error, double p, double alpha, double beta, double LB=0.0, double UB=1.0)
#define Int32
Definition: BasicTypes.h:20
double gammln(enum MathError *error, double xx)
double FDIST(enum MathError *error, double p, Int32 df1, Int32 df2)
double oldTDIST(enum MathError *error, double x, Int32 nu)
double GAMMAINV(enum MathError *error, double p, double alpha, double beta)
double BETAINV(enum MathError *error, double p, double q, double alpha)
double RSquareKM(enum MathError *error, Int32 nX, double X[], double meanX, Int32 nY, double Y[], double meanY)
double NORMINV(enum MathError *error, double p, double mean, double standard_dev)
Definition: statistic.h:41
double Predict(enum MathError *error, Int32 nX, double X[])
Int32 ParametersNum
Definition: ReportServiceCore/Classes/CalculationEngine/statistic.h:188
double CHIINV(enum MathError *error, double p, Int32 df)
double TINV(enum MathError *error, double p, Int32 nu)
double FINV(enum MathError *error, double f, Int32 df1, Int32 df2)
double BETADIST(enum MathError *error, double x, double alpha, double beta, double LB=0.0, double UB=1.0)
double PERMUT(enum MathError *error, Int32 x, Int32 n)
double HYPGEOMDIST(enum MathError *error, Int32 x, Int32 n, Int32 M, Int32 N)
double VarianceKM(enum MathError *error, Int32 n, double Vn[], double mean)
double BINOMDIST(enum MathError *error, Int32 x, Int32 n, double p, bool fCum=true)
double Slope(enum MathError *error, Int32 nY, double Y[], Int32 nX, double X[])
double oldFDIST(enum MathError *error, double p, Int32 df1, Int32 df2)
double BETAIN(enum MathError *error, double x, double a, double b, double beta)
double CovarianceKM(enum MathError *error, Int32 nX, double X[], double meanX, Int32 nY, double Y[], double meanY)
double NORMSINV(enum MathError *error, double f)
double WEIBULL(enum MathError *error, double x, double alpha, double beta, bool fCum=true)
double Skew(enum MathError *error, Int32 n, double Vn[])
double COMBIN(enum MathError *error, Int32 x, Int32 n)
double CorrelationKM(enum MathError *error, Int32 nX, double X[], double meanX, Int32 nY, double Y[], double meanY)
double StDev(enum MathError *error, Int32 n, double Vn[])
double(* InverseFunction)(double)
Definition: ReportServiceCore/Classes/CalculationEngine/statistic.h:187
double Confidence(enum MathError *error, double alpha, double standard_dev, Int32 size)
Definition: statistic.h:40
double NORMSDIST(enum MathError *error, double z)
Definition: ReportServiceCore/Classes/CalculationEngine/statistic.h:182
Int32 ObservationsNum
Definition: ReportServiceCore/Classes/CalculationEngine/statistic.h:188
double Variance(enum MathError *error, Int32 n, double Vn[])
double LOGNORMDIST(enum MathError *error, double x, double mean, double standard_dev)
double MeanTTest(enum MathError *error, Int32 nX, double X[], double hypMean, enum Hypothesis Hyp)
double HeteroscedasticTTest(enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[], enum Hypothesis Hyp, double offset=0.0)
double NEGBINOMDIST(enum MathError *error, Int32 f, Int32 s, double p)
double FTest(enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[], enum Hypothesis Hyp, double ratio=1.0)
double POISSON(enum MathError *error, Int32 x, double mean, bool fCum=true)
double Intercept(enum MathError *error, Int32 nY, double Y[], Int32 nX, double X[])
RegressionFunction
Definition: ReportServiceCore/Classes/CalculationEngine/statistic.h:174
double VarTest(enum MathError *error, Int32 nX, double X[], double hypVar, enum Hypothesis Hyp)
double LinearTrend_S(enum MathError *error, double x, double py[], int n)
double TTAILINV(enum MathError *error, double p, Int32 nu)
double RSquare(enum MathError *error, Int32 nX, double X[], Int32 nY, double Y[])
double GAMMADIST(enum MathError *error, double x, double alpha, double beta, bool fCum=true)