I am trying to integrate the difference in a stepwise function and a continuous function, but the value I am getting is as if the step-wise function were identically zero, which I know it is not. Here is my code
NumLessThan[x_, data_] := (i = 1;
For[i := 1, x > Sort[data][[i]] && i <= Length[data], ,If[++i > Length[data], Break[]]]; i - 1)
CDFDisc[x_, data_] := NumLessThan[x, Sort[data]]/Length[data]
data =
RandomVariate[NormalDistribution[],
2]; Plot[{CDF[NormalDistribution[], x], CDFDisc[x, data]}, {x, -5,
4}]
NIntegrate[
Abs[CDF[NormalDistribution[], x] - CDFDisc[x, data]], {x, -10, 10}]
(* 10. *)
Plot[
Abs[CDF[NormalDistribution[], x] - CDFDisc[x, data]], {x, -10,
10}, PlotRange -> Full]
NIntegrate[CDF[NormalDistribution[], x], {x, -10, 10}]
(* 10. *)
I am aware that my discrete CDF function is algorithmically terrible, but I am just trying to get it to work before I make it computationally more optimal. The function CDFDisc
outputs the correct values. And when I plot the integrand, I get exactly what I would expect. The mystery is why, inside NIntegrate
it is treating CDFDisc
as if it were zero everywhere.
CDF[EmpiricalDistribution[data], x]
$\endgroup$NIntegrate
thinks myCDFDisc
is zero, and I can't figure out why. In addition to getting this specific example working I want to get an understanding of how what I did is wrong. $\endgroup$x_?NumericQ
to force a numerical only interpretation. $\endgroup$