Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Fitting simulated data from a probability curve' topicStudent Support Forum > General > Archives > "Fitting simulated data from a probability curve"

Next Comment >Help | Reply To Topic
Author Comment/Response
Caitlin
08/08/12 2:39pm

I have been trying to generate "random" data using a probability distribution. However, when applying FindFit to the data, there is a large error between the fit curve and the original curve. Here is my code:

\[Tau] = 0.866; lower = 0; upper = 10; numberOfMeasurements = 1*^2; \
binWidth = 0.15;
P[t_] := Exp[-(t/\[Tau])];
Print["Establish a distribution function for the probability of \
survival using ProbabilityDistribution[]"]; probOfSurvival =
ProbabilityDistribution[-D[P[t], t], {t, lower, upper}];
Print["Generate simulated data"]; simulatedData =
Table[RandomVariate[
probOfSurvival], \
{numberOfMeasurements}];(*For[i=Length[simulatedData],i>0,i--,If[\
simulatedData[[i]]>upper,simulatedData=Delete[simulatedData,i]];If[\
simulatedData[[i]]<lower,simulatedData=Delete[simulatedData,i]]];*)\
Print[ListPlot[simulatedData, PlotRange -> All]];
Print["Compare histogram of simulated data to the probability of \
survival"]; histogramBins =
HistogramList[simulatedData, {binWidth}, "PDF"]; scale =
Max[histogramBins[[2]]]; Print[
Histogram[simulatedData, {binWidth}, "PDF",
Epilog ->
First@Plot[PDF[probOfSurvival, t], {t, lower, 1*^2},
PlotRange -> All, PlotStyle -> Red]]]; timeArray =
Table[(histogramBins[[1, j]] + histogramBins[[1, j + 1]])/2, {j, 1,
Length[histogramBins[[2]]]}]; scale =
Max[histogramBins[[2]]]/First[Max[P[t]]];
probArray =
Table[N[histogramBins[[2, j]]], {j, 1,
Length[histogramBins[[2]]]}]; For[l = Length[probArray], l > 0, l--,
If[probArray[[l]] <= 0, probArray = Delete[probArray, l]]]; For[
k = Length[probArray] - 1, k > 0, k--,
probArray[[k]] += probArray[[k + 1]]]; probArray =
probArray/Max[probArray]; data =
Table[{timeArray[[l]], probArray[[l]]}, {l, 1,
Length[probArray]}]; Print["List plot of sample data"]; Print[
ListPlot[data, PlotRange -> All]]; model = Exp[-(t/\[Tau]f)]; fit =
FindFit[data, model, \[Tau]f, t,
Method -> "Automatic"]; Print[fit]; modelf =
Function[{t},
model /. fit]; Print["Plot of sample Data and Curve fit"]; Print[
Show[Plot[modelf[t], {t, lower, upper}, PlotRange -> All],
ListPlot[data,
PlotRange -> {{lower, upper},
All}]]]; Print["Relative error in tau"]; Print[
Abs[\[Tau] - \[Tau]f]/(\[Tau]) /. fit];

URL: ,

Subject (listing for 'Fitting simulated data from a probability curve')
Author Date Posted
Fitting simulated data from a probability curve Caitlin 08/08/12 2:39pm
Re: Fitting simulated data from a probability c... toen 08/09/12 10:33am
Re: Fitting simulated data from a probability c... Michael 08/13/12 9:58pm
Next Comment >Help | Reply To Topic