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) | |
} |
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:
$ cargo build
Compiling EMAlgorithm v0.1.0 (file:///Users/pedro/Documents/Code/EMAlgorithm)
src/main.rs:12:27: 12:41 error: the type of this value must be known in this context
src/main.rs:12 let norms: Vec<f64> = a_probs.iter().zip(b_probs).map(|x| x.0 + x.1).collect();
^~~~~~~~~~~~~~
src/main.rs:12:63: 12:66 error: the type of this value must be known in this context
src/main.rs:12 let norms: Vec<f64> = a_probs.iter().zip(b_probs).map(|x| x.0 + x.1).collect();
^~~
note: in expansion of closure expansion
src/main.rs:12:59: 12:72 note: expansion site
src/main.rs:13:59: 13:62 error: the type of this value must be known in this context
src/main.rs:13 let a_norm_probs = a_probs.iter().zip(&norms).map(|x| x.0 / x.1);
^~~
note: in expansion of closure expansion
src/main.rs:13:55: 13:68 note: expansion site
src/main.rs:14:59: 14:62 error: the type of this value must be known in this context
src/main.rs:14 let b_norm_probs = b_probs.iter().zip(&norms).map(|x| x.0 / x.1);
^~~
note: in expansion of closure expansion
src/main.rs:14:55: 14:68 note: expansion site
error: aborting due to 4 previous errors
Could not compile `EMAlgorithm`.
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)).collect();
let b_probs = observations.iter().map(|x| binomial(theta_b, *x, 10)).collect();
let norms: Vec<f64> = a_probs.iter().zip(b_probs.iter()).map(|x| x.0 + x.1).collect();
let a_norm_probs = a_probs.iter().zip(&norms).map(|x| x.0 / x.1);
let b_norm_probs = b_probs.iter().zip(&norms).map(|x| x.0 / x.1);
(0.0, 0.0)
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compile error:
Trying to make code look similar to my scala implementation: