13
$\begingroup$

I defined a security bound for a random walk (cube with 15 length) and I have this code to obtain a sample of the random times at which the random walk crosses the boundary for the first time:

w = Do[i = 0; NestWhile[(i++; # + RandomReal[{-1, 1}, 3]) &, {0, 0, 0}, Norm[#] < 15 &];
       Sow[i], {1000}] // Reap 

(code taken from Sample of the random times at which the random walk crosses the boundary)

And I constructed a histogram with w:

histogram

And now I want to know the distribution of this data. I thought that it was a gamma because of the histogram, but I ran some tests in Excel and I concluded that it isn't, but I would like to confirm that with Mathematica. Is that possible?

And when I do this:

\[ScriptCapitalH] = DistributionFitTest[w[[2, 1]], Automatic, "HypothesisTestData"];

\[ScriptCapitalH]["FittedDistribution"]

I have for output: NormalDistribution[233.588, 150.231], but skewness[w] = 1.83 (if it was normally distributed, it should be approximately zero).

Would like some help please :)

$\endgroup$
7
  • $\begingroup$ @Szabolcs doesn't this question boil down to "How do I use DistributionFitTest to determine whether or not my data fit a gamma distribution?"? If so, then mma.se may be more appropriate than math. $\endgroup$ Mar 25, 2014 at 13:56
  • $\begingroup$ @bobthechemist You're right, I was focusing on "And now i want to know the distribution of this data". $\endgroup$
    – Szabolcs
    Mar 25, 2014 at 14:05
  • 1
    $\begingroup$ The documentation on this is not at all clear, but I don't think Automatic as the second argument does at all what you think. It seems to just assume a normal dist and the Automatic argument specifies the test type. (Can anyone cook up an example where Automatic returns anything other than NormalDistribution ) ? $\endgroup$
    – george2079
    Mar 25, 2014 at 15:55
  • 3
    $\begingroup$ Just for clarity, DistributionFitTest defaults to a test for normality. The fitted distribution is NOT the best fitting distribution. It is the best fitting distribution in the family you are testing. When setting the distribution family to Automatic you are using the normal family of distributions. $\endgroup$
    – Andy Ross
    Mar 25, 2014 at 18:32
  • $\begingroup$ thanks for help, didnt find that anywhere @Andy Ross, now i have my answer :) $\endgroup$ Mar 25, 2014 at 19:28

2 Answers 2

16
$\begingroup$

If you wanted to automate things you might do something like this:

 tests = Sort[(g = 
      DistributionFitTest[
           w, #, {"PValue", "FittedDistribution"}])  & /@  {
               GammaDistribution[a, b, c, d], 
               NormalDistribution[a, b],
               ChiSquareDistribution[a],
               HalfNormalDistribution[a],
               LogNormalDistribution[a, b]}]

 {{0., NormalDistribution[232.755, 156.711]},
 {7.09814*10^-19, ChiSquareDistribution[195.469]}, 
 {1.34007*10^-16, HalfNormalDistribution[0.00446664]}, 
 {0.066279,  GammaDistribution[6.16556, 6.45882, 0.548972, 33.4612]}, 
 {0.0966786, LogNormalDistribution[5.27028, 0.590685]}}

 GraphicsGrid[ 
    Partition[ Show[ {Histogram[w, Automatic, "ProbabilityDensity"],
                      Plot[PDF[#[[2]], x], {x, 0, 1500}, PlotStyle -> Thick, 
                 PlotPoints -> 1000 , PlotRange -> {0, 0.008}]}, 
                 PlotRange -> {0, 0.005}, 
                 PlotLabel -> Style[Head[#[[2]]], FontSize -> 10] ] & /@ tests  , 
           3 , 3 , {1, 1} , {}] ]

enter image description here

$\endgroup$
21
$\begingroup$

The mathematica help is very thorough and is very indicative of what you should do next. By way of the histogram diagram obtained, you can compare your data against the proposed distribution.

Show[Histogram[w[[2, 1]], Automatic, "ProbabilityDensity"], 
Plot[PDF[h["FittedDistribution"], x], {x, 0, 1500}, 
PlotStyle -> Thick]]

NormalFit

The reference points you to run the ProbabilityPlot function so you can see how well the curve fits.

ProbabilityPlot[w[[2, 1]], h["FittedDistribution"]]

ProbabilityPlot

Not that great.

By further exploring the parametric distributions available in Mathematica, all very well documented in the help , the Gamma distribution looks like a good candidate.

dist = GammaDistribution[α, β, γ, μ]
myFit = DistributionFitTest[w[[2, 1]], dist, "HypothesisTestData"]
Show[Histogram[w[[2, 1]], Automatic, "ProbabilityDensity"], 
Plot[PDF[myFit["FittedDistribution"], x], {x, 0, 1500}, 
PlotStyle -> Thick]]

This looks much more like it. GammaFit

ProbabilityPlot[w[[2, 1]], myFit["FittedDistribution"]]

enter image description here

$\endgroup$
2
  • $\begingroup$ thanks for help :D not exactly what i wanted but it was really helpfull. I dont understand is why the output for [ScriptCapitalH]["FittedDistribution"] is not Gammadistribution. And other question when i do KolmogorovSmirnovTest[w[[2, 1]]] mathematica gives me zero, do u have any ideia why this is happening? $\endgroup$ Mar 25, 2014 at 13:48
  • 1
    $\begingroup$ DEar @MarianadaCosta, I'm not privy to how the algorithms have been developed. It might be that Automatic just defaults to the most used distribution, namely, the Normal Distribution. I would recommend that you take a look a look at EDA (exploratory data analysis). A starting point en.wikipedia.org/wiki/Exploratory_data_analysis $\endgroup$
    – Zviovich
    Mar 25, 2014 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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