[][src]Module peroxide::numerical::optimize

To optimize parametric model (non-linear regression)

Optimizer structure

Declaration

extern crate peroxide;
use peroxide::{Number, OptMethod, OptOption};
use std::collections::HashMap;

pub struct Optimizer<F>
where F: Fn(&Vec<f64>, Vec<Number>) -> Option<Vec<Number>> {
    domain: Vec<f64>,
    observed: Vec<f64>,
    func: Box<F>,
    param: Vec<Number>,
    max_iter: usize,
    error: f64,
    method: OptMethod,
    option: HashMap<OptOption, bool>,
}

Method (Should fill)

Method (Optional)

Method (Generate result)

Example

extern crate peroxide;
use peroxide::*;

fn main() {
    // To prepare noise
    let normal = Normal(0f64, 0.1f64);
    let normal2 = Normal(0f64, 100f64);

    // Noise to domain
    let mut x = seq(0., 99., 1f64);
    x = zip_with(|a, b| (a + b).abs(), &x, &normal.sample(x.len()));

    // Noise to image
    let mut y = x.fmap(|t| t.powi(2));
    y = zip_with(|a, b| a + b, &y, &normal2.sample(y.len()));

    // Initial parameter
    let n_init = vec![1f64];
    let data = hstack!(x.clone(), y.clone());

    // Optimizer setting
    let mut opt = Optimizer::new(data, quad);
    let p = opt.set_init_param(n_init)
        .set_max_iter(50)
        .set_method(LevenbergMarquardt)
        .optimize();
    p.print();                  // Optimized parameter
    opt.get_error().print();    // Optimized RMSE

    // To prepare plotting
    let z = quad(&x, NumberVector::from_f64_vec(p)).unwrap().to_f64_vec();

    // Plot
    //#[cfg(feature = "plot")]
    //{
    //    let mut plt = Plot2D::new();
    //    plt.set_domain(x)
    //        .insert_image(y)    // plot data
    //        .insert_image(z)    // plot fit
    //        .set_legend(vec!["Data", "Fit"])
    //        .set_title("Test ($y=x^2$ with noise)")
    //        .set_path("example_data/lm_test.png")
    //        .set_marker(vec![Point, Line])
    //        .savefig().expect("Can't draw a plot");
    //}
}

fn quad(x: &Vec<f64>, n: Vec<Number>) -> Option<Vec<Number>> {
    Some(
        x.clone().into_iter()
            .map(|t| Number::from_f64(t))
            .map(|t| t.powf(n[0]))
            .collect()
    )
}

LM test

Re-exports

pub use self::OptMethod::GaussNewton;
pub use self::OptMethod::GradientDescent;
pub use self::OptMethod::LevenbergMarquardt;

Structs

Optimizer

Optimizer for optimization (non-linear regression)

Enums

OptMethod
OptOption