dices 0.3.0

calculate discrete probability distributions and statistics for combinations of dices
Documentation
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

#[cfg(feature = "wasm")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "wasm")]
use fraction::{BigFraction, BigUint, Sign};

#[cfg(feature = "wasm")]
use std::fmt::Display;

use fraction::{One, ToPrimitive, Zero};
use std::ops::Add;

use crate::{
    dice_string_parser::DiceBuildingError,
    wasm_safe::{elapsed_millis, random_number_between_0_and_1, WasmSafeInstant},
    DiceBuilder,
};

use super::dice_builder::{AggrValue, Prob, Value};

/// A [`Dice`] represents a discrete probability distribution, providing paramters like mean, standard deviation and the `roll()` method to randomly sample from this distribution
///
/// A [`Dice`] is always created using a [`DiceBuilder`]. The simplest way is to use:
/// ```
/// use dices::Dice;
/// let dice = Dice::build_from_string("2w6+3").unwrap();
/// ```
/// which is equivalent to
/// ```
/// use dices::DiceBuilder;
/// let dice_builder = DiceBuilder::from_string("2w6+3").unwrap();
/// let dice = dice_builder.build();
/// ```
///
/// Values of the distribution are of type [`i64`]
/// The probabilities are of type [`BigFraction`](fraction::BigFraction) from the [`fraction`](fraction) crate.
/// This allows for precise probabilites with infinite precision, at the cost of some slower operations compared to floats, but avoids pitfalls like floating point precision errors.

#[derive(Debug, PartialEq, Eq)]
pub struct Dice {
    /// a string that can be used to recreate the [`DiceBuilder`] that the [`Dice`] was created from.
    pub builder_string: String,
    /// mininum value of the probability distribution
    pub min: Value,
    /// maximum value of the probability distribution
    pub max: Value,
    /// median  of the probability distribution
    pub median: Value,
    /// mode or modes of the probability distribution
    pub mode: Vec<Value>,
    /// mean of the probability distribution
    pub mean: AggrValue,
    /// variance of the probability distribution
    pub variance: AggrValue,
    /// the probability mass function (pmf) of the dice
    ///
    /// tuples of each value and its probability in ascending order (regarding value)
    pub distribution: Vec<(Value, Prob)>,
    /// the cumulative distribution function (cdf) of the dice
    ///
    /// tuples of each value and its cumulative probability in ascending order (regarding value)
    pub cumulative_distribution: Vec<(Value, Prob)>,

    /// time it took to build the dice in microseconds
    pub build_time: u64,
}

impl Dice {
    /// uses the `input` to create a [`DiceBuilder`] and calls `build()` on it
    pub fn build_from_string(input: &str) -> Result<Dice, DiceBuildingError> {
        let builder = DiceBuilder::from_string(input)?;
        Ok(builder.build())
    }

    /// uses the `input` to create a [`DiceBuilder`]. Same as [`DiceBuilder::from_string(input)`]
    pub fn builder(input: &str) -> Result<DiceBuilder, DiceBuildingError> {
        DiceBuilder::from_string(input)
    }

    /// builds a [`Dice`] from a given [`DiceBuilder`]
    ///
    /// this method calculates the distribution and all distribution paramters on the fly, to create the [`Dice`].
    /// Depending on the complexity of the `dice_builder` heavy lifting like convoluting probability distributions may take place here.
    pub fn from_builder(dice_builder: DiceBuilder) -> Dice {
        let start_instant = WasmSafeInstant::now();
        let distribution: Vec<(Value, Prob)> = dice_builder.distribution_iter().collect();
        let max: Value = distribution.last().map(|e| e.0).unwrap();
        let min: Value = distribution.first().map(|e| e.0).unwrap();
        let mut mean: AggrValue = AggrValue::from(0);

        let mut total_probability: Prob = Prob::new(0u64, 1u64);
        let median_prob: Prob = Prob::new(1u64, 2u64);
        // todo median
        let mut median: Option<Value> = None;
        let mut mode: Option<(Vec<Value>, Prob)> = None;

        for (val, prob) in distribution.iter().cloned() {
            mean += prob.clone() * Prob::from(val);
            total_probability += prob.clone();
            match median {
                Some(_) => {}
                None => {
                    if total_probability >= median_prob {
                        median = Some(val);
                    }
                }
            }
            match &mode {
                Some((old_vec, p)) => {
                    if prob > *p {
                        mode = Some((vec![val], prob));
                    } else if prob == *p {
                        let newvec: Vec<Value> = [val].iter().chain(old_vec).copied().collect();
                        mode = Some((newvec, prob));
                    }
                }
                None => {
                    mode = Some((vec![val], prob));
                }
            }
        }

        let mut variance: AggrValue = AggrValue::from(0);
        for (val, prob) in distribution.iter().cloned() {
            let val = AggrValue::from(val);
            let val_minus_mean = &val - &mean;
            let square = (&val_minus_mean) * (&val_minus_mean);
            variance += square * prob
        }

        let median = median.unwrap();
        let mode = mode.unwrap().0;

        // TODO: MAYBE: make cumulative_distribution lazy?
        let cumulative_distribution = cumulative_distribution_from_distribution(&distribution);

        let build_time: u64 = elapsed_millis(&start_instant);
        Dice {
            mean,
            variance,
            mode,
            min,
            max,
            median,
            distribution,
            cumulative_distribution,
            builder_string: dice_builder.to_string(),
            build_time,
        }
    }

    /// Rolls a random number for this [`Dice`].
    ///
    /// For this a random float is uniformly sampled over the interval [0,1) and checked against the accumulated discrete porbability distribution of this [`Dice`].
    ///
    /// # Examples
    ///
    /// rolling 2 standard playing dice:
    /// ```
    /// use dices::Dice;
    /// let d : Dice = Dice::build_from_string("2d6").unwrap();
    /// println!("rolled: {}", d.roll());
    /// //prints something like: "rolled: 9"
    /// ```
    pub fn roll(&self) -> Value {
        let r = random_number_between_0_and_1();
        for (val, prob) in self.cumulative_distribution.iter() {
            if prob.to_f64().unwrap() >= r {
                return *val;
            }
        }
        panic! {"Something went wrong in rolling. random value: {r}"}
    }

    /// rolls the [`Dice`] `n` times and returns the results as a vector
    pub fn roll_many(&self, n: usize) -> Vec<Value> {
        (0..n).map(|_| self.roll()).collect()
    }

    /// probability that a number sampled from `self` is `value`
    pub fn prob(&self, value: Value) -> Prob {
        match self.distribution.iter().find(|(v, _)| *v == value) {
            None => Prob::zero(),
            Some((_, p)) => p.clone(),
        }
    }

    /// probability that a number sampled from `self` is less than or equal to `value`
    pub fn prob_lte(&self, value: Value) -> Prob {
        if let Some((v, _)) = self.distribution.last() {
            if value >= *v {
                return Prob::one();
            }
        }

        let mut lastp: Option<&Prob> = None;
        for (v, p) in self.cumulative_distribution.iter() {
            if *v > value {
                break;
            }
            lastp = Some(p);
        }
        match lastp {
            None => Prob::zero(),
            Some(p) => p.clone(),
        }
    }

    /// probability that a number sampled from `self` is less than `value`
    pub fn prob_lt(&self, value: Value) -> Prob {
        if let Some((v, _)) = self.distribution.last() {
            if value > *v {
                return Prob::one();
            }
        }

        let mut lastp: Option<&Prob> = None;
        for (v, p) in self.cumulative_distribution.iter() {
            if *v >= value {
                break;
            }
            lastp = Some(p);
        }
        match lastp {
            None => Prob::zero(),
            Some(p) => p.clone(),
        }
    }

    /// probability that a number sampled from `self` is greater than or equal to `value`
    pub fn prob_gte(&self, value: Value) -> Prob {
        Prob::one() - self.prob_lt(value)
    }

    /// probability that a number sampled from `self` is greater than `value`
    pub fn prob_gt(&self, value: Value) -> Prob {
        Prob::one() - self.prob_lte(value)
    }

    /// returns prob_lt, prob_lte, prob, prob_gte, prob_gt in the [ProbAll] struct.
    /// Computes them more efficiently than if we use all the functions individually.
    pub fn prob_all(&self, value: Value) -> ProbAll {
        let lt = self.prob_lt(value);
        let eq = self.prob(value);
        let lte = &eq + &lt;
        let gte = &Prob::one() - &lt;
        let gt = &Prob::one() - &lte;
        ProbAll {
            lt,
            lte,
            eq,
            gte,
            gt,
        }
    }

    /// returns the smallest p-quantile of the distribution.
    /// The smallest p-quantile q is the smallest value in the distribution for which it holds, that P(x ≤ q) ≥ p
    /// currently the trait [ToFloat] is implementen for [BigFraction] and [f64]
    pub fn quantile<T: ToFloat>(&self, p: T) -> Value {
        let p: f64 = p.to_float();
        if p >= 1.0 {
            return self.cumulative_distribution.last().unwrap().0;
        }
        for (i, prob) in &self.cumulative_distribution {
            if prob.to_float() >= p {
                return *i;
            }
        }
        panic!("should never end up here if a proper cumulative distribution is present")
    }
}

pub trait ToFloat {
    fn to_float(&self) -> f64;
}

impl ToFloat for f64 {
    fn to_float(&self) -> f64 {
        *self
    }
}

impl ToFloat for Prob {
    fn to_float(&self) -> f64 {
        self.to_f64().unwrap()
    }
}

fn cumulative_distribution_from_distribution(distribution: &[(Value, Prob)]) -> Vec<(Value, Prob)> {
    let mut acc_distr: Vec<(Value, Prob)> = vec![];
    let mut last_acc_prob: Option<Prob> = None;
    for (val, prob) in distribution {
        match last_acc_prob {
            None => {
                acc_distr.push((*val, prob.clone()));
                last_acc_prob = Some(prob.clone());
            }
            Some(acc_p) => {
                let acc_p = acc_p.clone().add(prob.clone());
                last_acc_prob = Some(acc_p.clone());
                acc_distr.push((*val, acc_p));
            }
        }
    }
    acc_distr
}

#[cfg(feature = "wasm")]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub struct JsDice {
    dice: Dice,
}

#[cfg(feature = "wasm")]
#[cfg_attr(feature = "wasm", wasm_bindgen)]
impl JsDice {
    #[cfg_attr(feature = "wasm", wasm_bindgen(getter))]
    pub fn builder_string(&self) -> String {
        self.dice.builder_string.to_string()
    }

    #[cfg_attr(feature = "wasm", wasm_bindgen(getter))]
    pub fn min(&self) -> Value {
        self.dice.min
    }

    #[cfg_attr(feature = "wasm", wasm_bindgen(getter))]
    pub fn max(&self) -> Value {
        self.dice.max
    }

    #[cfg_attr(feature = "wasm", wasm_bindgen(getter))]
    pub fn median(&self) -> Value {
        self.dice.median
    }

    #[cfg_attr(feature = "wasm", wasm_bindgen(getter))]
    pub fn mode(&self) -> Vec<Value> {
        self.dice.mode.iter().cloned().collect()
    }

    #[cfg_attr(feature = "wasm", wasm_bindgen(getter))]
    pub fn mean(&self) -> wasm_bindgen::JsValue {
        serde_wasm_bindgen::to_value(&JsFraction::from_big_fraction(&self.dice.mean)).unwrap()
    }

    #[cfg_attr(feature = "wasm", wasm_bindgen(getter))]
    pub fn variance(&self) -> wasm_bindgen::JsValue {
        serde_wasm_bindgen::to_value(&JsFraction::from_big_fraction(&self.dice.variance)).unwrap()
    }

    #[cfg_attr(feature = "wasm", wasm_bindgen(getter))]
    pub fn distribution(&self) -> wasm_bindgen::JsValue {
        let js_dist = JsDistribution::from_distribution(&self.dice.distribution);
        serde_wasm_bindgen::to_value(&js_dist).unwrap()
    }

    #[cfg_attr(feature = "wasm", wasm_bindgen(getter))]
    pub fn cumulative_distribution(&self) -> wasm_bindgen::JsValue {
        let js_dist = JsDistribution::from_distribution(&self.dice.cumulative_distribution);
        serde_wasm_bindgen::to_value(&js_dist).unwrap()
    }

    #[cfg_attr(feature = "wasm", wasm_bindgen(getter))]
    pub fn build_time(&self) -> u64 {
        self.dice.build_time
    }

    pub fn build_from_string(input: &str) -> Result<JsDice, String> {
        match DiceBuilder::from_string(input) {
            Ok(builder) => Ok(JsDice {
                dice: builder.build(),
            }),
            Err(err) => Err(format!("{:?}", err)),
        }
    }

    pub fn roll(&self) -> Value {
        self.dice.roll()
    }

    pub fn roll_many(&self, n: usize) -> Vec<Value> {
        self.dice.roll_many(n)
    }
    /// probability that a number sampled from `self` is less than `value`
    pub fn prob_lt(&self, value: Value) -> wasm_bindgen::JsValue {
        serde_wasm_bindgen::to_value(&JsFraction::from_big_fraction(&self.dice.prob_lt(value)))
            .unwrap()
    }

    /// probability that a number sampled from `self` is less or equal than `value`
    pub fn prob_lte(&self, value: Value) -> wasm_bindgen::JsValue {
        serde_wasm_bindgen::to_value(&JsFraction::from_big_fraction(&self.dice.prob_lte(value)))
            .unwrap()
    }

    /// probability that a number sampled from `self` is greater than or equal to `value`
    pub fn prob_gte(&self, value: Value) -> wasm_bindgen::JsValue {
        serde_wasm_bindgen::to_value(&JsFraction::from_big_fraction(&self.dice.prob_gte(value)))
            .unwrap()
    }

    /// probability that a number sampled from `self` is greater than `value`
    pub fn prob_gt(&self, value: Value) -> wasm_bindgen::JsValue {
        serde_wasm_bindgen::to_value(&JsFraction::from_big_fraction(&self.dice.prob_gt(value)))
            .unwrap()
    }

    /// returns \[prob_lt, prob_lte, prob, prob_gte, prob_gt\] as a vector.
    /// Computes them more efficiently than if we use all the functions individually.
    pub fn prob_all(&self, value: Value) -> wasm_bindgen::JsValue {
        let ProbAll {
            lt,
            lte,
            eq,
            gte,
            gt,
        } = self.dice.prob_all(value);

        let js_fractions: Vec<JsFraction> = vec![lt, lte, eq, gte, gt]
            .iter()
            .map(|e| JsFraction::from_big_fraction(&e))
            .collect();

        serde_wasm_bindgen::to_value(&js_fractions).unwrap()
    }

    /// returns the smallest p-quantile of the distribution.
    /// The smallest p-quantile q is the smallest value in the distribution for which it holds, that P(x ≤ q) ≥ p
    /// currently the trait [ToFloat] is implementen for [BigFraction] and [f64]
    pub fn quantile(&self, p: f64) -> Value {
        self.dice.quantile(p)
    }
}

#[cfg(feature = "wasm")]
#[derive(Debug, Serialize, Deserialize)]
pub struct JsDistribution {
    pub values: Vec<(Value, JsFraction)>,
}

#[cfg(feature = "wasm")]
impl JsDistribution {
    pub fn from_distribution(dist: &Vec<(Value, Prob)>) -> JsDistribution {
        JsDistribution {
            values: dist
                .iter()
                .map(|e| (e.0, JsFraction::from_big_fraction(&e.1)))
                .collect(),
        }
    }
}

#[cfg(feature = "wasm")]
#[derive(Debug, Serialize, Deserialize)]
pub struct JsFraction {
    // pub numer: Vec<u64>,
    // pub denom: Vec<u64>,
    // pub negative: bool,
    pub string: String,
    pub float: f32,
}

#[cfg(feature = "wasm")]
impl Display for JsFraction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} ({})", self.string, self.float,)
    }
}

#[cfg(feature = "wasm")]
impl JsFraction {
    pub fn from_big_fraction(big_fraction: &BigFraction) -> JsFraction {
        JsFraction {
            string: big_fraction.to_string(),
            float: big_fraction.to_f32().unwrap(),
        }
    }
}

// https://rustwasm.github.io/wasm-bindgen/reference/arbitrary-data-with-serde.html

pub struct ProbAll {
    pub lt: Prob,
    pub lte: Prob,
    pub eq: Prob,
    pub gte: Prob,
    pub gt: Prob,
}