Created
October 6, 2015 06:35
-
-
Save EntilZha/faaa11e4b05afcba68e0 to your computer and use it in GitHub Desktop.
Rust code trying to implement the EM algorithm for a simple example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let observations = [5, 9, 8, 4, 7]; | |
let (theta_a, theta_b) = em(0.5, 0.5, observations); | |
println!("ThetaA: {}, ThetaB: {}", theta_a, theta_b); | |
} | |
fn em(theta_a_0: f64, theta_b_0: f64, observations: [i32; 5]) -> (f64, f64) { | |
let mut theta_a = theta_a_0; | |
let mut theta_b = theta_b_0; | |
let a_probs = observations.iter().map(|x| binomial(theta_a, *x, 10)); | |
let b_probs = observations.iter().map(|x| binomial(theta_b, *x, 10)); | |
let norms: Vec<f64> = a_probs.zip(b_probs).map(|x| x.0 + x.1).collect(); | |
let a_norm_probs = a_probs.zip(&norms).map(|x| x.0 / x.1); | |
let b_norm_probs = b_probs.zip(&norms).map(|x| x.0 / x.1); | |
(0.0, 0.0) | |
} | |
fn binomial(p: f64, heads: i32, n: i32) -> f64 { | |
p.powi(heads) * (1.0 - p).powi(n - heads) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why would this have a compile error on inference? If you put types
Vec<f64>
for those it works, but don't know why I should have to: