I am trying to find a fit to the cumulative distribution of a set of points using FindFit or NMinimize.
In particular, I would like to find the parameters of the cdf of the Beta Distribution that would minimize the uniform distance to the cumulative distribution of the above mentionned points.
So in particular, I am avoiding the use of NonLinearModelFit for reasons outlined here What is the difference between FindFit and NonlinearModelFit
However I get errors. I would appreciate a lot if someone could have a look at my code and let me know what am I missing.. (I tried a bunch of solutions neither works..)
So, here is the empirical cumulative probability function (in my problem it is a function derived from a non-parameteric kernel) but let us cook it up as a piecewise function:
piece[x_] := Piecewise[{{x^3, 1 >= x >= 0}, {1, x > 1}}, 0]
Now I generate my "model" whose parameters I want to determine
funcr[al_?NumericQ, be_?NumericQ, x_?NumericQ] :=
CDF[BetaDistribution[al, be], x];
So I can define first a norm which I will be later minimizing with respect to parameters $al$ and $be$ of the beta distribution
norm[al_, be_, x_] := Abs[funcr[al, be, x] - piece[x]];
max[al_, be_] := ArgMax[{norm[al, be, x], 0 <= x <= 1}, x]
And then I am trying to minimize this max function over all $\alpha$ and $\beta$ of the beta distribution (as given here by al_ and be_).
NMinimize[{max[al, be], al >= 0, be >= 0}, {al, be}]
And at this stage I get the error:
*The function value
\Abs[-0.028069671808523725`+funcr[al,be,0.04573179628771076`]] is not \
a number at {x} = {0.04573179628771076`}. >>*
I know I am very pedestrian with my code, but I would really appreciate all your suggestions from which I can learn how to be a bit more sophisticated and especially correct....!
Answer
You could do for example:
int[al_?NumericQ, be_?NumericQ] := NIntegrate[(funcr[al, be, x] - piece[x])^2, {x, 0, 1}]
nm = NMinimize[{int[al, be], al >= 1, be >= 1}, {al, be}]
Plot[{piece@x, funcr[al, be, x] /. nm[[2]]}, {x, 0, 1},
PlotStyle -> {{Thickness[.01], Red}, {Dashed, Thickness[.01], Blue}}]
Comments
Post a Comment