# How to combine an if statement with Statistics

Bellow is a code which attempts to change the sign (+/-) of S with an if statement. S1 is shown and S2 is the attempt to include the if statement.

You can see from the information yielded from S1 that the Gaussian fit is off by roughly a factor of 2 when it should in theory match. This is because it is overestimating positive events due to the lack of the sign change. The sign change should change with the quantity I have assigned the label "Signal". When the Signal is positive S2 should be positive and when Signal is negative S2 should become negative.

off = 140;
α = .3;
Non = RandomVariate[PoissonDistribution[α*off], 100000];
Noff = RandomVariate[PoissonDistribution[off], 100000];

h2 = Plot[
Evaluate@
Table[PDF[NormalDistribution[0, σ], x], {σ, 1}], {x, -6, 6},
PlotStyle -> Red];

S[off_, α_] := Non - α*Noff;
hist = Histogram[S[off, α], Automatic, "ProbabilityDensity",
PlotLabel -> "Signal"]

S1[off_, α_] := .5 Sqrt[2] (Non*Log[(1 + α)/α (Non/(Non + Noff))] +
Noff*Log[(1 + α) (Noff/(Non + Noff))])^(1/2);

ListPlot[S1[off, α], PlotLabel -> "Formula 17",
PlotRange -> {{0, 100000}, {0, 8}}]
hist1 = Histogram[S1[off, α], "Log", "ProbabilityDensity",
PlotLabel -> "Formula 17 LOG"]
hist11 = Histogram[S1[off, α], Automatic, "ProbabilityDensity",
PlotLabel -> "Formula 17"]
Show[hist11, h2]
ProbabilityScalePlot[S1[off, α], "Normal",
PlotLabel -> "Formula 17"]

S2[off_, α_] :=
If[Non - α*Noff > 0,
Sqrt[2] (Non*Log[(1 + α)/α (Non/(Non + Noff))] +
Noff*Log[(1 + α) (Noff/(Non + Noff))])^(1/2),
-Sqrt[2] (Non*Log[(1 + α)/α (Non/(Non + Noff))] +
Noff*Log[(1 + α) (Noff/(Non + Noff))])^(1/2)];

ListPlot[S2[off, α], PlotLabel -> "Formula 17"]
hist2 = Histogram[S2[off, α], "Log", "ProbabilityDensity",
PlotLabel -> "Formula 17 LOG"]
hist22 = Histogram[S2[off, α], Automatic, "ProbabilityDensity",
PlotLabel -> "Formula 17"]
Show[hist22, h2]
ProbabilityScalePlot[S2[off, α], "Normal",
PlotLabel -> "Formula 17"]

-
Am I wrong or the distribution Non should be RandomVariate[PoissonDistribution[α*off], 100000] instead of RandomVariate[PoissonDistribution[α]*off], 100000]. Are you missing something or just a typo ? –  Sektor Jul 16 at 14:18
I just looked and I think it does read RandomVariate[PoissonDistribution[α*off], 100000] –  William John-Pierre Duhe Jul 16 at 16:43