The OpenD Programming Language

1 /++
2 This module contains algorithms for the $(LINK2 https://en.wikipedia.org/wiki/Beta_distribution, Beta Proportion Distribution).
3 
4 An alternate parameterization of the $(MREF mir,stat,distribution,beta) distribuion in terms of 
5 the mean of the distribution and the sum of its shape parameters (also known as the sample
6 size of the Beta distribution). 
7 
8 License: $(HTTP www.apache.org/licenses/LICENSE-2.0, Apache-2.0)
9 
10 Authors: John Michael Hall
11 
12 Copyright: 2022-3 Mir Stat Authors.
13 
14 Macros:
15 DISTREF = $(REF_ALTTEXT $(TT $2), $2, mir, stat, distribution, $1)$(NBSP)
16 
17 +/
18 
19 module mir.stat.distribution.beta_proportion;
20 
21 import mir.internal.utility: isFloatingPoint;
22 
23 /++
24 Computes the beta proportion probability density function (PDF).
25 
26 Params:
27     x = value to evaluate PDF
28     mu = shape parameter #1
29     kappa = shape parameter #2
30 
31 See_also:
32     $(LINK2 https://en.wikipedia.org/wiki/Beta_distribution, Beta Proportion Distribution),
33     $(DISTREF beta, betaPDF)
34 +/
35 @safe pure nothrow @nogc
36 T betaProportionPDF(T)(const T x, const T mu, const T kappa)
37     if (isFloatingPoint!T)
38     in (x >= 0, "x must be greater than or equal to 0")
39     in (x <= 1, "x must be less than or equal to 1")
40     in (mu > 0, "mu must be greater than zero")
41     in (mu < 1, "mu must be less than one")
42     in (kappa > 0, "kappa must be greater than zero")
43 {
44     import mir.stat.distribution.beta: betaPDF;
45 
46     immutable T alpha = mu * kappa;
47     immutable T beta = kappa - alpha;
48     return betaPDF(x, alpha, beta);
49 }
50 
51 ///
52 version(mir_stat_test)
53 @safe pure nothrow @nogc
54 unittest {
55     import mir.math.common: approxEqual;
56 
57     assert(0.5.betaProportionPDF(0.5, 2) == 1);
58     assert(0.75.betaProportionPDF((1.0 / 3), 3).approxEqual(0.5));
59     assert(0.25.betaProportionPDF((1.0 / 9), 4.5).approxEqual(0.9228516));
60 }
61 
62 /++
63 Computes the beta proportion cumulatve distribution function (CDF).
64 
65 Params:
66     x = value to evaluate CDF
67     mu = shape parameter #1
68     kappa = shape parameter #2
69 
70 See_also:
71     $(LINK2 https://en.wikipedia.org/wiki/Beta_distribution, Beta Proportion Distribution),
72     $(DISTREF beta, betaCDF)
73 +/
74 @safe pure nothrow @nogc
75 T betaProportionCDF(T)(const T x, const T mu, const T kappa)
76     if (isFloatingPoint!T)
77     in (x >= 0, "x must be greater than or equal to 0")
78     in (x <= 1, "x must be less than or equal to 1")
79     in (mu > 0, "mu must be greater than zero")
80     in (mu < 1, "mu must be less than one")
81     in (kappa > 0, "kappa must be greater than zero")
82 {
83     import mir.stat.distribution.beta: betaCDF;
84 
85     immutable T alpha = mu * kappa;
86     immutable T beta = kappa - alpha;
87     return betaCDF(x, alpha, beta);
88 }
89 
90 ///
91 version(mir_stat_test)
92 @safe pure nothrow @nogc
93 unittest {
94     import mir.math.common: approxEqual;
95 
96     assert(0.5.betaProportionCDF(0.5, 2).approxEqual(0.5));
97     assert(0.75.betaProportionCDF((1.0 / 3), 3).approxEqual(0.9375));
98     assert(0.25.betaProportionCDF((1.0 / 9), 4.5).approxEqual(0.8588867));
99 }
100 
101 /++
102 Computes the beta proportion complementary cumulative distribution function (CCDF).
103 
104 Params:
105     x = value to evaluate CCDF
106     mu = shape parameter #1
107     kappa = shape parameter #2
108 
109 See_also:
110     $(LINK2 https://en.wikipedia.org/wiki/Beta_distribution, Beta Proportion Distribution),
111     $(DISTREF beta, betaCDF)
112 +/
113 @safe pure nothrow @nogc
114 T betaProportionCCDF(T)(const T x, const T mu, const T kappa)
115     if (isFloatingPoint!T)
116     in (x >= 0, "x must be greater than or equal to 0")
117     in (x <= 1, "x must be less than or equal to 1")
118     in (mu > 0, "mu must be greater than zero")
119     in (mu < 1, "mu must be less than one")
120     in (kappa > 0, "kappa must be greater than zero")
121 {
122     import mir.stat.distribution.beta: betaCCDF;
123 
124     immutable T alpha = mu * kappa;
125     immutable T beta = kappa - alpha;
126     return betaCCDF(x, alpha, beta);
127 }
128 
129 ///
130 version(mir_stat_test)
131 @safe pure nothrow @nogc
132 unittest {
133     import mir.math.common: approxEqual;
134 
135     assert(0.5.betaProportionCCDF(0.5, 2).approxEqual(0.5));
136     assert(0.75.betaProportionCCDF((1.0 / 3), 3).approxEqual(0.0625));
137     assert(0.25.betaProportionCCDF((1.0 / 9), 4.5).approxEqual(0.1411133));
138 }
139 
140 /++
141 Computes the beta proportion inverse cumulative distribution function (InvCDF).
142 
143 Params:
144     p = value to evaluate InvCDF
145     mu = shape parameter #1
146     kappa = shape parameter #2
147 
148 See_also:
149     $(LINK2 https://en.wikipedia.org/wiki/Beta_distribution, Beta Proportion Distribution),
150     $(DISTREF beta, betaInvCDF)
151 +/
152 @safe pure nothrow @nogc
153 T betaProportionInvCDF(T)(const T p, const T mu, const T kappa)
154     if (isFloatingPoint!T)
155     in (p >= 0, "p must be greater than or equal to 0")
156     in (p <= 1, "p must be less than or equal to 1")
157     in (mu > 0, "mu must be greater than zero")
158     in (mu < 1, "mu must be less than one")
159     in (kappa > 0, "kappa must be greater than zero")
160 {
161     import mir.stat.distribution.beta: betaInvCDF;
162 
163     immutable T alpha = mu * kappa;
164     immutable T beta = kappa - alpha;
165     return betaInvCDF(p, alpha, beta);
166 }
167 
168 ///
169 version(mir_stat_test)
170 @safe pure nothrow @nogc
171 unittest {
172     import mir.math.common: approxEqual;
173 
174     assert(0.5.betaProportionInvCDF(0.5, 2).approxEqual(0.5));
175     assert(0.9375.betaProportionInvCDF((1.0 / 3), 3).approxEqual(0.75));
176     assert(0.8588867.betaProportionInvCDF((1.0 / 9), 4.5).approxEqual(0.25));
177 }
178 
179 /++
180 Computes the beta proportion log probability density function (LPDF).
181 
182 Params:
183     x = value to evaluate LPDF
184     mu = shape parameter #1
185     kappa = shape parameter #2
186 
187 See_also:
188     $(LINK2 https://en.wikipedia.org/wiki/Beta_distribution, Beta Proportion Distribution),
189     $(DISTREF beta, betaLPDF)
190 +/
191 @safe pure nothrow @nogc
192 T betaProportionLPDF(T)(const T x, const T mu, const T kappa)
193     if (isFloatingPoint!T)
194     in (x >= 0, "x must be greater than or equal to 0")
195     in (x <= 1, "x must be less than or equal to 1")
196     in (mu > 0, "mu must be greater than zero")
197     in (mu < 1, "mu must be less than one")
198     in (kappa > 0, "kappa must be greater than zero")
199 {
200     import mir.stat.distribution.beta: betaLPDF;
201 
202     immutable T alpha = mu * kappa;
203     immutable T beta = kappa - alpha;
204     return betaLPDF(x, alpha, beta);
205 }
206 
207 ///
208 version(mir_stat_test)
209 @safe pure nothrow @nogc
210 unittest {
211     import mir.math.common: approxEqual, log;
212 
213     assert(0.5.betaProportionLPDF(0.5, 2).approxEqual(log(betaProportionPDF(0.5, 0.5, 2))));
214     assert(0.75.betaProportionLPDF((1.0 / 3), 3).approxEqual(log(betaProportionPDF(0.75, (1.0 / 3), 3))));
215     assert(0.25.betaProportionLPDF((1.0 / 9), 4.5).approxEqual(log(betaProportionPDF(0.25, (1.0 / 9), 4.5))));
216 }