Skip to content

Commit

Permalink
Merge branch 'main' into version
Browse files Browse the repository at this point in the history
  • Loading branch information
tibetsou authored Feb 7, 2023
2 parents bd15a5b + ec93c8d commit baeece4
Show file tree
Hide file tree
Showing 56 changed files with 288 additions and 93 deletions.
2 changes: 1 addition & 1 deletion opensrdk-sandbox
Submodule opensrdk-sandbox updated from b9260c to 77c15e
2 changes: 1 addition & 1 deletion src/continuous/beta/multivariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Distribution for Dirichlet {
type Value = Vec<f64>;
type Condition = DirichletParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let alpha = theta.alpha();

if x.len() != alpha.len() {
Expand Down
2 changes: 1 addition & 1 deletion src/continuous/beta/univariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Distribution for Beta {
type Value = f64;
type Condition = BetaParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let alpha = theta.alpha();
let beta = theta.beta();

Expand Down
4 changes: 2 additions & 2 deletions src/continuous/elliptical/cauchy/multivariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ where
type Value = Vec<f64>;
type Condition = T;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let studentt_params = MultivariateStudentTWrapper::new(theta);

MultivariateStudentT::new().fk(x, &studentt_params)
MultivariateStudentT::new().p_kernel(x, &studentt_params)
}

// fn sample(
Expand Down
4 changes: 2 additions & 2 deletions src/continuous/elliptical/cauchy/univariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ impl Distribution for Cauchy {
type Value = f64;
type Condition = CauchyParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let studentt_params = StudentTParams::new(1.0, theta.mu(), theta.sigma())?;

StudentT.fk(x, &studentt_params)
StudentT.p_kernel(x, &studentt_params)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/continuous/elliptical/normal/multivariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where
type Value = Vec<f64>;
type Condition = T;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let x_mu = theta.x_mu(x)?.col_mat();
let n = x.len();

Expand Down
2 changes: 1 addition & 1 deletion src/continuous/elliptical/normal/univariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Distribution for Normal {
type Value = f64;
type Condition = NormalParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let mu = theta.mu();
let sigma = theta.sigma();

Expand Down
2 changes: 1 addition & 1 deletion src/continuous/elliptical/student_t/multivariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ where
type Value = Vec<f64>;
type Condition = T;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let elliptical = theta.elliptical();
let x_mu = elliptical.x_mu(x)?.col_mat();

Expand Down
2 changes: 1 addition & 1 deletion src/continuous/elliptical/student_t/univariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Distribution for StudentT {
type Value = f64;
type Condition = StudentTParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let nu = theta.nu();
let mu = theta.mu();
let sigma = theta.sigma();
Expand Down
2 changes: 1 addition & 1 deletion src/continuous/exp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Distribution for Exp {
type Value = f64;
type Condition = ExpParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let lambda = theta.lambda();

Ok(lambda * (-lambda * x).exp())
Expand Down
2 changes: 1 addition & 1 deletion src/continuous/fisher_f/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Distribution for FisherF {
type Value = f64;
type Condition = FisherFParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let m = theta.m();
let n = theta.n();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Distribution for InverseWishart {
type Condition = InverseWishartParams;

/// x must be cholesky decomposed
fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let lpsi = theta.lpsi().0.to_mat();
let nu = theta.nu();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Distribution for NormalInverseWishart {
type Value = ExactMultivariateNormalParams;
type Condition = NormalInverseWishartParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let mu0 = theta.mu0().clone();
let lambda = theta.lambda();
let lpsi = theta.lpsi().clone();
Expand All @@ -43,7 +43,7 @@ impl Distribution for NormalInverseWishart {
let n = MultivariateNormal::new();
let w_inv = InverseWishart;

Ok(n.fk(
Ok(n.p_kernel(
mu,
&ExactMultivariateNormalParams::new(
mu0,
Expand All @@ -55,7 +55,7 @@ impl Distribution for NormalInverseWishart {
.unwrap(),
),
)?,
)? * w_inv.fk(lsigma, &InverseWishartParams::new(lpsi, nu)?)?)
)? * w_inv.p_kernel(lsigma, &InverseWishartParams::new(lpsi, nu)?)?)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/continuous/gamma/chi_squared/multivariate/wishart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Distribution for Wishart {
type Condition = WishartParams;

/// x must be cholesky decomposed
fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let lv = theta.lv();
let n = theta.n();

Expand Down
2 changes: 1 addition & 1 deletion src/continuous/gamma/chi_squared/univariate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Distribution for ChiSquared {
type Value = f64;
type Condition = ChiSquaredParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let k = theta.k();

Ok(x.powf(k / 2.0 - 1.0) * (-x / 2.0).exp())
Expand Down
2 changes: 1 addition & 1 deletion src/continuous/gamma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Distribution for Gamma {
type Value = f64;
type Condition = GammaParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let shape = theta.shape();
let scale = theta.scale();

Expand Down
2 changes: 1 addition & 1 deletion src/continuous/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Distribution for ContinuousUniform {
type Value = f64;
type Condition = Range<f64>;

fn fk(
fn p_kernel(
&self,
_: &Self::Value,
_theta: &Self::Condition,
Expand Down
2 changes: 1 addition & 1 deletion src/discrete/geometric/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Distribution for Geometric {
type Value = u64;
type Condition = GeometricParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let p = theta.p();

Ok((1.0 - p).powi((x - 1) as i32) * p)
Expand Down
6 changes: 5 additions & 1 deletion src/discrete/integer/bernoulli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ impl Distribution for Bernoulli {
type Value = bool;
type Condition = BernoulliParams;

fn fk(&self, _x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(
&self,
_x: &Self::Value,
theta: &Self::Condition,
) -> Result<f64, DistributionError> {
Ok(theta.p())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/discrete/integer/categorical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Distribution for Categorical {
type Value = usize;
type Condition = CategoricalParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let k = *x;
if k < theta.p().len() {
return Err(DistributionError::InvalidParameters(
Expand Down
2 changes: 1 addition & 1 deletion src/discrete/nomial/binomial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Distribution for Binomial {
type Value = u64;
type Condition = BinomialParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let n = theta.n();
let p = theta.p();

Expand Down
2 changes: 1 addition & 1 deletion src/discrete/nomial/multinomial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Distribution for Multinominal {
type Value = u64;
type Condition = MultinomialParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let n = theta.n();
let p = theta.p();

Expand Down
2 changes: 1 addition & 1 deletion src/discrete/poisson/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Distribution for Poisson {
type Value = u64;
type Condition = PoissonParams;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
let lambda = theta.lambda();

Ok(lambda.powi(*x as i32) / factorial(*x) as f64 * (-lambda).exp())
Expand Down
6 changes: 5 additions & 1 deletion src/discrete/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ where
type Value = T;
type Condition = HashSet<T>;

fn fk(&self, _x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(
&self,
_x: &Self::Value,
theta: &Self::Condition,
) -> Result<f64, DistributionError> {
Ok(1.0 / theta.len() as f64)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/distribution/conditionalize_latent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
// type Value = V;
// type Condition = W;

// fn fk(
// fn p_kernel(
// &self,
// x: &Self::Value,
// theta: &Self::Condition,
// ) -> Result<f64, crate::DistributionError> {
// let converted = (x, theta).converter();
// self.distribution.fk(converted.0, converted.1)
// self.distribution.p_kernel(converted.0, converted.1)
// }

// fn sample(
Expand Down
4 changes: 2 additions & 2 deletions src/distribution/conditioned/condition_differentiable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ where
type Value = T;
type Condition = U2;

fn fk(
fn p_kernel(
&self,
x: &Self::Value,
theta: &Self::Condition,
) -> Result<f64, crate::DistributionError> {
self.conditioned_distribution
.distribution
.fk(x, &(self.conditioned_distribution.condition)(theta)?)
.p_kernel(x, &(self.conditioned_distribution.condition)(theta)?)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/distribution/conditioned/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ where
type Value = T;
type Condition = U2;

fn fk(
fn p_kernel(
&self,
x: &Self::Value,
theta: &Self::Condition,
) -> Result<f64, crate::DistributionError> {
self.distribution.fk(x, &(self.condition)(theta)?)
self.distribution.p_kernel(x, &(self.condition)(theta)?)
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/distribution/continuous_samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ where

T::restore(sum.elems(), &info)
}

pub fn mean(&mut self) -> Result<T, DistributionError> {
let (sum, info) = self.sum().unwrap().transform_vec();
let elems = sum
.iter()
.map(|elem| elem / self.samples.len() as f64)
.collect::<Vec<f64>>();
T::restore(&elems, &info)
}
}

impl<T> Distribution for ContinuousSamplesDistribution<T>
Expand All @@ -70,7 +79,7 @@ where
type Value = T;
type Condition = ();

fn fk(&self, x: &Self::Value, _: &Self::Condition) -> Result<f64, DistributionError> {
fn p_kernel(&self, x: &Self::Value, _: &Self::Condition) -> Result<f64, DistributionError> {
let eq_num = &self
.samples
.iter()
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/degenerate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ where
type Value = T;
type Condition = ();

fn fk(
fn p_kernel(
&self,
x: &Self::Value,
_theta: &Self::Condition,
Expand Down
6 changes: 3 additions & 3 deletions src/distribution/dependent_joint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ where
type Value = (T, UL);
type Condition = UR;

fn fk(&self, x: &(T, UL), theta: &UR) -> Result<f64, DistributionError> {
Ok(self.lhs.fk(&x.0, &x.1)? * self.rhs.fk(&x.1, theta)?)
fn p_kernel(&self, x: &(T, UL), theta: &UR) -> Result<f64, DistributionError> {
Ok(self.lhs.p_kernel(&x.0, &x.1)? * self.rhs.p_kernel(&x.1, theta)?)
}
}

Expand Down Expand Up @@ -102,7 +102,7 @@ where
theta: &Self::Condition,
) -> Result<Vec<f64>, crate::DistributionError> {
// let diff_l = &self.lhs.ln_diff_value(&x.0, &x.1)?;
// let diff = (diff_l.clone().col_mat() * &self.rhs.fk(&x.1, theta)?).vec();
// let diff = (diff_l.clone().col_mat() * &self.rhs.p_kernel(&x.1, theta)?).vec();
let diff_a = self.lhs.ln_diff_value(&x.0, &x.1).unwrap();
let diff_b = self.lhs.ln_diff_condition(&x.0, &x.1)?.col_mat()
+ self.rhs.ln_diff_value(&x.1, &theta)?.col_mat();
Expand Down
13 changes: 8 additions & 5 deletions src/distribution/discrete_posterior/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ where
.range
.par_iter()
.map(|u| -> Result<_, DistributionError> {
Ok((self.likelihood.fk(theta, u)? * self.prior.fk(u, &())?, u))
Ok((
self.likelihood.p_kernel(theta, u)? * self.prior.p_kernel(u, &())?,
u,
))
})
.collect::<Result<Vec<(f64, &B)>, _>>()?;
Ok(weighted)
Expand All @@ -64,8 +67,8 @@ where
type Value = B;
type Condition = A;

fn fk(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
Ok(self.likelihood.fk(theta, x)? * self.prior.fk(x, &())?)
fn p_kernel(&self, x: &Self::Value, theta: &Self::Condition) -> Result<f64, DistributionError> {
Ok(self.likelihood.p_kernel(theta, x)? * self.prior.p_kernel(x, &())?)
}
}

Expand Down Expand Up @@ -109,8 +112,8 @@ mod tests {
);

// println!("{:?}", model.weighted(&1.0).unwrap());
let true_result = model.fk(&true, &1.0).unwrap();
let false_result = model.fk(&false, &1.0).unwrap();
let true_result = model.p_kernel(&true, &1.0).unwrap();
let false_result = model.p_kernel(&false, &1.0).unwrap();
assert!(true_result < false_result);
}
}
Loading

0 comments on commit baeece4

Please sign in to comment.