Skip to main content
Determined correct definition of H2
Source Link
JimB
  • 42.9k
  • 3
  • 51
  • 108

Update: Here is the real H2 you want

rSample[nSamples_, lambda_, nUnder_, nBar_] :=
  If[# <= Exp[lambda (nUnder - nBar)], nUnder, (nBar*lambda + Log[#])/lambda] &
  /@ RandomReal[{0, 1}, nSamples]

H2[s_, lambda_, nUnder_, nBar_] := Piecewise[{{0, s < 2 nUnder},
 {E^((nUnder - nBar) 2 lambda), s == 2 nUnder},
 {E^((-2 nBar + s) lambda) (1 - 2 nUnder lambda + s lambda), 2 nUnder < s <= nUnder + nBar},
 {E^((-2 nBar + s) lambda) (1 + 2 nBar lambda - s lambda), nUnder + nBar < s <= 2 nBar}},
 1]

nBar = 4;
nUnder = 7/2;
lambda = 2;
ed = EmpiricalDistribution[
   rSample[10000, lambda, nUnder, nBar] + 
   rSample[10000, lambda, nUnder, nBar]];

Plot[{H2[s, lambda, nUnder, nBar], CDF[ed, s]}, {s, 2 nUnder - 0.5, 2 nBar + 0.5},
 PlotStyle -> {Thickness[0.02], Thickness[0.005]},
 PlotLegends -> {"The real H2", "Empirical distribution function\nof sum of two samples"}]

The real H2 and empirical distribution

Update: Here is the real H2 you want

rSample[nSamples_, lambda_, nUnder_, nBar_] :=
  If[# <= Exp[lambda (nUnder - nBar)], nUnder, (nBar*lambda + Log[#])/lambda] &
  /@ RandomReal[{0, 1}, nSamples]

H2[s_, lambda_, nUnder_, nBar_] := Piecewise[{{0, s < 2 nUnder},
 {E^((nUnder - nBar) 2 lambda), s == 2 nUnder},
 {E^((-2 nBar + s) lambda) (1 - 2 nUnder lambda + s lambda), 2 nUnder < s <= nUnder + nBar},
 {E^((-2 nBar + s) lambda) (1 + 2 nBar lambda - s lambda), nUnder + nBar < s <= 2 nBar}},
 1]

nBar = 4;
nUnder = 7/2;
lambda = 2;
ed = EmpiricalDistribution[
   rSample[10000, lambda, nUnder, nBar] + 
   rSample[10000, lambda, nUnder, nBar]];

Plot[{H2[s, lambda, nUnder, nBar], CDF[ed, s]}, {s, 2 nUnder - 0.5, 2 nBar + 0.5},
 PlotStyle -> {Thickness[0.02], Thickness[0.005]},
 PlotLegends -> {"The real H2", "Empirical distribution function\nof sum of two samples"}]

The real H2 and empirical distribution

Source Link
JimB
  • 42.9k
  • 3
  • 51
  • 108

It would seem sufficient to achieve the objective by obtaining the empirical distribution of a large number of samples of the sum of two independent observations and compare it to H2 rather than an empirical distribution built from samples from H2.

First define H for a single sample:

nBar = 4;
nUnder = 7/2;
lambda = 2;

H[s_] := Piecewise[{{0, s < nUnder}, {Exp[lambda (s - nBar)], nUnder <= s <= nBar},
  {1, s > nBar}}, 0]

Create a function to draw multiple samples from H and compare the empirical distribution from a large number of samples to H:

rSample[nSamples_, lambda_, nUnder_, nBar_] :=
 If[# <= Exp[lambda (nUnder - nBar)], nUnder, (nBar*lambda + Log[#])/lambda] &
   /@ RandomReal[{0, 1}, nSamples]

ed = EmpiricalDistribution[rSample[10000, lambda, nUnder, nBar]];
Plot[{H[s], CDF[ed, s]}, {s, nUnder - 1, nBar + 1},
 PlotStyle -> {Thickness[0.02], Thickness[0.005]},
 PlotLegends -> {"H", "Empirical distribution function"}]

True and empirical distribution functions

Visually things look just fine.

Now essentially do the same thing but with taking the sum of two samples and compare against H2:

H21[s_] := Exp[lambda*(s - 2*nBar)]*(1 + lambda*(nBar - nUnder))
H22[s_] := Exp[lambda*(s - 2*nBar)]*(1 - lambda*(s - 2*nBar))
H2[s_] := Piecewise[{{H21[s], 2*nUnder <= s < nUnder + nBar},
  {H22[s], nUnder + nBar <= s < 2*nBar}, {1, s >= 2*nBar}}, 0]


ed = EmpiricalDistribution[
   rSample[10000, lambda, nUnder, nBar] + 
   rSample[10000, lambda, nUnder, nBar]];
Plot[{H2[s], CDF[ed, s]}, {s, 2 nUnder - 1, 2 nBar + 1},
 PlotStyle -> {Thickness[0.02], Thickness[0.005]},
 PlotLegends -> {"H2", 
   "Empirical distribution function\nof sum of two samples"}]

H2 and empirical distribution

If the above steps are correct, it would seem that the definition of H2 needs work.