Created
January 6, 2017 20:23
-
-
Save anonymous/855a54a3f6b8c58d59adbcf88925766f to your computer and use it in GitHub Desktop.
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
#![feature(non_ascii_idents)] | |
use std::io::{Write, BufRead, BufReader}; | |
use std::process::exit; | |
use std::fs::File; | |
fn main() { | |
let args : Vec<_> = std::env::args().collect(); | |
if args.len() != 2 { | |
writeln!(std::io::stderr(), "Usage: {} FILE", args[0]).unwrap(); | |
exit(1); | |
} | |
let reader = BufReader::new(File::open(&args[1]).unwrap()); | |
let log_pops : Vec<f32> = | |
reader.lines().map(Result::unwrap) | |
.map(|ref line| line.parse::<u32>().unwrap()) | |
.map(|pop| f32::ln(pop as f32)).collect(); | |
let n = log_pops.len() as f32; | |
let mut x̅ : f32 = 0.0; | |
let mut y̅ : f32 = 0.0; | |
let mut s̅q̅r̅x̅ : f32 = 0.0; | |
let mut s̅q̅r̅y̅ : f32 = 0.0; | |
let mut x̅y̅ : f32 = 0.0; | |
for (&x, y) in log_pops.iter().zip((1..log_pops.len()).map(|i| f32::ln(i as f32))) { | |
x̅ += x; | |
s̅q̅r̅x̅ += x * x; | |
y̅ += y; | |
s̅q̅r̅y̅ += y * y; | |
x̅y̅ += x * y; | |
} | |
x̅ /= n; | |
y̅ /= n; | |
s̅q̅r̅x̅ /= n; | |
s̅q̅r̅y̅ /= n; | |
x̅y̅ /= n; | |
let m = (x̅y̅ - x̅ * y̅) / (s̅q̅r̅x̅ - x̅ * x̅); | |
let sqr_r = (x̅y̅ - x̅ * y̅) * (x̅y̅ - x̅ * y̅) / ((s̅q̅r̅x̅ - x̅ * x̅) * (s̅q̅r̅y̅ - y̅ * y̅)); | |
println!("m: {}", m); | |
println!("R²: {}", sqr_r); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment