Giter Site home page Giter Site logo

emilinya / omegaoptimizer Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 805 KB

The ultimate parameter optimizer (non-linear regression program)

License: GNU General Public License v3.0

Python 3.05% Rust 96.95%
backtracking gradient-descent gui newton-raphson non-linear-regression regression rust

omegaoptimizer's Introduction

GNU GPLv3 License Test TopOpt

Omega Optimizer

Omega Optimizer is a program that finds the parameters such that a parametrized function best matches some data. It uses a gui where you can see your data, choose from several parameterized functions, run the optimizer, and save the result as a figure. I have written a blog post on how this program works, which you can read on my website.

Usage

The program is run using cargo run --release -- <datafile>, where <datafile> is the path to a plain text file with x-values in the first column and y-values in the second column, with the values separated by a space. The program can also be ran without the gui using the -f flag. In this case, you must also provide a function. To print a list of available functions, use the -p flag. For more options, see the -h flag.

Adding a new function

Omega Optimizer currently has 6 functions to choose from. If none of them matches your dataset, you can easily add a new function by following these steps:

  1. Calculate the gradient and hessian of your function with respect to its parameters.
  2. Create a new rust file in src/functions where you create a new struct named after your function.
  3. Derive the Differentiated<D> trait for your struct, where D is the number of parameters.
  4. In src/functions/mod.rs, you will find an evocation of the create_function_enum macro. There, add a new line of the form filename::StructName<D>.

And now your function should be available as an option in the function list. To ensure you have implemented the gradient and hessian correctly, simply run cargo test, which tells you all indices that are implemented incorrectly.

For example, let's implement an exponential decay given by $f(x; a, \lambda) = ae^{-\lambda x}$. We first calculate the gradient and hessian:

$$\nabla f = \begin{bmatrix} 1 \\ -ax \end{bmatrix}e^{-\lambda x}, \ \\ \mathbf{H}f = \begin{bmatrix} 0 & -x \\ -x & ax^2 \end{bmatrix}e^{-\lambda x}.$$

We then create the file src/functions/decay.rs, where we define the Decay struct and implement Differentiated<2> for it, giving us:

use nalgebra::{Matrix2, Vector2};

use super::Differentiated;

pub struct Decay;

impl Differentiated<2> for Decay {
    const PARAMETER_NAMES: [&'static str; 2] = ["a", "λ"];
    const NAME: &'static str = "decay";

    fn f(x: f64, params: &Vector2<f64>) -> f64 {
        let (a, l) = (params.x, params.y);
        a * (-l * x).exp()
    }

    fn grad(x: f64, params: &Vector2<f64>) -> Vector2<f64> {
        let (a, l) = (params.x, params.y);
        let exp = (-l * x).exp();
        Vector2::new(1.0, -a * x) * exp
    }

    fn hess(x: f64, params: &Vector2<f64>) -> Matrix2<f64> {
        let (a, l) = (params.x, params.y);
        let exp = (-l * x).exp();
        Matrix2::new(0.0, -x, -x, a * x * x) * exp
    }
}

We then add decay::Decay<2> to create_function_enum, and now the function is available through the gui:

omegaoptimizer's People

Contributors

emilinya avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.