Re: Problem with FindFit - No more memory available.
- To: mathgroup at smc.vnet.net
- Subject: [mg123638] Re: Problem with FindFit - No more memory available.
- From: Ray Koopman <koopman at sfu.ca>
- Date: Wed, 14 Dec 2011 05:59:49 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jc7app$p0g$1@smc.vnet.net>
On Dec 13, 2:52 am, Anne Beyer <abe... at stanford.edu> wrote:
> Hi,
>
> I'm trying to fit a model to data using FindFit. The model includes a
> FindRoot and NIntegrate function. When I run the program, I get the
> error message that "No more memory is available. Mathematica kernel
> has shut down." Do you know what I'm doing wrong? Any help is highly
> appreciated.
>
> Thanks,
> Anne
>
> << "MultivariateStatistics`"
>
> H[t_, m_, s_] := CDF[LogNormalDistribution[m, s], t]/
> PDF[LogNormalDistribution[m, s], t]
>
> r[t_, c_, m_, s_] := (c + t)/(c*H[t, m, s] + t*(c + t))
>
> t[c_?NumericQ, m_?NumericQ, s_?NumericQ, y_?NumericQ] := x /.
> FindRoot[r[x, c, m, s] == y, {x, 0.2}]
>
> U[t_?NumericQ, m_?NumericQ, s_?NumericQ, c_?NumericQ] :=
> NIntegrate[0.5*(c/(c*H[x, m, s] + x*(c + x)))^2, {x, t, Infinity}]
>
> w[c_?NumericQ, m_?NumericQ, s_?NumericQ, y_?NumericQ] := U[t[c, m, s,
> y], m, s, c] + (t[c, m, s, y]*c)/(c + t[c, m, s, y])*1/2*y^2
>
> "Data and estimation"
> data = {{0.2, 0.02}, {0.4, 0.05}, {0.6, 0.1}, {0.8, 0.15}, {0.3,
> 0.03}}
> model = w[c, m, s, y]
> estimatedw = FindFit[data, {model, c > 0, s > 0}, {{c, 1}, {m, 1},
> {s, 1}}, y]
> Show[ListPlot[data], Plot[estimatedw[r], {r, 0, 0.8}], Frame -> True]
This works fine for me (using v5.2), but I made several changes to
your code. Most notably, I eliminated the constraints in FindFit by
solving for the logs of c and s, and I defined estimatedw to be a
function of y.
<<Statistics`ContinuousDistributions`
H[t_,m_,s_] = CDF[LogNormalDistribution[m, s], t]/
PDF[LogNormalDistribution[m, s], t];
r[t_,c_,m_,s_] = (c+t)/(c*H[t,m,s] + t*(c+t));
t[c_?NumericQ, m_?NumericQ, s_?NumericQ, y_?NumericQ] :=
x /. FindRoot[r[x,c,m,s] == y, {x, .2}]
U[t_?NumericQ, m_?NumericQ, s_?NumericQ, c_?NumericQ] :=
NIntegrate[.5*(c/(c*H[x,m,s] + x*(c+x)))^2, {x, t, Infinity}]
w[c_?NumericQ, m_?NumericQ, s_?NumericQ, y_?NumericQ] :=
With[{x = t[c,m,s,y]}, U[x,m,s,c] + .5 y^2/(1/c + 1/x)]
data = {{.2, .02}, {.4, .05}, {.6, .1}, {.8, .15}, {.3, .03}};
model = w[E^logc, m, E^logs, y];
estimatedw[y_] = model /.
FindFit[data, model, {{logc,0},{m,1},{logs,0}}, y]
w[3.99091, -1.61356, 1.07757, y]
Plot[estimatedw[y], {y,0,.8}, Frame->True,
Prolog->{PointSize[.02],Point/@data}]