Honeycomb  0.1
Component-Model Framework
StudentT.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
7 
8 namespace honey
9 {
10 
12 
28 template<class Real>
29 class StudentT_ : public RandomDist<Real>
30 {
31  typedef RandomDist<Real> Super;
32  RandomDist_imports();
34  typedef Beta_<Double> Beta;
35  typedef BetaInc<Double> BetaInc;
36  typedef ChiSqr_<Double> ChiSqr;
37  typedef Random_<Real> Random;
38 
39 public:
40  typedef typename Random::DistStats DistStats;
41  typedef Vec<2,Real> Vec2;
42 
43  StudentT_(optional<RandomGen&> gen, Real n) : Super(gen), n(n) { assert(n > 0); }
45 
46  virtual Real next() const;
47  virtual Real pdf(Real x) const;
48  virtual Real cdf(Real x) const;
49  virtual Real cdfComp(Real x) const;
50  virtual Real cdfInv(Real P) const;
51  virtual Real mean() const { return 0; }
52  virtual Real variance() const { return n / (n - 2); }
53 
54  struct Stats
55  {
56  DistStats dist;
57  Vec2 meanCi;
58  Vec2 stdDevCi;
59  szt df;
61  int tail;
62  Real t;
63  Real p;
64 
65  friend ostream& operator<<(ostream& os, const Stats& val)
66  {
67  int ci = 100*(1-val.alpha);
68  return os << stringstream::indentInc << "{" << endl
69  << "Dist:" << endl << val.dist << endl
70  << "Mean CI " << std::setw(2) << ci
71  << "%: " << val.meanCi << endl
72  << "Std Dev CI " << std::setw(2) << ci
73  << "%: " << val.stdDevCi << endl
74  << "DF: " << val.df << endl
75  << "t Value: " << val.t << endl
76  << (val.tail == 0 ? "Pr > |t|: " :
77  val.tail == 1 ? "Pr > t: " :
78  "Pr < t: ") << val.p
79  << stringstream::indentDec << endl << "}";
80  }
81  };
82 
84 
97  template<class Range>
98  static bool test(const Range& samples, optional<Stats&> stats = optnull, Real mu = 0, Real alpha = 0.05, int tail = 0)
99  {
100  assert(tail >= -1 && tail <= 1);
101  DistStats d = Random::stats(samples);
102  sdt df = d.n - 1;
103  StudentT_ student(df);
104  Real t = (d.mean - mu) / d.stdErr;
105  Real p = student.cdfComp(tail == 0 ? Alge::abs(t) : tail == 1 ? t : -t);
106  if (tail == 0) p *= 2;
107 
108  if (stats)
109  {
110  stats->dist = d;
111  Real meanCi = student.cdfInv(1 - (tail == 0 ? alpha/2 : alpha)) * d.stdErr;
112  stats->meanCi = Vec2( tail == -1 ? -Real_::inf : d.mean - meanCi,
113  tail == 1 ? Real_::inf : d.mean + meanCi);
114  stats->stdDevCi = ChiSqr(df).stdDevCi(d.stdDev, alpha).template cast<Vec2>();
115  stats->df = df;
116  stats->alpha = alpha;
117  stats->tail = tail;
118  stats->t = t;
119  stats->p = p;
120  }
121 
122  return p <= alpha;
123  }
124 
125  struct PooledStats
126  {
127  DistStats dist[2];
131  Vec2 meanCi;
132  Vec2 stdDevCi;
133  szt df;
135  int tail;
136  Real t;
137  Real p;
138 
139  friend ostream& operator<<(ostream& os, const PooledStats& val)
140  {
141  int ci = 100*(1-val.alpha);
142  return os << stringstream::indentInc << "{" << endl
143  << "Dist 1:" << endl << val.dist[0] << endl
144  << "Dist 2:" << endl << val.dist[1] << endl
145  << "Pooled Diff (1 - 2):" << endl
146  << stringstream::indentInc << "{" << endl
147  << "Mean: " << val.mean << endl
148  << "Std Dev: " << val.stdDev << endl
149  << "Std Err: " << val.stdErr << endl
150  << "Mean CI " << std::setw(2) << ci
151  << "%: " << val.meanCi << endl
152  << "Std Dev CI " << std::setw(2) << ci
153  << "%: " << val.stdDevCi << endl
154  << "DF: " << val.df << endl
155  << "t Value: " << val.t << endl
156  << (val.tail == 0 ? "Pr > |t|: " :
157  val.tail == 1 ? "Pr > t: " :
158  "Pr < t: ") << val.p
159  << stringstream::indentDec << endl << "}"
160  << stringstream::indentDec << endl << "}";
161  }
162  };
163 
165 
181  template<class Range, class Range2>
182  static typename std::enable_if<mt::isRange<Range2>::value, bool>::type
183  test(const Range& samples1, const Range2& samples2, optional<PooledStats&> stats = optnull, Real mu = 0, Real alpha = 0.05, int tail = 0)
184  {
185  assert(tail >= -1 && tail <= 1);
186  DistStats d1 = Random::stats(samples1);
187  DistStats d2 = Random::stats(samples2);
188 
189  sdt df = d1.n + d2.n - 2;
190  StudentT_ student(df);
191  Real mean = d1.mean - d2.mean;
192  Real stdDev = Alge::sqrt(((d1.n-1)*Alge::sqr(d1.stdDev) + (d2.n-1)*Alge::sqr(d2.stdDev)) / df);
193  Real stdErr = stdDev * Alge::sqrt((1/Real(d1.n)) + (1/Real(d2.n)));
194  Real t = (mean - mu) / stdErr;
195  Real p = student.cdfComp(tail == 0 ? Alge::abs(t) : tail == 1 ? t : -t);
196  if (tail == 0) p *= 2;
197 
198  if (stats)
199  {
200  stats->dist[0] = d1;
201  stats->dist[1] = d2;
202  stats->mean = mean;
203  stats->stdDev = stdDev;
204  stats->stdErr = stdErr;
205  Real meanCi = student.cdfInv(1 - (tail == 0 ? alpha/2 : alpha)) * stdErr;
206  stats->meanCi = Vec2( tail == -1 ? -Real_::inf : mean - meanCi,
207  tail == 1 ? Real_::inf : mean + meanCi);
208  stats->stdDevCi = ChiSqr(df).stdDevCi(stdDev, alpha).template cast<Vec2>();
209  stats->df = df;
210  stats->alpha = alpha;
211  stats->tail = tail;
212  stats->t = t;
213  stats->p = p;
214  }
215 
216  return p <= alpha;
217  }
218 
220 };
221 
225 
226 extern template class StudentT_<Float>;
227 extern template class StudentT_<Double>;
228 
229 }
Real t
T-test statistic.
Definition: StudentT.h:62
int tail
Whether test is two-tailed or lower/upper tailed.
Definition: StudentT.h:61
Generate a random variate from a beta distribution .
Definition: Beta.h:30
virtual Real cdfInv(Real P) const
Inverse of the CDF.
Definition: StudentT.cpp:60
friend ostream & operator<<(ostream &os, const PooledStats &val)
Definition: StudentT.h:139
Real t
T-test statistic.
Definition: StudentT.h:136
virtual Real mean() const
Calc mean.
Definition: StudentT.h:51
Vec2 meanCi
Lower and upper 100*(1-alpha)% confidence interval of the pooled mean.
Definition: StudentT.h:131
Real alpha
Test significance level.
Definition: StudentT.h:60
int tail
Whether test is two-tailed or lower/upper tailed.
Definition: StudentT.h:135
Vec< 2, Real > Vec2
Definition: StudentT.h:41
static optnull_t optnull
Null optional, use to reset an optional to an uninitialized state or test for initialization.
Definition: Optional.h:12
Real mean
Pooled mean (1 - 2)
Definition: StudentT.h:128
Real p
Probability of observing value more extreme than t.
Definition: StudentT.h:63
ptrdiff_t sdt
Size difference type, shorthand for ptrdiff_t.
Definition: Core.h:92
Real stdErr
Pooled standard error.
Definition: StudentT.h:130
Generate a random variate from a noncentral chi-square distribution.
Definition: ChiSqr.h:52
friend ostream & operator<<(ostream &os, const Stats &val)
Definition: StudentT.h:65
static Real sqrt(Real x)
Square Root.
Definition: Alge.h:63
StudentT_< Double > StudentT_d
Definition: StudentT.h:224
DistStats dist[2]
Sample distribution stats.
Definition: StudentT.h:127
StudentT_(Real n)
Definition: StudentT.h:44
Vec2 stdDevCi
Lower and upper 100*(1-alpha)% confidence interval of the standard deviation.
Definition: StudentT.h:58
DistStats dist
Sample distribution stats.
Definition: StudentT.h:56
Generate a random variate from a Student's t-distribution.
Definition: StudentT.h:29
virtual Real cdf(Real x) const
Cumulative Distribution Function.
Definition: StudentT.cpp:28
virtual Real variance() const
Calc variance.
Definition: StudentT.h:52
static Int abs(Int x)
Get absolute value of signed integer.
Definition: Alge.h:21
StudentT_< Real > StudentT
Definition: StudentT.h:222
static bool test(const Range &samples, optional< Stats & > stats=optnull, Real mu=0, Real alpha=0.05, int tail=0)
One-sample t-test: Test the null hypothesis that the samples are from a normally distributed populati...
Definition: StudentT.h:98
Real stdDev
Sample standard deviation.
Definition: Random.h:134
szt df
Degrees of freedom.
Definition: StudentT.h:133
ostream & indentInc(ostream &os)
Increase stream indent level by 1.
Definition: Stream.h:32
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
StudentT_< Float > StudentT_f
Definition: StudentT.h:223
ostream & indentDec(ostream &os)
Decrease stream indent level by 1.
Definition: Stream.h:34
Definition: Random.h:128
szt df
Degrees of freedom.
Definition: StudentT.h:59
szt n
Sample size.
Definition: Random.h:130
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
Enables any type to be optional so it can exist in an uninitialized null state.
Definition: Optional.h:52
Real stdErr
Standard error of the mean (ie. standard deviation of the sample-mean estimate of the population mean...
Definition: Random.h:135
static DistStats stats(const Range &samples)
Calculate distribution statistics.
Definition: Random.h:152
static Num sqr(Num x)
Square.
Definition: Alge.h:60
Real stdDev
Pooled standard deviation.
Definition: StudentT.h:129
Real n
Definition: StudentT.h:219
Real mean
Sample mean.
Definition: Random.h:131
Random-related methods.
Definition: Random.h:21
virtual Real next() const
Get next randomly distributed variate. Requires a random generator (see ctor or setGen()) ...
Definition: StudentT.cpp:10
virtual Real pdf(Real x) const
Probability Density Function.
Definition: StudentT.cpp:16
Real p
Probability of observing value more extreme than t.
Definition: StudentT.h:137
Real alpha
Test significance level.
Definition: StudentT.h:134
Real stdDev() const
Calc standard deviation.
Definition: Dist.h:81
Vec2 stdDevCi
Lower and upper 100*(1-alpha)% confidence interval of the pooled standard deviation.
Definition: StudentT.h:132
Vec2 meanCi
Lower and upper 100*(1-alpha)% confidence interval of the mean.
Definition: StudentT.h:57
Generate a normally (Gaussian) distributed random variate.
Definition: Gaussian.h:26
Definition: StudentT.h:125
static std::enable_if< mt::isRange< Range2 >::value, bool >::type test(const Range &samples1, const Range2 &samples2, optional< PooledStats & > stats=optnull, Real mu=0, Real alpha=0.05, int tail=0)
Two-sample t-test: Test the null hypothesis that the difference between two sample distributions is a...
Definition: StudentT.h:183
ostream & endl(ostream &os)
End line and apply any indentation to the next line.
Definition: Stream.h:40
Global Honeycomb namespace.
virtual Real cdfComp(Real x) const
Complement of the CDF.
Definition: StudentT.cpp:54
Random::DistStats DistStats
Definition: StudentT.h:40
StudentT_(optional< RandomGen & > gen, Real n)
Definition: StudentT.h:43
Definition: StudentT.h:54
Base class for all random distributions.
Definition: Dist.h:15