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
// use crate::{
// ConditionDifferentiableDistribution, DependentJoint, Distribution, DistributionError, Event,
// IndependentJoint, RandomVariable, ValueDifferentiableDistribution,
// };
// use rand::prelude::*;
// use std::{
// fmt::Debug,
// marker::PhantomData,
// ops::{BitAnd, Mul},
// };
// #[derive(Clone, Debug)]
// pub struct ConditionalizeLatentVariableDistribution<D, F, T, U, V, W>
// where
// D: Distribution<Value = T, Condition = U>,
// F: Fn((V, W)) -> (T, U) + Clone + Debug + Send + Sync,
// T: RandomVariable,
// U: RandomVariable,
// V: RandomVariable,
// W: RandomVariable,
// {
// distribution: D,
// converter: F,
// phantom: PhantomData<(V, W)>,
// }
// impl<D, F, T, U, V, W> ConditionalizeLatentVariableDistribution<D, F, T, U, V, W>
// where
// D: Distribution<Value = T, Condition = U>,
// F: Fn((V, W)) -> (T, U) + Clone + Debug + Send + Sync,
// T: RandomVariable,
// U: RandomVariable,
// V: RandomVariable,
// W: RandomVariable,
// {
// pub fn new(distribution: D, converter: F) -> Self {
// Self {
// distribution,
// converter,
// phantom: PhantomData,
// }
// }
// }
// impl<D, F, T, U, V, W> Distribution for ConditionalizeLatentVariableDistribution<D, F, T, U, V, W>
// where
// D: Distribution<Value = T, Condition = U>,
// F: Fn((V, W)) -> (T, U) + Clone + Debug + Send + Sync,
// T: RandomVariable,
// U: RandomVariable,
// V: RandomVariable,
// W: RandomVariable,
// {
// type Value = V;
// type Condition = W;
// fn p_kernel(
// &self,
// x: &Self::Value,
// theta: &Self::Condition,
// ) -> Result<f64, crate::DistributionError> {
// let converted = (x, theta).converter();
// self.distribution.p_kernel(converted.0, converted.1)
// }
// fn sample(
// &self,
// x: &Self::Value,
// theta: &Self::Condition,
// rng: &mut dyn RngCore,
// ) -> Result<Self::Value, crate::DistributionError> {
// todo!()
// }
// }
// impl<D, F, T, U, V, W, Rhs, TRhs> Mul<Rhs>
// for ConditionalizeLatentVariableDistribution<D, F, T, U, V, W>
// where
// D: Distribution<Value = T, Condition = U>,
// F: Fn((V, W)) -> (T, U) + Clone + Debug + Send + Sync,
// T: RandomVariable,
// U: RandomVariable,
// V: RandomVariable,
// W: RandomVariable,
// Rhs: Distribution<Value = TRhs, Condition = U>,
// TRhs: RandomVariable,
// {
// type Output = ConditionalizeLatentVariableDistribution<
// IndependentJoint<D, Rhs, T, TRhs, U>,
// F,
// T,
// U,
// V,
// W,
// >;
// fn mul(self, rhs: Rhs) -> Self::Output {
// ConditionalizeLatentVariableDistribution::new(
// IndependentJoint::new(self.distribution, rhs),
// self.converter,
// )
// }
// }
// impl<D, F, T, U, V, W, Rhs, URhs> BitAnd<Rhs>
// for ConditionalizeLatentVariableDistribution<D, F, T, U, V, W>
// where
// D: Distribution<Value = T, Condition = U>,
// F: Fn((V, W)) -> (T, U) + Clone + Debug + Send + Sync,
// T: RandomVariable,
// U: RandomVariable,
// V: RandomVariable,
// W: RandomVariable,
// Rhs: Distribution<Value = U, Condition = URhs>,
// URhs: RandomVariable,
// {
// type Output = ConditionalizeLatentVariableDistribution<
// DependentJoint<D, Rhs, T, U, URhs>,
// F,
// (T, U),
// URhs,
// V,
// W,
// >;
// fn bitand(self, rhs: Rhs) -> Self::Output {
// let converter_new = |result: (V, W, URhs)| ((result.0, result.1).converter(), result.2);
// ConditionalizeLatentVariableDistribution::new(
// DependentJoint::new(self.distribution, rhs),
// converter_new,
// )
// }
// }
// impl<D, F, T, U, V, W> ValueDifferentiableDistribution
// for ConditionalizeLatentVariableDistribution<D, F, T, U, V, W>
// where
// D: Distribution<Value = T, Condition = U>,
// F: Fn((V, W)) -> (T, U) + Clone + Debug + Send + Sync,
// T: RandomVariable,
// U: RandomVariable,
// V: RandomVariable,
// W: RandomVariable,
// {
// fn ln_diff_value(
// &self,
// x: &Self::Value,
// theta: &Self::Condition,
// ) -> Result<Vec<f64>, DistributionError> {
// let converted = (x, theta).converter()?;
// let v_len = x.len();
// let t_len = converted.0.len();
// let diff = t_len - v_len;
// if 0 < diff {
// let value_orig = self.distribution.ln_diff_value(converted)?;
// let condition_orig = self.distribution.ln_diff_condition(converted)?;
// let result_value = [value_orig, condition_orig[..diff]].concat();
// Ok(result_value)
// } else {
// let value_orig = self.distribution.ln_diff_value(converted)?;
// Ok(value_orig[..t_len])
// }
// }
// }
// impl<D, F, T, U, V, W> ConditionDifferentiableDistribution
// for ConditionalizeLatentVariableDistribution<D, F, T, U, V, W>
// where
// D: Distribution<Value = T, Condition = U>,
// F: Fn((V, W)) -> (T, U) + Clone + Debug + Send + Sync,
// T: RandomVariable,
// U: RandomVariable,
// V: RandomVariable,
// W: RandomVariable,
// {
// fn ln_diff_condition(
// &self,
// x: &Self::Value,
// theta: &Self::Condition,
// ) -> Result<Vec<f64>, DistributionError> {
// let converted = (x, theta).converter()?;
// let w_len = x.len();
// let u_len = converted.1.len();
// let diff = u_len - w_len;
// if 0 < diff {
// let value_orig = self.distribution.ln_diff_value(converted)?;
// let condition_orig = self.distribution.ln_diff_condition(converted)?;
// let result_condition = [value_orig[w_len..], condition_orig].concat();
// Ok(result_condition)
// } else {
// let value_orig = self.distribution.ln_diff_condition(converted)?;
// Ok(value_orig[..w_len])
// }
// }
// }