3
$\begingroup$

I have the following code, but I don't know how to use the NonlinearModelFit code to fit the data to plot the Gaussian curve:

Ph = 0.5; 
N10 = 10;
bincoin10 = ListPlot[Table[{Nh,10*10*Ph^(Nh)*(1 - Ph)^(N10 - Nh)*N10!/((Nh!*(10-Nh)!))}, {Nh,0,10}]]
$\endgroup$
1

3 Answers 3

4
$\begingroup$

I'm not sure why you would want to assume that the table of 100 times the probability mass function of a binomial distribution could be considered appropriate data for use in NonlinearModelFit. In other words the data really isn't in the form of

$$y=a*\exp[-(x-\mu)^2/v] + e$$

with $e\sim N(0,\sigma^2)$. You might consider the corresponding normal curve with the same mean and variance as the binomial:

x = Range[0, 10];
z = Range[0, 100]/10;
ListPlot[{Transpose[{x, 100 PDF[BinomialDistribution[N10, Ph], x]}],
  Transpose[{z, 100 PDF[NormalDistribution[N10 Ph, (N10 Ph (1 - Ph))^0.5], z]}]},
 PlotStyle -> {Red, Green}, Joined -> {False, True},
 PlotLegends -> {"100*Binomial[N10,Ph]",  "100*Corresponding normal density"}]

Normal and binomial pdf times 100

$\endgroup$
2
  • $\begingroup$ Thank you, The purpose of 100 was to adjust the Y axis to show the expected values rather than their probabilities. $\endgroup$
    – Shawn
    Sep 23, 2016 at 22:08
  • $\begingroup$ Ok. But the expected value for a count of x is n*N10*Pr(X=x) where n is the number of times that you sample from a binomial with parameters N10 and Ph. So you have n=10. In other words without such an explicit definition it appears that the number 100 that you use comes out of nowhere. $\endgroup$
    – JimB
    Sep 23, 2016 at 22:46
3
$\begingroup$

You should define the bincoin10 as a Table, rather than ListPlot.

bincoin10 = 
  Table[{Nh, 
    10*10*Ph^(Nh)*(1 - Ph)^(N10 - Nh)*N10!/((Nh!*(10 - Nh)!))}, {Nh, 
    0, 10}];

Then you can simply use NonlinearModelFit with self-defined Gaussian function.

gauss = A 1/(σ Sqrt[2. π]) Exp[-(1./2.) ((x - μ)/σ)^2];
nlm = NonlinearModelFit[bincoin10, gauss, {{σ, 1.}, {μ, 0}, {A, 25}}, x]
Show[ListPlot[bincoin10, PlotStyle -> Red, PlotRange -> All], 
 Plot[nlm[x], {x, 0, 2000}, PlotRange -> All]]

enter image description here

$\endgroup$
2
$\begingroup$

Try this:

Ph = 0.5;
N10 = 10;
bincoin10 = 
 ListPlot[Table[{Nh, 
    10*10*Ph^(Nh)*(1 - Ph)^(N10 - Nh)*N10!/((Nh!*(10 - Nh)!))}, {Nh, 
    0, 10}]]

data = Table[{Nh, 
10*10*Ph^(Nh)*(1 - Ph)^(N10 - Nh)*N10!/((Nh!*(10 - Nh)!))}, {Nh, 
0, 10}];

eq = Fit[data, {1, x, x^2, x^3, x^4}, x]

$0.0655594 x^4-1.31119 x^3+7.3208 x^2-7.6486 x+0.699301$

Plot[eq, {x, 0, 10}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.