|
| 1 | +#![allow(dead_code)] |
| 2 | + |
| 3 | +use crate::algorithms::number::Number; |
| 4 | +use crate::algorithms::rv::{DomainType, FunctionalForm, RandomVariable}; |
| 5 | + |
| 6 | +/// Truncates a discrete random variable by cutting off a portion of the support |
| 7 | +/// and normalizing total probability of the distribution to 1. |
| 8 | +/// |
| 9 | +/// # Arguments |
| 10 | +/// * `random_variable` - the random variable to truncate |
| 11 | +/// * `min_support` - the minimum support of the new random variable. |
| 12 | +/// Must be greater than or equal to the current minimum support. |
| 13 | +/// * `max_support` - the maximum support of the new random variable. |
| 14 | +/// Must be less than or equal to the current maximum support. |
| 15 | +/// |
| 16 | +/// # Returns |
| 17 | +/// * `truncated_rv` - the truncated random variable |
| 18 | +/// |
| 19 | +/// # Examples |
| 20 | +/// ``` |
| 21 | +/// use applpy_rust::algorithms::number::Number; |
| 22 | +/// use applpy_rust::algorithms::rv::{DomainType, FunctionalForm, RandomVariable}; |
| 23 | +/// use applpy_rust::algorithms::transform::truncate_discrete; |
| 24 | +/// use num_rational::Rational64; |
| 25 | +/// |
| 26 | +/// let rv = RandomVariable { |
| 27 | +/// function: vec![ |
| 28 | +/// Number::Rational(Rational64::new(1, 10)), |
| 29 | +/// Number::Rational(Rational64::new(2, 10)), |
| 30 | +/// Number::Rational(Rational64::new(3, 10)), |
| 31 | +/// Number::Rational(Rational64::new(4, 10)), |
| 32 | +/// ], |
| 33 | +/// support: vec![ |
| 34 | +/// Number::Integer(1), |
| 35 | +/// Number::Integer(2), |
| 36 | +/// Number::Integer(3), |
| 37 | +/// Number::Integer(4), |
| 38 | +/// ], |
| 39 | +/// functional_form: FunctionalForm::Pdf, |
| 40 | +/// domain_type: DomainType::Discrete, |
| 41 | +/// }; |
| 42 | +/// |
| 43 | +/// let truncated = truncate_discrete(&rv, Number::Integer(2), Number::Integer(3)).unwrap(); |
| 44 | +/// |
| 45 | +/// assert_eq!(truncated.support, vec![Number::Integer(2), Number::Integer(3)]); |
| 46 | +/// assert_eq!( |
| 47 | +/// truncated.function, |
| 48 | +/// vec![ |
| 49 | +/// Number::Rational(Rational64::new(2, 5)), |
| 50 | +/// Number::Rational(Rational64::new(3, 5)), |
| 51 | +/// ] |
| 52 | +/// ); |
| 53 | +/// assert!(matches!(truncated.functional_form, FunctionalForm::Pdf)); |
| 54 | +/// assert!(matches!(truncated.domain_type, DomainType::Discrete)); |
| 55 | +/// ``` |
| 56 | +pub fn truncate_discrete( |
| 57 | + random_variable: &RandomVariable, |
| 58 | + min_support: Number, |
| 59 | + max_support: Number, |
| 60 | +) -> Result<RandomVariable, String> { |
| 61 | + let pdf_random_variable = random_variable.to_pdf()?; |
| 62 | + let function = pdf_random_variable.function; |
| 63 | + let support = pdf_random_variable.support; |
| 64 | + |
| 65 | + if min_support >= max_support { |
| 66 | + return Err("max_support must be greater than the min_support".to_string()); |
| 67 | + } |
| 68 | + |
| 69 | + let first_support = *support.first().ok_or("support is empty")?; |
| 70 | + if min_support < first_support { |
| 71 | + return Err( |
| 72 | + "min support must be greater than or equal to the lowest support value".to_string(), |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + let last_support = *support.last().ok_or("support is empty")?; |
| 77 | + if max_support > last_support { |
| 78 | + return Err( |
| 79 | + "max support must be less than or equal to the highest support value".to_string(), |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + let mut truncation_area = Number::Integer(0); |
| 84 | + for (&support_value, &function_value) in support.iter().zip(function.iter()) { |
| 85 | + if support_value >= min_support && support_value <= max_support { |
| 86 | + truncation_area += function_value; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + let zero = Number::Integer(0); |
| 91 | + if truncation_area == zero { |
| 92 | + return Err("there is no probability mass within the specified support range".to_string()); |
| 93 | + } |
| 94 | + |
| 95 | + let mut truncated_function = Vec::new(); |
| 96 | + let mut truncated_support = Vec::new(); |
| 97 | + |
| 98 | + for (&support_value, &function_value) in support.iter().zip(function.iter()) { |
| 99 | + if support_value >= min_support && support_value <= max_support { |
| 100 | + let probability = function_value / truncation_area; |
| 101 | + truncated_function.push(probability); |
| 102 | + truncated_support.push(support_value); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + let truncated_rv = RandomVariable { |
| 107 | + function: truncated_function, |
| 108 | + support: truncated_support, |
| 109 | + functional_form: FunctionalForm::Pdf, |
| 110 | + domain_type: DomainType::Discrete, |
| 111 | + }; |
| 112 | + Ok(truncated_rv) |
| 113 | +} |
| 114 | + |
| 115 | +#[cfg(test)] |
| 116 | +mod tests { |
| 117 | + use super::*; |
| 118 | + use num_rational::Rational64; |
| 119 | + |
| 120 | + fn sample_discrete_rv() -> RandomVariable { |
| 121 | + RandomVariable { |
| 122 | + function: vec![ |
| 123 | + Number::Rational(Rational64::new(1, 10)), |
| 124 | + Number::Rational(Rational64::new(2, 10)), |
| 125 | + Number::Rational(Rational64::new(3, 10)), |
| 126 | + Number::Rational(Rational64::new(4, 10)), |
| 127 | + ], |
| 128 | + support: vec![ |
| 129 | + Number::Integer(1), |
| 130 | + Number::Integer(2), |
| 131 | + Number::Integer(3), |
| 132 | + Number::Integer(4), |
| 133 | + ], |
| 134 | + functional_form: FunctionalForm::Pdf, |
| 135 | + domain_type: DomainType::Discrete, |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn truncate_discrete_renormalizes_probabilities_within_range() { |
| 141 | + let rv = sample_discrete_rv(); |
| 142 | + let truncated = truncate_discrete(&rv, Number::Integer(2), Number::Integer(3)).unwrap(); |
| 143 | + |
| 144 | + assert_eq!( |
| 145 | + truncated.support, |
| 146 | + vec![Number::Integer(2), Number::Integer(3)] |
| 147 | + ); |
| 148 | + assert_eq!( |
| 149 | + truncated.function, |
| 150 | + vec![ |
| 151 | + Number::Rational(Rational64::new(2, 5)), |
| 152 | + Number::Rational(Rational64::new(3, 5)) |
| 153 | + ] |
| 154 | + ); |
| 155 | + assert!(matches!(truncated.functional_form, FunctionalForm::Pdf)); |
| 156 | + assert!(matches!(truncated.domain_type, DomainType::Discrete)); |
| 157 | + } |
| 158 | + |
| 159 | + #[test] |
| 160 | + fn truncate_discrete_returns_error_when_min_support_exceeds_bounds() { |
| 161 | + let rv = sample_discrete_rv(); |
| 162 | + let result = truncate_discrete(&rv, Number::Integer(0), Number::Integer(3)); |
| 163 | + |
| 164 | + assert!(matches!( |
| 165 | + result, |
| 166 | + Err(msg) if msg == "min support must be greater than or equal to the lowest support value" |
| 167 | + )); |
| 168 | + } |
| 169 | +} |
0 commit comments