Skip to content

Commit

Permalink
FIX: Fix example of optimize (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
Axect committed Apr 1, 2021
1 parent e356aa8 commit b8f5c5d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
39 changes: 39 additions & 0 deletions examples/optim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#[macro_use]
extern crate peroxide;
use peroxide::fuga::*;

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
}

fn quad(x: &Vec<f64>, n: Vec<AD>) -> Option<Vec<AD>> {
Some(
x.clone().into_iter()
.map(|t| AD1(t, 0f64))
.map(|t| t.pow(n[0]))
.collect()
)
}
6 changes: 2 additions & 4 deletions src/numerical/optimize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,10 @@
//! p.print(); // Optimized parameter
//! opt.get_error().print(); // Optimized RMSE
//!
//! // To prepare plotting
//! let z = quad(&x, p.to_ad_vec()).unwrap().to_f64_vec();
//!
//! // Plot
//! //#[cfg(feature = "plot")]
//! //{
//! // let z = quad(&x, p.to_ad_vec()).unwrap().to_f64_vec();
//! // let mut plt = Plot2D::new();
//! // plt.set_domain(x)
//! // .insert_image(y) // plot data
Expand All @@ -94,7 +92,7 @@
//! fn quad(x: &Vec<f64>, n: Vec<AD>) -> Option<Vec<AD>> {
//! Some(
//! x.clone().into_iter()
//! .map(|t| AD::from(t))
//! .map(|t| AD1(t, 0f64))
//! .map(|t| t.pow(n[0]))
//! .collect()
//! )
Expand Down

0 comments on commit b8f5c5d

Please sign in to comment.