## rusfun

Little experimental crate to fit simple models to data via WASM in the browser

### 6 releases

 0.4.0 May 25, 2021 Nov 4, 2019 Aug 22, 2019 Aug 5, 2019

32KB
623 lines

# RUSFUN

The `rusfun` crate is a small library to compile parametrized functions from Rust to wasm. Furthermore it contains minimizer routines to find for a given set of data, parameters that minimize a cost function.

Currently the Levenberg-Marquardt algorithm is implemented to minimize

$\chi^2 = \sum_{i=1}^N \biggl( \frac{y_i - f(x_i)}{\sigma_i} \biggr)^2$

To define a function, a Func1D structs is defined, which contains as fields a reference to the initial parameters p, a reference to the domain x and a function, which maps p and x to the model values f(x). A few models are pre-defined in the standard, size_distribution and sas modules.

To initiate a Gaussian function for example one can do:

``````let p = array![300.0, 3.0, 0.2, 0.0];
let model = size_distribution::gaussian;

let model_function = func1d::Func1D::new(&p, &x, model);
``````

Note that p and x are ndarrays.

The function can then be evaluated by calling

``````model_function.output()
``````

To minimize a model for given data (xᵢ, yᵢ, σᵢ) with LM a Minimizer struct needs to be initialized as mutable variable, with the previously defined model_function, a reference to y and σ as ndarrays, as well as an initial ƛ value for the LM step.

``````let mut minimizer = curve_fit::Minimizer::init(&model_function, &y, &sy, 0.01);
``````

Then a fit can be performed by

``````minimizer.fit()
``````

and the result can be printed by

``````minimizer.report()
``````

So far the basic function of the rusfun crate. The crate is very young and the syntax might have breaking changes when more flexibility in choice for fitting algorithms are implemented.

~16MB
~312K SLoC