ClearAll[tF]
SeedRandom[10]
n = 100;
sample = Sort[Sin[Pi*RandomVariate[UniformDistribution[{0, 1}], n]]];
dist = EmpiricalDistribution[sample];
Use tF
to define a custom ProbabilityDistribution
:
tF[y_] := 2/Pi*ArcSin[y]
tfdist = ProbabilityDistribution[{"CDF", tF[x]}, {x, 0, 1}];
Row[Plot[Evaluate[#[tfdist, x]], {x, 0, 1}, Filling -> Axis,
ImageSize -> 400, PlotLabel -> Style[#, 16]] & /@ {PDF, CDF}]
KolmogorovSmirnovTest
is based on the maximum difference between CDF
s of two distributions.
ksts = KolmogorovSmirnovTest[sample, tfdist, "TestStatistic"]
0.169802
f1 = CDF[dist, #] &;
f2 = CDF[tfdist, #] &;
MaxValue[{Abs[f1[x] - f2[x]], -1 < x < 1}, {x}]
0.169802
xv = ArgMax[{Abs[f1[x] - f2[x]], 0 < x < 1}, {x}][[1]];
yv = Through@{f1[#] - f2[#] &, f1, f2}@xv;
points = Thread[{xv, yv}];
tsttable = Style[#, "Panel", 16]&@@ KolmogorovSmirnovTest[sample, tfdist, "TestDataTable"];
legends = (Style[#, "Panel", 16] & /@ {"empirical dist", "tfdist", "difference"});
Plot[{f1[x], f2[x], f1[x] - f2[x]}, {x, 0, 1}, Exclusions -> None,
PlotRange -> All, PlotStyle -> Thick, PlotLegends -> legends, AspectRatio->1,
ImageSize -> 500, LabelStyle -> {"Panel", 16}, PlotLabel -> tsttable,
GridLines -> ({{xv}, yv}), Ticks -> {Automatic, yv},
Prolog -> {Thickness[.025], Opacity[.9], Green, CapForm["Butt"],
Line[Rest@points], Line[{First@points, {xv, 0}}], PointSize[Large], Red, Point[points]}]
ProbabilityPlot: Empirical CDF values versus theoretical CDF values:
ProbabilityPlot[sample, tfdist, Joined -> True,
ReferenceLineStyle -> Directive[Thick, Dashing[None], Red],
PlotStyle -> Directive[Thick, Blue], ImageSize -> 400, LabelStyle -> {"Panel", 16},
FrameTicks -> {Join[{0}, Round[yv[[{2, 3}]], .001], {1}],
Join[{0}, Round[yv[[{2, 3}]], .001], {1}], Automatic, Automatic},
AspectRatio -> 1, GridLines -> ({yv[[{2, 3}]], yv[[{2, 3}]]}),
Filling -> {1 -> {2}}, PlotLabel -> Style["P-P Plot", 18],
FrameLabel -> { "Theoretical CDF values", "Empirical CDF values"},
Epilog -> {Text[Style["\[CapitalDelta] = " <>
ToString[Round[Subtract @@ yv[[{2, 3}]], .0001]], 16, "Panel"],
{yv[[3]]/2, Mean@yv[[{3, 2}]]}],
Purple, Arrowheads[{-.05, .05}] , Thickness[.01],
Arrow@ Line[{yv[[{3, 2}]], yv[[{3, 3}]]}]}]
See also: What is the formula behind the Kolmogorov-Smirnov test statistics?