# on Kolmogorov-Smirnov test

I found a problem to calculate Kolmogorov-Smirnov test statistic or p-value the data is

R = {0.171434, 0.134263, 0.155931, 0.135479, 0.196356, 0.152357, 0.133084, 0.10537,
0.14654, 0.116676, 0.123145, 0.145377, 0.12366, 0.156681, 0.208564, 0.202139,
0.227931, 0.15622, 0.118042, 0.104006, 0.322, 0.327, 0.331, 0.34, 0.383, 0.454}


and the custom distribution is

custom1[a_, b_, α_, β_, θ_, λ_] =
ProbabilityDistribution[
a b α β θ λ^θ x^(θ - 1)
E^-(λ x)^β θ (1 - (1 - (1 - (1 - E^-(λ x)^θ)^α)^β )^a )^(b - 1)
(1 - (1 - (1 - E^-(λ x)^θ)^α)^β )^(a - 1) (1 - (1 - E^-(λ x)^θ^α)^(β - 1)
(1 - E^-(λ x)^θ)^(α - 1)),
{x, 0, \[Infinity]},
Assumptions ->
a > 0 &&
b > 0 && α > 0 && β > 0 && θ >
0 &&  λ > 0]


I found best parameters estimators are custom1[26.99, 0.17, 13, 3.58, 0.77, 45.11]

and the cdf is can any one help?
Thanks

• When parameters are estimated from the data a correction to the typical p-value is needed in order to maintain the size of the test. That correction is only added for some distributions and not in full generality. Nov 10, 2014 at 13:51
• Without other details I'm assuming you are also having difficulties getting this to compute. That is likely due to the complexity of the CDF which is needed for the KS test. Nov 10, 2014 at 13:58
• the cdf is F(x,a,b,[Alpha],[Beta],[Theta],[Lambda])=1[Minus]{1[Minus][1[Minus](1[Minus](1[Minus]e^[Minus]([Lambda] x)^[Theta])^^[Alpha])^[Beta]]^a}^b
– Momo
Nov 10, 2014 at 17:43
• still need help to solve this problem>>
– Momo
Nov 12, 2014 at 12:41
• I don't think you will be able to use the built in KS test with this distribution. You could easily construct your own Chi-Square test using this answer Nov 12, 2014 at 13:57

As I pointed out in the comments I don't believe you will be able to use built-in tests to compute this. The Kolmogorov-Smirnov test requires that you can compute the CDF of the distribution. Unfortunately, ProbabilityDistribution seems to convert to PDF even if you create it with the CDF. Then it reverts back to the definition for CDF when it tries to compute it which is really slow.

The best solution I can come up with is using a chi square test. Here is how I'd accomplish that.

R = {0.171434, 0.134263, 0.155931, 0.135479, 0.196356, 0.152357,
0.133084, 0.10537, 0.14654, 0.116676, 0.123145, 0.145377, 0.12366,
0.156681, 0.208564, 0.202139, 0.227931, 0.15622, 0.118042,
0.104006, 0.322, 0.327, 0.331, 0.34, 0.383, 0.454};

cdf[a_, b_, al_, be_, th_, la_][x_] :=
1 - (1 - (1 - (1 - (1 - Exp[-(la*x)^th])^al)^be)^a)^b


The chi square test bins the data and compares observed and expected bin counts. To do this we need a quantile function for your distribution.

quantile[a_, b_, al_, be_, th_, la_][q_] :=
x /. FindRoot[cdf[a, b, al, be, th, la][x] == q, {x, .1}]

Plot[quantile[26.99, 0.17, 13, 3.58, 0.77, 45.11][q], {q, 0, 1}] Now for the chi square test using information from my answer to this question.

chisq[data_, pars_] :=
Block[{n = Length[data], nbin, rng, bins, observed, expected, stat,
p},
nbin = Ceiling[2 n^(2/5)];
rng = Range[0, 1, 1/nbin];
bins = (quantile @@ pars) /@ rng[[2 ;; -2]];
bins = Join[{0}, bins, {\[Infinity]}];
observed = BinCounts[data, {bins}];
expected = ConstantArray[n/nbin, nbin];
stat = Total[(observed - expected)^2/expected];
p = SurvivalFunction[ChiSquareDistribution[nbin - 1 - Length[pars]],
stat];
{"p-val" -> p, "chi-sqr" -> stat} // N

]


The resulting p-value and test statistic for your data...

 chisq[R, {26.99, 0.17, 13, 3.58, 0.77, 45.11}]

(* {"p-val" -> 0.00362735, "chi-sqr" -> 8.46154} *)


This correctly accounts for estimated parameters in a way that just wouldn't be feasible with the KS test. However, your data set is sufficiently small that the counts in each bin are often very small as well. This means that the asymptotic chi-square distribution of the test statistic may be suspect.