Skip to content
Jonas Almeida edited this page Feb 7, 2025 · 26 revisions

Detailed instructions about the inner workings of fminsearch ES6 module can be found as notes in the code itself. Here we'll go through the steps of loading and using the module.

Dynamically Loading fminsearch ES6

mod = await import(`https://episphere.github.io/fminsearch/fminsearch.mjs`)

You'll see that mod includes the three exports, mod.fminsearch, mod.logistic and mod.plotly.

export{
	fminsearch,
	logistic,
	plotly
}

Correspondingly you'll find these three components in the mod variable: fminsearch is the non-linear regression core, logistic is a logistic regression function, as an example of how to load models from different loccations, and plotly is the popular graphics library that will be used to produce different types of visualizations. If you just want to load the fminsearch core, you can also do

fminsearch = (await import(`https://episphere.github.io/fminsearch/fminsearch.mjs`)).fminsearch

by the same logic, if you only want the logistic model you could do

logistic = (await import(`https://episphere.github.io/fminsearch/fminsearch.mjs`)).logistic

However, using your core fminsearch.mjs to store models looks messy, so better do that as a separate function module. You'll find a fun.mjs file where a catalog of models can be kept. For example,

logistic = (await import(`https://episphere.github.io/fminsearch/fun.mjs`)).logistic

You can, of course maintain your own catalog in your github repository.

Running fminsearch

We'll use the same example you find as notes in the fminsearch.mjs code.

x = [32,37,42,47,52,57,62,67,72,77,82,87,92];
y = [749,1525,1947,2201,2380,2537,2671,2758,2803,2943,3007,2979,2992];
fun = function(x,P){return x.map(function(xi){return (P[0]+1/(1/(P[1]*(xi-P[2]))+1/P[3]))})};
Parms=fminsearch(fun,[100,30,10,5000],x,y)

Going line by line, the first two lines provide the x and y arrays for a model of the type y=fun(x). That function, fun is defined in the third line directly but it could be picked from a function catalog, for example,

fun = (await import(`https://episphere.github.io/fminsearch/fun.mjs`)).rational

The 4th line can then be used to fit the parameters of the model by minimizing a objective cost function. The parameter values will be the output of the fminsearch iterated regression procedure:

Parms=fminsearch(fun,[100,30,10,5000],x,y)

... that is, when you run fminsearch, the initial parameter values, [100,30,10,5000] are regressed to [279.036,277.135,29.968,3272.459] in order to minimize Opt.ObjFun.

Parameterizing the regression

A number of configuration options are set by default, for example, the choice of sum of squared deviations as the cost function. These optional parameters settings are defined in the Opt object. Case in point, note this line in the code

if(!Opt.objFun){Opt.objFun=function(y,yp){return y.map(function(yi,i){return Math.pow((yi-yp[i]),2)}).reduce(function(a,b){return a+b})}} // SSD default objective function being minimized

Have a look at the block of code where you find that line for examples of other optional parameters, such as the default number of iterations or the initial parameter values, or indeed your own objective cost function, Opt.objFun.

Feedback

As you go through these notes, please feel welcome to ask questions, suggest new material or provide any other type of feedback at Issues.

Clone this wiki locally