Something like this, perhaps:
Here’s how I did it:
Cobyla minimises a non-linear objective function subject to constraints.
- The demo and its components (including cobyla) use the module pattern, which has the advantage of keeping the global namespace uncluttered.
- To adapt Cobyla to this curve fitting problem, I wrote a short wrapper which is added onto the
cobyla.nlFit(data, fitFn, start, min, max, constraints, solverParams). This function minimises the sum of squared differences
(y1^2-y2^2)between the data points,
(x,y1), and the fitted points,
- The Weibull cumulative distribution function (CDF), inverse CDF and mean are defined in the “distribution” module. Thus
distribution.weibull([2,1,5]) .inverseCdf(0.5)gives the median (50th percentile) of a Weibull distribution with shape parameter 2, scale parameter 1 and location parameter 5.
- The chart is built with d3. I am building an open-source library of a few useful chart types, d3elements, which are added onto the d3 module as
d3.elts. This one is called
- I apologise in advance that this sample code is quite complicated. If you see ways to make it simpler, please let me know.
- Finally, this may be obvious, but I like the rigour that a tool like jshint imposes. Hence the odd comment at the top of
/* global fitdemo: true, jQuery, d3, _, distribution, cobyla */
Check out the source code on bitbucket here.
Please let me know what you think!