13
$\begingroup$

In order to analyze simulated data, @risk provides a histogram with handle bars, so that we can move these handles to see what proportion of data are on each side. For example,

@risk histogram.

I am trying to create similar graph for my empirical data, but I have no idea how to do it. Any help starting with the following code would be much appreciated.

   mydata = RandomVariate[TriangularDistribution[{100, 400}, 200], 5000];
   Histogram[mydata, Automatic, "Probability"]
   mydist = EmpiricalDistribution[mydata];

histogram

$\endgroup$

2 Answers 2

10
$\begingroup$

A starting point:

BlockRandom[SeedRandom[42]; (* for reproducibility *)
            mydata = RandomVariate[TriangularDistribution[{100, 400}, 200], 5000]];

DynamicModule[{hd, hist, xmin, xmax, yr},
              Manipulate[Show[hist, 
                              Plot[PDF[hd, x], {x, h[[1, 1]], h[[2, 1]]},
                                   Exclusions -> None, Filling -> {1 -> Axis},
                                   FillingStyle -> Opacity[0.8, Pink],
                                   PlotPoints -> 95, PlotRange -> yr, 
                                   PlotStyle -> Directive[AbsoluteThickness[2], Pink]],
                              Axes -> None, Frame -> True,
                              GridLines -> {{h[[1, 1]], h[[2, 1]]}, None}, 
                              Method -> {"GridLinesInFront" -> True}, 
                              PlotLabel -> StringForm["``%",
                                                      100 Round[Abs[CDF[hd, h[[2, 1]]] -
                                                                    CDF[hd, h[[1, 1]]]],
                                                                0.001]]],
                         {{h, {{xmin, 0}, {xmax, 0}}}, Locator, Appearance -> None},
                         Initialization :> (hd = HistogramDistribution[mydata];
                                            {xmin, xmax} = First[hd["Domain"]]; 
                                            hist = Histogram[mydata, Automatic, "PDF", 
                                            ChartBaseStyle ->
                                            Directive[EdgeForm[], LightPink]];
                                            yr = Last[Charting`get2DPlotRange[hist]];)]]

interactive histogram

$\endgroup$
0
10
$\begingroup$

Try this:

With[{dist = CDF[mydist]},
 Manipulate[
  Histogram[mydata, Automatic, "Probability",
   PlotRange -> {MinMax@mydata, All},
   PlotLabel -> 
    StringForm["Percent Inside: ``" , 
     dist[bounds[[2]]] - dist[bounds[[1]]]],
   Epilog ->
    {
     EdgeForm[None],
     GrayLevel[1, .5],
     Rectangle[
      {Min@mydata, 0},
      {bounds[[1]], 1000000}
      ],
     Rectangle[
      {bounds[[2]], 0},
      {Max@mydata, 1000000}
      ]
     }
   ],
  {
   {bounds, Rescale[{.25, .75}, {0, 1}, MinMax@mydata]},
   Sequence @@ MinMax@mydata,
   ControlType -> IntervalSlider
   }
  ]
 ]

enter image description here

$\endgroup$
0

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.