/
exponential.gleam
337 lines (323 loc) · 9.66 KB
/
exponential.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
////<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.15.6/dist/katex.min.css" integrity="sha384-ZPe7yZ91iWxYumsBEOn7ieg8q/o+qh/hQpSaPow8T6BwALcXSCS6C6fSRPIAnTQs" crossorigin="anonymous">
////<script defer src="https://cdn.jsdelivr.net/npm/katex@0.15.6/dist/katex.min.js" integrity="sha384-ljao5I1l+8KYFXG7LNEA7DyaFvuvSCmedUf6Y6JI7LJqiu8q5dEivP2nDdFH31V4" crossorigin="anonymous"></script>
////<script defer src="https://cdn.jsdelivr.net/npm/katex@0.15.6/dist/contrib/auto-render.min.js" integrity="sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR" crossorigin="anonymous"></script>
////<script>
//// document.addEventListener("DOMContentLoaded", function() {
//// renderMathInElement(document.body, {
//// // customised options
//// // • auto-render specific keys, e.g.:
//// delimiters: [
//// {left: '$$', right: '$$', display: false},
//// // {left: '$', right: '$', display: false},
//// // {left: '\\(', right: '\\)', display: false},
//// {left: '\\[', right: '\\]', display: true}
//// ],
//// // • rendering keys, e.g.:
//// throwOnError : false
//// });
//// });
////</script>
////<style>
//// .katex { font-size: 1.1em; }
////</style>
////
//// Functions related to continuous exponential random variables.
////
//// ---
////
//// * **Available functions**
//// * [`exponential_mean`](#exponential_mean)
//// * [`exponential_variance`](#exponential_variance)
//// * [`exponential_pdf`](#exponential_pdf)
//// * [`exponential_cdf`](#exponential_cdf)
//// * [`exponential_random`](#exponential_random)
import gleam/iterator.{Iterator}
import gleam_stats/math
if erlang {
import gleam/pair
import gleam/list
import gleam_stats/distributions/uniform
}
fn check_exponential_parameters(lambda: Float) {
case lambda >. 0.0 {
False ->
"Invalid input argument: lambda <= 0. Valid input is lambda > 0."
|> Error
True ->
True
|> Ok
}
}
/// <div style="text-align: right;">
/// <a href="https://github.com/nicklasxyz/gleam_stats/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// Analytically compute the mean of a continuous exponential random variable
/// with given rate parameter $$\lambda \in \(0, +\infty)$$.
///
/// The mean returned is: $$\frac{1}{\lambda}$$.
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top ↑</small>
/// </a>
/// </div>
///
pub fn exponential_mean(lambda: Float) -> Result(Float, String) {
case check_exponential_parameters(lambda) {
Error(string) ->
string
|> Error
_ ->
1.0 /. lambda
|> Ok
}
}
/// <div style="text-align: right;">
/// <a href="https://github.com/nicklasxyz/gleam_stats/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// Analytically compute the variance of a continuous exponential random variable
/// with given rate parameter $$\lambda \in \(0, +\infty)$$.
///
/// The variance returned is: $$\frac{1}{\lambda^{2}}$$.
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top ↑</small>
/// </a>
/// </div>
///
pub fn exponential_variance(lambda: Float) -> Result(Float, String) {
case check_exponential_parameters(lambda) {
Error(string) ->
string
|> Error
_ -> {
assert Ok(v) = math.pow(lambda, 2.0)
1.0 /. v
|> Ok
}
}
}
/// <div style="text-align: right;">
/// <a href="https://github.com/nicklasxyz/gleam_stats/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// Evaluate, at a certain point $$x \in \(-\infty, +\infty\)$$, the probability density function (pdf)
/// of a continuous exponential random variable with given rate parameter $$\lambda \in \(0, +\infty)$$.
///
/// The pdf is defined as:
///
/// \\[
/// f(x; \lambda) =
/// \begin{cases}
/// \lambda \cdot e^{-\lambda \cdot x} &\text{if } x \geq 0, \\\\
/// 0 &\text{if } x < 0.
/// \end{cases}
/// \\]
///
/// <details>
/// <summary>Example:</summary>
///
/// import gleam_stats/distributions/exponential
/// import gleeunit/should
///
/// pub fn example() {
/// let lambda: Float = 1.
/// // For illustrational purposes, evaluate the pdf at the
/// // point -100.0
/// exponential.exponential_pdf(-100.0, lambda)
/// |> should.equal(Ok(0.0))
/// }
/// </details>
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top ↑</small>
/// </a>
/// </div>
///
pub fn exponential_pdf(x: Float, lambda: Float) -> Result(Float, String) {
do_exponential_pdf(x, lambda)
}
if erlang {
fn do_exponential_pdf(x: Float, lambda: Float) -> Result(Float, String) {
case check_exponential_parameters(lambda) {
Error(string) ->
string
|> Error
_ ->
case x >=. 0.0 {
True ->
lambda *. math.exp(-1.0 *. lambda *. x)
|> Ok
False ->
0.0
|> Ok
}
}
}
}
if javascript {
external fn do_exponential_pdf(Float, Float) -> Result(Float, String) =
"../../exponential.mjs" "exponential_pdf"
}
/// <div style="text-align: right;">
/// <a href="https://github.com/nicklasxyz/gleam_stats/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// Evaluate, at a certain point $$x \in \(-\infty, +\infty\)$$, the cumulative distribution
/// function (cdf) of a continuous exponential random variable with given rate parameter
/// $$\lambda \in \(0, +\infty)$$.
///
/// The cdf is defined as:
///
/// \\[
/// F(x; \lambda) =
/// \begin{cases}
/// 1 - e^{-\lambda \cdot x} &\text{if } x \geq 0, \\\\
/// 0 &\text{if } x < 0.
/// \end{cases}
/// \\]
///
/// <details>
/// <summary>Example:</summary>
///
/// import gleam_stats/distributions/exponential
/// import gleeunit/should
///
/// pub fn example() {
/// let lambda: Float = 1.
/// // For illustrational purposes, evaluate the cdf at the
/// // point -100.0
/// exponential.exponential_cdf(-100.0, lambda)
/// |> should.equal(Ok(0.0))
/// }
/// </details>
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top ↑</small>
/// </a>
/// </div>
///
pub fn exponential_cdf(x: Float, lambda: Float) -> Result(Float, String) {
do_exponential_cdf(x, lambda)
}
if erlang {
fn do_exponential_cdf(x: Float, lambda: Float) -> Result(Float, String) {
case check_exponential_parameters(lambda) {
Error(string) ->
string
|> Error
_ ->
case x >=. 0.0 {
True ->
1.0 -. math.exp(-1.0 *. lambda *. x)
|> Ok
False ->
0.0
|> Ok
}
}
}
}
if javascript {
external fn do_exponential_cdf(Float, Float) -> Result(Float, String) =
"../../exponential.mjs" "exponential_cdf"
}
/// <div style="text-align: right;">
/// <a href="https://github.com/nicklasxyz/gleam_stats/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// </div>
///
/// Generate $$m \in \mathbb{Z}\_{>0}$$ random numbers from a continuous exponential distribution
/// with given rate parameter $$\lambda \in \(0, +\infty)$$.
///
/// The random numbers are generated using the inverse transform method.
///
/// <details>
/// <summary>Example:</summary>
///
/// import gleam/iterator.{Iterator}
/// import gleam_stats/generators
/// import gleam_stats/distributions/exponential
///
/// pub fn example() {
/// let seed: Int = 5
/// let seq: Int = 1
/// let lambda: Float = 1.
/// assert Ok(out) =
/// generators.seed_pcg32(seed, seq)
/// |> exponential.exponential_random(lambda, 5_000)
/// let rands: List(Float) = pair.first(out)
/// let stream: Iterator(Int) = pair.second(out)
/// }
/// </details>
///
/// <div style="text-align: right;">
/// <a href="#">
/// <small>Back to top ↑</small>
/// </a>
/// </div>
///
pub fn exponential_random(
stream: Iterator(Int),
lambda: Float,
m: Int,
) -> Result(#(List(Float), Iterator(Int)), String) {
do_exponential_random(stream, lambda, m)
}
if erlang {
fn do_exponential_random(
stream: Iterator(Int),
lambda: Float,
m: Int,
) -> Result(#(List(Float), Iterator(Int)), String) {
case check_exponential_parameters(lambda) {
Error(string) ->
string
|> Error
_ ->
case m > 0 {
False -> Error("Invalid input arugment: m < 0. Valid input is m > 0.")
True -> {
// Take out 'm' integers from the stream of pseudo-random numbers and generate
// uniform random numbers.
assert Ok(out) = uniform.uniform_random(stream, 0., 1., m)
// Transform the 'm' continuous uniform random numbers to exponential distributed
// random numbers.
let numbers: List(Float) =
pair.first(out)
|> list.map(fn(x: Float) -> Float {
assert Ok(x1) = math.log(x)
1. /. { -1. *. lambda } *. x1
})
// Then return a tuple consisting of a list of exponential random numbers
// and the stream of pseudo-random numbers where the 'm' integers have been dropped
// from the stream.
#(numbers, pair.second(out))
|> Ok
}
}
}
}
}
if javascript {
external fn do_exponential_random(
Iterator(Int),
Float,
Int,
) -> Result(#(List(Float), Iterator(Int)), String) =
"../../exponential.mjs" "exponential_random"
}