Fitting an Exponential
Overview
Often you have a data series that likely comes from an exponential curve, possibly with some noise. At times, you need to smooth
the data, often because you need the curve to monotonic. One way to construct the curve is to fit an exponential curve to the
data. Usually this is done just using
ordinary least squares regression.
Fitting the Data
For exponential data, we are assuming a relationship such as the following between the independent and dependent variables.
{% y_i = \alpha \times e ^{\beta \times x_i} %}
In order to use a linear regression, we need to recast the equation as a linear equation.
{% ln(y_i) = ln(\alpha) + \beta \times ln(x_i) %}
The following code demonstrates running a simple regression on an arbitrary dataset using
the
ols linear regression library
let data = [0.466792485,0.166153805,0.059623922,0.041566325,0.016447223,0.001183964,0.000425231,0.00019861]
let y = data.map(p=>[Math.log(p)]);
//include the intercept
let x = $range(y.length).map(p=>[1,p]);
//load the libraries
let olsregression = await import('/lib/ols-regression/v1.0.0/ols-regression.mjs');
//run the regression
let reg = olsregression.regress(y, x);
let coef = reg.Coefficients;
Try it!