commons-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r959801 [9/10] - in /websites/production/commons/content/proper/commons-math/apidocs: ./ org/apache/commons/math3/exception/class-use/ org/apache/commons/math3/fraction/ org/apache/commons/math3/geometry/euclidean/oned/class-use/ org/apache...
Date Mon, 27 Jul 2015 19:40:06 GMT
Modified: websites/production/commons/content/proper/commons-math/apidocs/src-html/org/apache/commons/math3/stat/inference/KolmogorovSmirnovTest.html
==============================================================================
--- websites/production/commons/content/proper/commons-math/apidocs/src-html/org/apache/commons/math3/stat/inference/KolmogorovSmirnovTest.html (original)
+++ websites/production/commons/content/proper/commons-math/apidocs/src-html/org/apache/commons/math3/stat/inference/KolmogorovSmirnovTest.html Mon Jul 27 19:40:06 2015
@@ -29,1012 +29,994 @@
 <span class="sourceLineNo">021</span>import java.util.Arrays;<a name="line.21"></a>
 <span class="sourceLineNo">022</span>import java.util.Iterator;<a name="line.22"></a>
 <span class="sourceLineNo">023</span><a name="line.23"></a>
-<span class="sourceLineNo">024</span>import org.apache.commons.math3.distribution.RealDistribution;<a name="line.24"></a>
-<span class="sourceLineNo">025</span>import org.apache.commons.math3.exception.InsufficientDataException;<a name="line.25"></a>
-<span class="sourceLineNo">026</span>import org.apache.commons.math3.exception.MathArithmeticException;<a name="line.26"></a>
-<span class="sourceLineNo">027</span>import org.apache.commons.math3.exception.NullArgumentException;<a name="line.27"></a>
-<span class="sourceLineNo">028</span>import org.apache.commons.math3.exception.NumberIsTooLargeException;<a name="line.28"></a>
-<span class="sourceLineNo">029</span>import org.apache.commons.math3.exception.OutOfRangeException;<a name="line.29"></a>
-<span class="sourceLineNo">030</span>import org.apache.commons.math3.exception.TooManyIterationsException;<a name="line.30"></a>
-<span class="sourceLineNo">031</span>import org.apache.commons.math3.exception.util.LocalizedFormats;<a name="line.31"></a>
-<span class="sourceLineNo">032</span>import org.apache.commons.math3.fraction.BigFraction;<a name="line.32"></a>
-<span class="sourceLineNo">033</span>import org.apache.commons.math3.fraction.BigFractionField;<a name="line.33"></a>
-<span class="sourceLineNo">034</span>import org.apache.commons.math3.fraction.FractionConversionException;<a name="line.34"></a>
-<span class="sourceLineNo">035</span>import org.apache.commons.math3.linear.Array2DRowFieldMatrix;<a name="line.35"></a>
-<span class="sourceLineNo">036</span>import org.apache.commons.math3.linear.FieldMatrix;<a name="line.36"></a>
-<span class="sourceLineNo">037</span>import org.apache.commons.math3.linear.MatrixUtils;<a name="line.37"></a>
-<span class="sourceLineNo">038</span>import org.apache.commons.math3.linear.RealMatrix;<a name="line.38"></a>
-<span class="sourceLineNo">039</span>import org.apache.commons.math3.random.RandomGenerator;<a name="line.39"></a>
-<span class="sourceLineNo">040</span>import org.apache.commons.math3.random.Well19937c;<a name="line.40"></a>
-<span class="sourceLineNo">041</span>import org.apache.commons.math3.util.CombinatoricsUtils;<a name="line.41"></a>
-<span class="sourceLineNo">042</span>import org.apache.commons.math3.util.FastMath;<a name="line.42"></a>
-<span class="sourceLineNo">043</span>import org.apache.commons.math3.util.MathArrays;<a name="line.43"></a>
-<span class="sourceLineNo">044</span><a name="line.44"></a>
-<span class="sourceLineNo">045</span>import static org.apache.commons.math3.util.MathUtils.PI_SQUARED;<a name="line.45"></a>
-<span class="sourceLineNo">046</span>import static org.apache.commons.math3.util.FastMath.PI;<a name="line.46"></a>
-<span class="sourceLineNo">047</span><a name="line.47"></a>
-<span class="sourceLineNo">048</span>/**<a name="line.48"></a>
-<span class="sourceLineNo">049</span> * Implementation of the &lt;a href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"&gt;<a name="line.49"></a>
-<span class="sourceLineNo">050</span> * Kolmogorov-Smirnov (K-S) test&lt;/a&gt; for equality of continuous distributions.<a name="line.50"></a>
-<span class="sourceLineNo">051</span> * &lt;p&gt;<a name="line.51"></a>
-<span class="sourceLineNo">052</span> * The K-S test uses a statistic based on the maximum deviation of the empirical distribution of<a name="line.52"></a>
-<span class="sourceLineNo">053</span> * sample data points from the distribution expected under the null hypothesis. For one-sample tests<a name="line.53"></a>
-<span class="sourceLineNo">054</span> * evaluating the null hypothesis that a set of sample data points follow a given distribution, the<a name="line.54"></a>
-<span class="sourceLineNo">055</span> * test statistic is \(D_n=\sup_x |F_n(x)-F(x)|\), where \(F\) is the expected distribution and<a name="line.55"></a>
-<span class="sourceLineNo">056</span> * \(F_n\) is the empirical distribution of the \(n\) sample data points. The distribution of<a name="line.56"></a>
-<span class="sourceLineNo">057</span> * \(D_n\) is estimated using a method based on [1] with certain quick decisions for extreme values<a name="line.57"></a>
-<span class="sourceLineNo">058</span> * given in [2].<a name="line.58"></a>
-<span class="sourceLineNo">059</span> * &lt;/p&gt;<a name="line.59"></a>
-<span class="sourceLineNo">060</span> * &lt;p&gt;<a name="line.60"></a>
-<span class="sourceLineNo">061</span> * Two-sample tests are also supported, evaluating the null hypothesis that the two samples<a name="line.61"></a>
-<span class="sourceLineNo">062</span> * {@code x} and {@code y} come from the same underlying distribution. In this case, the test<a name="line.62"></a>
-<span class="sourceLineNo">063</span> * statistic is \(D_{n,m}=\sup_t | F_n(t)-F_m(t)|\) where \(n\) is the length of {@code x}, \(m\) is<a name="line.63"></a>
-<span class="sourceLineNo">064</span> * the length of {@code y}, \(F_n\) is the empirical distribution that puts mass \(1/n\) at each of<a name="line.64"></a>
-<span class="sourceLineNo">065</span> * the values in {@code x} and \(F_m\) is the empirical distribution of the {@code y} values. The<a name="line.65"></a>
-<span class="sourceLineNo">066</span> * default 2-sample test method, {@link #kolmogorovSmirnovTest(double[], double[])} works as<a name="line.66"></a>
-<span class="sourceLineNo">067</span> * follows:<a name="line.67"></a>
-<span class="sourceLineNo">068</span> * &lt;ul&gt;<a name="line.68"></a>
-<span class="sourceLineNo">069</span> * &lt;li&gt;For very small samples (where the product of the sample sizes is less than<a name="line.69"></a>
-<span class="sourceLineNo">070</span> * {@value #SMALL_SAMPLE_PRODUCT}), the exact distribution is used to compute the p-value for the<a name="line.70"></a>
-<span class="sourceLineNo">071</span> * 2-sample test.&lt;/li&gt;<a name="line.71"></a>
-<span class="sourceLineNo">072</span> * &lt;li&gt;For mid-size samples (product of sample sizes greater than or equal to<a name="line.72"></a>
-<span class="sourceLineNo">073</span> * {@value #SMALL_SAMPLE_PRODUCT} but less than {@value #LARGE_SAMPLE_PRODUCT}), Monte Carlo<a name="line.73"></a>
-<span class="sourceLineNo">074</span> * simulation is used to compute the p-value. The simulation randomly generates partitions of \(m +<a name="line.74"></a>
-<span class="sourceLineNo">075</span> * n\) into an \(m\)-set and an \(n\)-set and reports the proportion that give \(D\) values<a name="line.75"></a>
-<span class="sourceLineNo">076</span> * exceeding the observed value.&lt;/li&gt;<a name="line.76"></a>
-<span class="sourceLineNo">077</span> * &lt;li&gt;When the product of the sample sizes exceeds {@value #LARGE_SAMPLE_PRODUCT}, the asymptotic<a name="line.77"></a>
-<span class="sourceLineNo">078</span> * distribution of \(D_{n,m}\) is used. See {@link #approximateP(double, int, int)} for details on<a name="line.78"></a>
-<span class="sourceLineNo">079</span> * the approximation.&lt;/li&gt;<a name="line.79"></a>
-<span class="sourceLineNo">080</span> * &lt;/ul&gt;<a name="line.80"></a>
-<span class="sourceLineNo">081</span> * &lt;/p&gt;<a name="line.81"></a>
-<span class="sourceLineNo">082</span> * &lt;p&gt;<a name="line.82"></a>
-<span class="sourceLineNo">083</span> * In the two-sample case, \(D_{n,m}\) has a discrete distribution. This makes the p-value<a name="line.83"></a>
-<span class="sourceLineNo">084</span> * associated with the null hypothesis \(H_0 : D_{n,m} \ge d \) differ from \(H_0 : D_{n,m} &gt; d \)<a name="line.84"></a>
-<span class="sourceLineNo">085</span> * by the mass of the observed value \(d\). To distinguish these, the two-sample tests use a boolean<a name="line.85"></a>
-<span class="sourceLineNo">086</span> * {@code strict} parameter. This parameter is ignored for large samples.<a name="line.86"></a>
-<span class="sourceLineNo">087</span> * &lt;/p&gt;<a name="line.87"></a>
-<span class="sourceLineNo">088</span> * &lt;p&gt;<a name="line.88"></a>
-<span class="sourceLineNo">089</span> * The methods used by the 2-sample default implementation are also exposed directly:<a name="line.89"></a>
-<span class="sourceLineNo">090</span> * &lt;ul&gt;<a name="line.90"></a>
-<span class="sourceLineNo">091</span> * &lt;li&gt;{@link #exactP(double, int, int, boolean)} computes exact 2-sample p-values&lt;/li&gt;<a name="line.91"></a>
-<span class="sourceLineNo">092</span> * &lt;li&gt;{@link #monteCarloP(double, int, int, boolean, int)} computes 2-sample p-values by Monte<a name="line.92"></a>
-<span class="sourceLineNo">093</span> * Carlo simulation&lt;/li&gt;<a name="line.93"></a>
-<span class="sourceLineNo">094</span> * &lt;li&gt;{@link #approximateP(double, int, int)} uses the asymptotic distribution The {@code boolean}<a name="line.94"></a>
-<span class="sourceLineNo">095</span> * arguments in the first two methods allow the probability used to estimate the p-value to be<a name="line.95"></a>
-<span class="sourceLineNo">096</span> * expressed using strict or non-strict inequality. See<a name="line.96"></a>
-<span class="sourceLineNo">097</span> * {@link #kolmogorovSmirnovTest(double[], double[], boolean)}.&lt;/li&gt;<a name="line.97"></a>
-<span class="sourceLineNo">098</span> * &lt;/ul&gt;<a name="line.98"></a>
-<span class="sourceLineNo">099</span> * &lt;/p&gt;<a name="line.99"></a>
-<span class="sourceLineNo">100</span> * &lt;p&gt;<a name="line.100"></a>
-<span class="sourceLineNo">101</span> * References:<a name="line.101"></a>
-<span class="sourceLineNo">102</span> * &lt;ul&gt;<a name="line.102"></a>
-<span class="sourceLineNo">103</span> * &lt;li&gt;[1] &lt;a href="http://www.jstatsoft.org/v08/i18/"&gt; Evaluating Kolmogorov's Distribution&lt;/a&gt; by<a name="line.103"></a>
-<span class="sourceLineNo">104</span> * George Marsaglia, Wai Wan Tsang, and Jingbo Wang&lt;/li&gt;<a name="line.104"></a>
-<span class="sourceLineNo">105</span> * &lt;li&gt;[2] &lt;a href="http://www.jstatsoft.org/v39/i11/"&gt; Computing the Two-Sided Kolmogorov-Smirnov<a name="line.105"></a>
-<span class="sourceLineNo">106</span> * Distribution&lt;/a&gt; by Richard Simard and Pierre L'Ecuyer&lt;/li&gt;<a name="line.106"></a>
-<span class="sourceLineNo">107</span> * &lt;/ul&gt;<a name="line.107"></a>
-<span class="sourceLineNo">108</span> * &lt;br/&gt;<a name="line.108"></a>
-<span class="sourceLineNo">109</span> * Note that [1] contains an error in computing h, refer to &lt;a<a name="line.109"></a>
-<span class="sourceLineNo">110</span> * href="https://issues.apache.org/jira/browse/MATH-437"&gt;MATH-437&lt;/a&gt; for details.<a name="line.110"></a>
-<span class="sourceLineNo">111</span> * &lt;/p&gt;<a name="line.111"></a>
-<span class="sourceLineNo">112</span> *<a name="line.112"></a>
-<span class="sourceLineNo">113</span> * @since 3.3<a name="line.113"></a>
-<span class="sourceLineNo">114</span> */<a name="line.114"></a>
-<span class="sourceLineNo">115</span>public class KolmogorovSmirnovTest {<a name="line.115"></a>
-<span class="sourceLineNo">116</span><a name="line.116"></a>
-<span class="sourceLineNo">117</span>    /**<a name="line.117"></a>
-<span class="sourceLineNo">118</span>     * Bound on the number of partial sums in {@link #ksSum(double, double, int)}<a name="line.118"></a>
-<span class="sourceLineNo">119</span>     */<a name="line.119"></a>
-<span class="sourceLineNo">120</span>    protected static final int MAXIMUM_PARTIAL_SUM_COUNT = 100000;<a name="line.120"></a>
-<span class="sourceLineNo">121</span><a name="line.121"></a>
-<span class="sourceLineNo">122</span>    /** Convergence criterion for {@link #ksSum(double, double, int)} */<a name="line.122"></a>
-<span class="sourceLineNo">123</span>    protected static final double KS_SUM_CAUCHY_CRITERION = 1E-20;<a name="line.123"></a>
-<span class="sourceLineNo">124</span><a name="line.124"></a>
-<span class="sourceLineNo">125</span>    /** Convergence criterion for the sums in #pelzGood(double, double, int)} */<a name="line.125"></a>
-<span class="sourceLineNo">126</span>    protected static final double PG_SUM_RELATIVE_ERROR = 1.0e-10;<a name="line.126"></a>
-<span class="sourceLineNo">127</span><a name="line.127"></a>
-<span class="sourceLineNo">128</span>    /** When product of sample sizes is less than this value, 2-sample K-S test is exact */<a name="line.128"></a>
-<span class="sourceLineNo">129</span>    protected static final int SMALL_SAMPLE_PRODUCT = 200;<a name="line.129"></a>
-<span class="sourceLineNo">130</span><a name="line.130"></a>
-<span class="sourceLineNo">131</span>    /**<a name="line.131"></a>
-<span class="sourceLineNo">132</span>     * When product of sample sizes exceeds this value, 2-sample K-S test uses asymptotic<a name="line.132"></a>
-<span class="sourceLineNo">133</span>     * distribution for strict inequality p-value.<a name="line.133"></a>
-<span class="sourceLineNo">134</span>     */<a name="line.134"></a>
-<span class="sourceLineNo">135</span>    protected static final int LARGE_SAMPLE_PRODUCT = 10000;<a name="line.135"></a>
-<span class="sourceLineNo">136</span><a name="line.136"></a>
-<span class="sourceLineNo">137</span>    /** Default number of iterations used by {@link #monteCarloP(double, int, int, boolean, int)} */<a name="line.137"></a>
-<span class="sourceLineNo">138</span>    protected static final int MONTE_CARLO_ITERATIONS = 1000000;<a name="line.138"></a>
-<span class="sourceLineNo">139</span><a name="line.139"></a>
-<span class="sourceLineNo">140</span>    /** Random data generator used by {@link #monteCarloP(double, int, int, boolean, int)} */<a name="line.140"></a>
-<span class="sourceLineNo">141</span>    private final RandomGenerator rng;<a name="line.141"></a>
-<span class="sourceLineNo">142</span><a name="line.142"></a>
-<span class="sourceLineNo">143</span>    /**<a name="line.143"></a>
-<span class="sourceLineNo">144</span>     * Construct a KolmogorovSmirnovTest instance with a default random data generator.<a name="line.144"></a>
-<span class="sourceLineNo">145</span>     */<a name="line.145"></a>
-<span class="sourceLineNo">146</span>    public KolmogorovSmirnovTest() {<a name="line.146"></a>
-<span class="sourceLineNo">147</span>        rng = new Well19937c();<a name="line.147"></a>
-<span class="sourceLineNo">148</span>    }<a name="line.148"></a>
-<span class="sourceLineNo">149</span><a name="line.149"></a>
-<span class="sourceLineNo">150</span>    /**<a name="line.150"></a>
-<span class="sourceLineNo">151</span>     * Construct a KolmogorovSmirnovTest with the provided random data generator.<a name="line.151"></a>
-<span class="sourceLineNo">152</span>     *<a name="line.152"></a>
-<span class="sourceLineNo">153</span>     * @param rng random data generator used by {@link #monteCarloP(double, int, int, boolean, int)}<a name="line.153"></a>
-<span class="sourceLineNo">154</span>     */<a name="line.154"></a>
-<span class="sourceLineNo">155</span>    public KolmogorovSmirnovTest(RandomGenerator rng) {<a name="line.155"></a>
-<span class="sourceLineNo">156</span>        this.rng = rng;<a name="line.156"></a>
-<span class="sourceLineNo">157</span>    }<a name="line.157"></a>
-<span class="sourceLineNo">158</span><a name="line.158"></a>
-<span class="sourceLineNo">159</span>    /**<a name="line.159"></a>
-<span class="sourceLineNo">160</span>     * Computes the &lt;i&gt;p-value&lt;/i&gt;, or &lt;i&gt;observed significance level&lt;/i&gt;, of a one-sample &lt;a<a name="line.160"></a>
-<span class="sourceLineNo">161</span>     * href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"&gt; Kolmogorov-Smirnov test&lt;/a&gt;<a name="line.161"></a>
-<span class="sourceLineNo">162</span>     * evaluating the null hypothesis that {@code data} conforms to {@code distribution}. If<a name="line.162"></a>
-<span class="sourceLineNo">163</span>     * {@code exact} is true, the distribution used to compute the p-value is computed using<a name="line.163"></a>
-<span class="sourceLineNo">164</span>     * extended precision. See {@link #cdfExact(double, int)}.<a name="line.164"></a>
-<span class="sourceLineNo">165</span>     *<a name="line.165"></a>
-<span class="sourceLineNo">166</span>     * @param distribution reference distribution<a name="line.166"></a>
-<span class="sourceLineNo">167</span>     * @param data sample being being evaluated<a name="line.167"></a>
-<span class="sourceLineNo">168</span>     * @param exact whether or not to force exact computation of the p-value<a name="line.168"></a>
-<span class="sourceLineNo">169</span>     * @return the p-value associated with the null hypothesis that {@code data} is a sample from<a name="line.169"></a>
-<span class="sourceLineNo">170</span>     *         {@code distribution}<a name="line.170"></a>
-<span class="sourceLineNo">171</span>     * @throws InsufficientDataException if {@code data} does not have length at least 2<a name="line.171"></a>
-<span class="sourceLineNo">172</span>     * @throws NullArgumentException if {@code data} is null<a name="line.172"></a>
-<span class="sourceLineNo">173</span>     */<a name="line.173"></a>
-<span class="sourceLineNo">174</span>    public double kolmogorovSmirnovTest(RealDistribution distribution, double[] data, boolean exact) {<a name="line.174"></a>
-<span class="sourceLineNo">175</span>        return 1d - cdf(kolmogorovSmirnovStatistic(distribution, data), data.length, exact);<a name="line.175"></a>
-<span class="sourceLineNo">176</span>    }<a name="line.176"></a>
-<span class="sourceLineNo">177</span><a name="line.177"></a>
-<span class="sourceLineNo">178</span>    /**<a name="line.178"></a>
-<span class="sourceLineNo">179</span>     * Computes the one-sample Kolmogorov-Smirnov test statistic, \(D_n=\sup_x |F_n(x)-F(x)|\) where<a name="line.179"></a>
-<span class="sourceLineNo">180</span>     * \(F\) is the distribution (cdf) function associated with {@code distribution}, \(n\) is the<a name="line.180"></a>
-<span class="sourceLineNo">181</span>     * length of {@code data} and \(F_n\) is the empirical distribution that puts mass \(1/n\) at<a name="line.181"></a>
-<span class="sourceLineNo">182</span>     * each of the values in {@code data}.<a name="line.182"></a>
-<span class="sourceLineNo">183</span>     *<a name="line.183"></a>
-<span class="sourceLineNo">184</span>     * @param distribution reference distribution<a name="line.184"></a>
-<span class="sourceLineNo">185</span>     * @param data sample being evaluated<a name="line.185"></a>
-<span class="sourceLineNo">186</span>     * @return Kolmogorov-Smirnov statistic \(D_n\)<a name="line.186"></a>
-<span class="sourceLineNo">187</span>     * @throws InsufficientDataException if {@code data} does not have length at least 2<a name="line.187"></a>
-<span class="sourceLineNo">188</span>     * @throws NullArgumentException if {@code data} is null<a name="line.188"></a>
-<span class="sourceLineNo">189</span>     */<a name="line.189"></a>
-<span class="sourceLineNo">190</span>    public double kolmogorovSmirnovStatistic(RealDistribution distribution, double[] data) {<a name="line.190"></a>
-<span class="sourceLineNo">191</span>        checkArray(data);<a name="line.191"></a>
-<span class="sourceLineNo">192</span>        final int n = data.length;<a name="line.192"></a>
-<span class="sourceLineNo">193</span>        final double nd = n;<a name="line.193"></a>
-<span class="sourceLineNo">194</span>        final double[] dataCopy = new double[n];<a name="line.194"></a>
-<span class="sourceLineNo">195</span>        System.arraycopy(data, 0, dataCopy, 0, n);<a name="line.195"></a>
-<span class="sourceLineNo">196</span>        Arrays.sort(dataCopy);<a name="line.196"></a>
-<span class="sourceLineNo">197</span>        double d = 0d;<a name="line.197"></a>
-<span class="sourceLineNo">198</span>        for (int i = 1; i &lt;= n; i++) {<a name="line.198"></a>
-<span class="sourceLineNo">199</span>            final double yi = distribution.cumulativeProbability(dataCopy[i - 1]);<a name="line.199"></a>
-<span class="sourceLineNo">200</span>            final double currD = FastMath.max(yi - (i - 1) / nd, i / nd - yi);<a name="line.200"></a>
-<span class="sourceLineNo">201</span>            if (currD &gt; d) {<a name="line.201"></a>
-<span class="sourceLineNo">202</span>                d = currD;<a name="line.202"></a>
-<span class="sourceLineNo">203</span>            }<a name="line.203"></a>
-<span class="sourceLineNo">204</span>        }<a name="line.204"></a>
-<span class="sourceLineNo">205</span>        return d;<a name="line.205"></a>
-<span class="sourceLineNo">206</span>    }<a name="line.206"></a>
-<span class="sourceLineNo">207</span><a name="line.207"></a>
-<span class="sourceLineNo">208</span>    /**<a name="line.208"></a>
-<span class="sourceLineNo">209</span>     * Computes the &lt;i&gt;p-value&lt;/i&gt;, or &lt;i&gt;observed significance level&lt;/i&gt;, of a two-sample &lt;a<a name="line.209"></a>
-<span class="sourceLineNo">210</span>     * href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"&gt; Kolmogorov-Smirnov test&lt;/a&gt;<a name="line.210"></a>
-<span class="sourceLineNo">211</span>     * evaluating the null hypothesis that {@code x} and {@code y} are samples drawn from the same<a name="line.211"></a>
-<span class="sourceLineNo">212</span>     * probability distribution. Specifically, what is returned is an estimate of the probability<a name="line.212"></a>
-<span class="sourceLineNo">213</span>     * that the {@link #kolmogorovSmirnovStatistic(double[], double[])} associated with a randomly<a name="line.213"></a>
-<span class="sourceLineNo">214</span>     * selected partition of the combined sample into subsamples of sizes {@code x.length} and<a name="line.214"></a>
-<span class="sourceLineNo">215</span>     * {@code y.length} will strictly exceed (if {@code strict} is {@code true}) or be at least as<a name="line.215"></a>
-<span class="sourceLineNo">216</span>     * large as {@code strict = false}) as {@code kolmogorovSmirnovStatistic(x, y)}.<a name="line.216"></a>
-<span class="sourceLineNo">217</span>     * &lt;ul&gt;<a name="line.217"></a>
-<span class="sourceLineNo">218</span>     * &lt;li&gt;For very small samples (where the product of the sample sizes is less than<a name="line.218"></a>
-<span class="sourceLineNo">219</span>     * {@value #SMALL_SAMPLE_PRODUCT}), the exact distribution is used to compute the p-value. This<a name="line.219"></a>
-<span class="sourceLineNo">220</span>     * is accomplished by enumerating all partitions of the combined sample into two subsamples of<a name="line.220"></a>
-<span class="sourceLineNo">221</span>     * the respective sample sizes, computing \(D_{n,m}\) for each partition and returning the<a name="line.221"></a>
-<span class="sourceLineNo">222</span>     * proportion of partitions that give \(D\) values exceeding the observed value.&lt;/li&gt;<a name="line.222"></a>
-<span class="sourceLineNo">223</span>     * &lt;li&gt;For mid-size samples (product of sample sizes greater than or equal to<a name="line.223"></a>
-<span class="sourceLineNo">224</span>     * {@value #SMALL_SAMPLE_PRODUCT} but less than {@value #LARGE_SAMPLE_PRODUCT}), Monte Carlo<a name="line.224"></a>
-<span class="sourceLineNo">225</span>     * simulation is used to compute the p-value. The simulation randomly generates partitions and<a name="line.225"></a>
-<span class="sourceLineNo">226</span>     * reports the proportion that give \(D\) values exceeding the observed value.&lt;/li&gt;<a name="line.226"></a>
-<span class="sourceLineNo">227</span>     * &lt;li&gt;When the product of the sample sizes exceeds {@value #LARGE_SAMPLE_PRODUCT}, the<a name="line.227"></a>
-<span class="sourceLineNo">228</span>     * asymptotic distribution of \(D_{n,m}\) is used. See {@link #approximateP(double, int, int)}<a name="line.228"></a>
-<span class="sourceLineNo">229</span>     * for details on the approximation.&lt;/li&gt;<a name="line.229"></a>
-<span class="sourceLineNo">230</span>     * &lt;/ul&gt;<a name="line.230"></a>
-<span class="sourceLineNo">231</span>     *<a name="line.231"></a>
-<span class="sourceLineNo">232</span>     * @param x first sample dataset<a name="line.232"></a>
-<span class="sourceLineNo">233</span>     * @param y second sample dataset<a name="line.233"></a>
-<span class="sourceLineNo">234</span>     * @param strict whether or not the probability to compute is expressed as a strict inequality<a name="line.234"></a>
-<span class="sourceLineNo">235</span>     *        (ignored for large samples)<a name="line.235"></a>
-<span class="sourceLineNo">236</span>     * @return p-value associated with the null hypothesis that {@code x} and {@code y} represent<a name="line.236"></a>
-<span class="sourceLineNo">237</span>     *         samples from the same distribution<a name="line.237"></a>
-<span class="sourceLineNo">238</span>     * @throws InsufficientDataException if either {@code x} or {@code y} does not have length at<a name="line.238"></a>
-<span class="sourceLineNo">239</span>     *         least 2<a name="line.239"></a>
-<span class="sourceLineNo">240</span>     * @throws NullArgumentException if either {@code x} or {@code y} is null<a name="line.240"></a>
-<span class="sourceLineNo">241</span>     */<a name="line.241"></a>
-<span class="sourceLineNo">242</span>    public double kolmogorovSmirnovTest(double[] x, double[] y, boolean strict) {<a name="line.242"></a>
-<span class="sourceLineNo">243</span>        final long lengthProduct = (long) x.length * y.length;<a name="line.243"></a>
-<span class="sourceLineNo">244</span>        if (lengthProduct &lt; SMALL_SAMPLE_PRODUCT) {<a name="line.244"></a>
-<span class="sourceLineNo">245</span>            return exactP(kolmogorovSmirnovStatistic(x, y), x.length, y.length, strict);<a name="line.245"></a>
-<span class="sourceLineNo">246</span>        }<a name="line.246"></a>
-<span class="sourceLineNo">247</span>        if (lengthProduct &lt; LARGE_SAMPLE_PRODUCT) {<a name="line.247"></a>
-<span class="sourceLineNo">248</span>            return monteCarloP(kolmogorovSmirnovStatistic(x, y), x.length, y.length, strict, MONTE_CARLO_ITERATIONS);<a name="line.248"></a>
-<span class="sourceLineNo">249</span>        }<a name="line.249"></a>
-<span class="sourceLineNo">250</span>        return approximateP(kolmogorovSmirnovStatistic(x, y), x.length, y.length);<a name="line.250"></a>
-<span class="sourceLineNo">251</span>    }<a name="line.251"></a>
-<span class="sourceLineNo">252</span><a name="line.252"></a>
-<span class="sourceLineNo">253</span>    /**<a name="line.253"></a>
-<span class="sourceLineNo">254</span>     * Computes the &lt;i&gt;p-value&lt;/i&gt;, or &lt;i&gt;observed significance level&lt;/i&gt;, of a two-sample &lt;a<a name="line.254"></a>
-<span class="sourceLineNo">255</span>     * href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"&gt; Kolmogorov-Smirnov test&lt;/a&gt;<a name="line.255"></a>
-<span class="sourceLineNo">256</span>     * evaluating the null hypothesis that {@code x} and {@code y} are samples drawn from the same<a name="line.256"></a>
-<span class="sourceLineNo">257</span>     * probability distribution. Assumes the strict form of the inequality used to compute the<a name="line.257"></a>
-<span class="sourceLineNo">258</span>     * p-value. See {@link #kolmogorovSmirnovTest(RealDistribution, double[], boolean)}.<a name="line.258"></a>
-<span class="sourceLineNo">259</span>     *<a name="line.259"></a>
-<span class="sourceLineNo">260</span>     * @param x first sample dataset<a name="line.260"></a>
-<span class="sourceLineNo">261</span>     * @param y second sample dataset<a name="line.261"></a>
-<span class="sourceLineNo">262</span>     * @return p-value associated with the null hypothesis that {@code x} and {@code y} represent<a name="line.262"></a>
-<span class="sourceLineNo">263</span>     *         samples from the same distribution<a name="line.263"></a>
-<span class="sourceLineNo">264</span>     * @throws InsufficientDataException if either {@code x} or {@code y} does not have length at<a name="line.264"></a>
-<span class="sourceLineNo">265</span>     *         least 2<a name="line.265"></a>
-<span class="sourceLineNo">266</span>     * @throws NullArgumentException if either {@code x} or {@code y} is null<a name="line.266"></a>
-<span class="sourceLineNo">267</span>     */<a name="line.267"></a>
-<span class="sourceLineNo">268</span>    public double kolmogorovSmirnovTest(double[] x, double[] y) {<a name="line.268"></a>
-<span class="sourceLineNo">269</span>        return kolmogorovSmirnovTest(x, y, true);<a name="line.269"></a>
-<span class="sourceLineNo">270</span>    }<a name="line.270"></a>
-<span class="sourceLineNo">271</span><a name="line.271"></a>
-<span class="sourceLineNo">272</span>    /**<a name="line.272"></a>
-<span class="sourceLineNo">273</span>     * Computes the two-sample Kolmogorov-Smirnov test statistic, \(D_{n,m}=\sup_x |F_n(x)-F_m(x)|\)<a name="line.273"></a>
-<span class="sourceLineNo">274</span>     * where \(n\) is the length of {@code x}, \(m\) is the length of {@code y}, \(F_n\) is the<a name="line.274"></a>
-<span class="sourceLineNo">275</span>     * empirical distribution that puts mass \(1/n\) at each of the values in {@code x} and \(F_m\)<a name="line.275"></a>
-<span class="sourceLineNo">276</span>     * is the empirical distribution of the {@code y} values.<a name="line.276"></a>
-<span class="sourceLineNo">277</span>     *<a name="line.277"></a>
-<span class="sourceLineNo">278</span>     * @param x first sample<a name="line.278"></a>
-<span class="sourceLineNo">279</span>     * @param y second sample<a name="line.279"></a>
-<span class="sourceLineNo">280</span>     * @return test statistic \(D_{n,m}\) used to evaluate the null hypothesis that {@code x} and<a name="line.280"></a>
-<span class="sourceLineNo">281</span>     *         {@code y} represent samples from the same underlying distribution<a name="line.281"></a>
-<span class="sourceLineNo">282</span>     * @throws InsufficientDataException if either {@code x} or {@code y} does not have length at<a name="line.282"></a>
-<span class="sourceLineNo">283</span>     *         least 2<a name="line.283"></a>
-<span class="sourceLineNo">284</span>     * @throws NullArgumentException if either {@code x} or {@code y} is null<a name="line.284"></a>
-<span class="sourceLineNo">285</span>     */<a name="line.285"></a>
-<span class="sourceLineNo">286</span>    public double kolmogorovSmirnovStatistic(double[] x, double[] y) {<a name="line.286"></a>
-<span class="sourceLineNo">287</span>        checkArray(x);<a name="line.287"></a>
-<span class="sourceLineNo">288</span>        checkArray(y);<a name="line.288"></a>
-<span class="sourceLineNo">289</span>        // Copy and sort the sample arrays<a name="line.289"></a>
-<span class="sourceLineNo">290</span>        final double[] sx = MathArrays.copyOf(x);<a name="line.290"></a>
-<span class="sourceLineNo">291</span>        final double[] sy = MathArrays.copyOf(y);<a name="line.291"></a>
-<span class="sourceLineNo">292</span>        Arrays.sort(sx);<a name="line.292"></a>
-<span class="sourceLineNo">293</span>        Arrays.sort(sy);<a name="line.293"></a>
-<span class="sourceLineNo">294</span>        final int n = sx.length;<a name="line.294"></a>
-<span class="sourceLineNo">295</span>        final int m = sy.length;<a name="line.295"></a>
-<span class="sourceLineNo">296</span><a name="line.296"></a>
-<span class="sourceLineNo">297</span>        // Find the max difference between cdf_x and cdf_y<a name="line.297"></a>
-<span class="sourceLineNo">298</span>        double supD = 0d;<a name="line.298"></a>
-<span class="sourceLineNo">299</span>        // First walk x points<a name="line.299"></a>
-<span class="sourceLineNo">300</span>        for (int i = 0; i &lt; n; i++) {<a name="line.300"></a>
-<span class="sourceLineNo">301</span>            final double x_i = sx[i];<a name="line.301"></a>
-<span class="sourceLineNo">302</span>            // ties can be safely ignored<a name="line.302"></a>
-<span class="sourceLineNo">303</span>            if (i &gt; 0 &amp;&amp; x_i == sx[i-1]) {<a name="line.303"></a>
-<span class="sourceLineNo">304</span>                continue;<a name="line.304"></a>
-<span class="sourceLineNo">305</span>            }<a name="line.305"></a>
-<span class="sourceLineNo">306</span>            final double cdf_x = edf(x_i, sx);<a name="line.306"></a>
-<span class="sourceLineNo">307</span>            final double cdf_y = edf(x_i, sy);<a name="line.307"></a>
-<span class="sourceLineNo">308</span>            final double curD = FastMath.abs(cdf_x - cdf_y);<a name="line.308"></a>
-<span class="sourceLineNo">309</span>            if (curD &gt; supD) {<a name="line.309"></a>
-<span class="sourceLineNo">310</span>                supD = curD;<a name="line.310"></a>
-<span class="sourceLineNo">311</span>            }<a name="line.311"></a>
-<span class="sourceLineNo">312</span>        }<a name="line.312"></a>
-<span class="sourceLineNo">313</span>        // Now look at y<a name="line.313"></a>
-<span class="sourceLineNo">314</span>        for (int i = 0; i &lt; m; i++) {<a name="line.314"></a>
-<span class="sourceLineNo">315</span>            final double y_i = sy[i];<a name="line.315"></a>
-<span class="sourceLineNo">316</span>            // ties can be safely ignored<a name="line.316"></a>
-<span class="sourceLineNo">317</span>            if (i &gt; 0 &amp;&amp; y_i == sy[i-1]) {<a name="line.317"></a>
-<span class="sourceLineNo">318</span>                continue;<a name="line.318"></a>
-<span class="sourceLineNo">319</span>            }<a name="line.319"></a>
-<span class="sourceLineNo">320</span>            final double cdf_x = edf(y_i, sx);<a name="line.320"></a>
-<span class="sourceLineNo">321</span>            final double cdf_y = edf(y_i, sy);<a name="line.321"></a>
-<span class="sourceLineNo">322</span>            final double curD = FastMath.abs(cdf_x - cdf_y);<a name="line.322"></a>
-<span class="sourceLineNo">323</span>            if (curD &gt; supD) {<a name="line.323"></a>
-<span class="sourceLineNo">324</span>                supD = curD;<a name="line.324"></a>
-<span class="sourceLineNo">325</span>            }<a name="line.325"></a>
-<span class="sourceLineNo">326</span>        }<a name="line.326"></a>
-<span class="sourceLineNo">327</span>        return supD;<a name="line.327"></a>
-<span class="sourceLineNo">328</span>    }<a name="line.328"></a>
-<span class="sourceLineNo">329</span><a name="line.329"></a>
-<span class="sourceLineNo">330</span>    /**<a name="line.330"></a>
-<span class="sourceLineNo">331</span>     * Computes the empirical distribution function.<a name="line.331"></a>
-<span class="sourceLineNo">332</span>     *<a name="line.332"></a>
-<span class="sourceLineNo">333</span>     * @param x the given x<a name="line.333"></a>
-<span class="sourceLineNo">334</span>     * @param samples the observations<a name="line.334"></a>
-<span class="sourceLineNo">335</span>     * @return the empirical distribution function \(F_n(x)\)<a name="line.335"></a>
-<span class="sourceLineNo">336</span>     */<a name="line.336"></a>
-<span class="sourceLineNo">337</span>    private double edf(final double x, final double[] samples) {<a name="line.337"></a>
-<span class="sourceLineNo">338</span>        final int n = samples.length;<a name="line.338"></a>
-<span class="sourceLineNo">339</span>        int index = Arrays.binarySearch(samples, x);<a name="line.339"></a>
-<span class="sourceLineNo">340</span>        if (index &gt;= 0) {<a name="line.340"></a>
-<span class="sourceLineNo">341</span>            while(index &lt; (n - 1) &amp;&amp; samples[index+1] == x) {<a name="line.341"></a>
-<span class="sourceLineNo">342</span>                ++index;<a name="line.342"></a>
-<span class="sourceLineNo">343</span>            }<a name="line.343"></a>
-<span class="sourceLineNo">344</span>        }<a name="line.344"></a>
-<span class="sourceLineNo">345</span>        return index &gt;= 0 ? (index + 1d) / n : (-index - 1d) / n;<a name="line.345"></a>
-<span class="sourceLineNo">346</span>    }<a name="line.346"></a>
-<span class="sourceLineNo">347</span><a name="line.347"></a>
-<span class="sourceLineNo">348</span>    /**<a name="line.348"></a>
-<span class="sourceLineNo">349</span>     * Computes the &lt;i&gt;p-value&lt;/i&gt;, or &lt;i&gt;observed significance level&lt;/i&gt;, of a one-sample &lt;a<a name="line.349"></a>
-<span class="sourceLineNo">350</span>     * href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"&gt; Kolmogorov-Smirnov test&lt;/a&gt;<a name="line.350"></a>
-<span class="sourceLineNo">351</span>     * evaluating the null hypothesis that {@code data} conforms to {@code distribution}.<a name="line.351"></a>
-<span class="sourceLineNo">352</span>     *<a name="line.352"></a>
-<span class="sourceLineNo">353</span>     * @param distribution reference distribution<a name="line.353"></a>
-<span class="sourceLineNo">354</span>     * @param data sample being being evaluated<a name="line.354"></a>
-<span class="sourceLineNo">355</span>     * @return the p-value associated with the null hypothesis that {@code data} is a sample from<a name="line.355"></a>
-<span class="sourceLineNo">356</span>     *         {@code distribution}<a name="line.356"></a>
-<span class="sourceLineNo">357</span>     * @throws InsufficientDataException if {@code data} does not have length at least 2<a name="line.357"></a>
-<span class="sourceLineNo">358</span>     * @throws NullArgumentException if {@code data} is null<a name="line.358"></a>
-<span class="sourceLineNo">359</span>     */<a name="line.359"></a>
-<span class="sourceLineNo">360</span>    public double kolmogorovSmirnovTest(RealDistribution distribution, double[] data) {<a name="line.360"></a>
-<span class="sourceLineNo">361</span>        return kolmogorovSmirnovTest(distribution, data, false);<a name="line.361"></a>
-<span class="sourceLineNo">362</span>    }<a name="line.362"></a>
-<span class="sourceLineNo">363</span><a name="line.363"></a>
-<span class="sourceLineNo">364</span>    /**<a name="line.364"></a>
-<span class="sourceLineNo">365</span>     * Performs a &lt;a href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"&gt; Kolmogorov-Smirnov<a name="line.365"></a>
-<span class="sourceLineNo">366</span>     * test&lt;/a&gt; evaluating the null hypothesis that {@code data} conforms to {@code distribution}.<a name="line.366"></a>
-<span class="sourceLineNo">367</span>     *<a name="line.367"></a>
-<span class="sourceLineNo">368</span>     * @param distribution reference distribution<a name="line.368"></a>
-<span class="sourceLineNo">369</span>     * @param data sample being being evaluated<a name="line.369"></a>
-<span class="sourceLineNo">370</span>     * @param alpha significance level of the test<a name="line.370"></a>
-<span class="sourceLineNo">371</span>     * @return true iff the null hypothesis that {@code data} is a sample from {@code distribution}<a name="line.371"></a>
-<span class="sourceLineNo">372</span>     *         can be rejected with confidence 1 - {@code alpha}<a name="line.372"></a>
-<span class="sourceLineNo">373</span>     * @throws InsufficientDataException if {@code data} does not have length at least 2<a name="line.373"></a>
-<span class="sourceLineNo">374</span>     * @throws NullArgumentException if {@code data} is null<a name="line.374"></a>
-<span class="sourceLineNo">375</span>     */<a name="line.375"></a>
-<span class="sourceLineNo">376</span>    public boolean kolmogorovSmirnovTest(RealDistribution distribution, double[] data, double alpha) {<a name="line.376"></a>
-<span class="sourceLineNo">377</span>        if ((alpha &lt;= 0) || (alpha &gt; 0.5)) {<a name="line.377"></a>
-<span class="sourceLineNo">378</span>            throw new OutOfRangeException(LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL, alpha, 0, 0.5);<a name="line.378"></a>
-<span class="sourceLineNo">379</span>        }<a name="line.379"></a>
-<span class="sourceLineNo">380</span>        return kolmogorovSmirnovTest(distribution, data) &lt; alpha;<a name="line.380"></a>
-<span class="sourceLineNo">381</span>    }<a name="line.381"></a>
-<span class="sourceLineNo">382</span><a name="line.382"></a>
-<span class="sourceLineNo">383</span>    /**<a name="line.383"></a>
-<span class="sourceLineNo">384</span>     * Calculates \(P(D_n &lt; d)\) using the method described in [1] with quick decisions for extreme<a name="line.384"></a>
-<span class="sourceLineNo">385</span>     * values given in [2] (see above). The result is not exact as with<a name="line.385"></a>
-<span class="sourceLineNo">386</span>     * {@link #cdfExact(double, int)} because calculations are based on<a name="line.386"></a>
-<span class="sourceLineNo">387</span>     * {@code double} rather than {@link org.apache.commons.math3.fraction.BigFraction}.<a name="line.387"></a>
-<span class="sourceLineNo">388</span>     *<a name="line.388"></a>
-<span class="sourceLineNo">389</span>     * @param d statistic<a name="line.389"></a>
-<span class="sourceLineNo">390</span>     * @param n sample size<a name="line.390"></a>
-<span class="sourceLineNo">391</span>     * @return \(P(D_n &lt; d)\)<a name="line.391"></a>
-<span class="sourceLineNo">392</span>     * @throws MathArithmeticException if algorithm fails to convert {@code h} to a<a name="line.392"></a>
-<span class="sourceLineNo">393</span>     *         {@link org.apache.commons.math3.fraction.BigFraction} in expressing {@code d} as \((k<a name="line.393"></a>
-<span class="sourceLineNo">394</span>     *         - h) / m\) for integer {@code k, m} and \(0 \le h &lt; 1\)<a name="line.394"></a>
-<span class="sourceLineNo">395</span>     */<a name="line.395"></a>
-<span class="sourceLineNo">396</span>    public double cdf(double d, int n)<a name="line.396"></a>
-<span class="sourceLineNo">397</span>        throws MathArithmeticException {<a name="line.397"></a>
-<span class="sourceLineNo">398</span>        return cdf(d, n, false);<a name="line.398"></a>
-<span class="sourceLineNo">399</span>    }<a name="line.399"></a>
-<span class="sourceLineNo">400</span><a name="line.400"></a>
-<span class="sourceLineNo">401</span>    /**<a name="line.401"></a>
-<span class="sourceLineNo">402</span>     * Calculates {@code P(D_n &lt; d)}. The result is exact in the sense that BigFraction/BigReal is<a name="line.402"></a>
-<span class="sourceLineNo">403</span>     * used everywhere at the expense of very slow execution time. Almost never choose this in real<a name="line.403"></a>
-<span class="sourceLineNo">404</span>     * applications unless you are very sure; this is almost solely for verification purposes.<a name="line.404"></a>
-<span class="sourceLineNo">405</span>     * Normally, you would choose {@link #cdf(double, int)}. See the class<a name="line.405"></a>
-<span class="sourceLineNo">406</span>     * javadoc for definitions and algorithm description.<a name="line.406"></a>
-<span class="sourceLineNo">407</span>     *<a name="line.407"></a>
-<span class="sourceLineNo">408</span>     * @param d statistic<a name="line.408"></a>
-<span class="sourceLineNo">409</span>     * @param n sample size<a name="line.409"></a>
-<span class="sourceLineNo">410</span>     * @return \(P(D_n &lt; d)\)<a name="line.410"></a>
-<span class="sourceLineNo">411</span>     * @throws MathArithmeticException if the algorithm fails to convert {@code h} to a<a name="line.411"></a>
-<span class="sourceLineNo">412</span>     *         {@link org.apache.commons.math3.fraction.BigFraction} in expressing {@code d} as \((k<a name="line.412"></a>
-<span class="sourceLineNo">413</span>     *         - h) / m\) for integer {@code k, m} and \(0 \le h &lt; 1\)<a name="line.413"></a>
-<span class="sourceLineNo">414</span>     */<a name="line.414"></a>
-<span class="sourceLineNo">415</span>    public double cdfExact(double d, int n)<a name="line.415"></a>
-<span class="sourceLineNo">416</span>        throws MathArithmeticException {<a name="line.416"></a>
-<span class="sourceLineNo">417</span>        return cdf(d, n, true);<a name="line.417"></a>
-<span class="sourceLineNo">418</span>    }<a name="line.418"></a>
-<span class="sourceLineNo">419</span><a name="line.419"></a>
-<span class="sourceLineNo">420</span>    /**<a name="line.420"></a>
-<span class="sourceLineNo">421</span>     * Calculates {@code P(D_n &lt; d)} using method described in [1] with quick decisions for extreme<a name="line.421"></a>
-<span class="sourceLineNo">422</span>     * values given in [2] (see above).<a name="line.422"></a>
-<span class="sourceLineNo">423</span>     *<a name="line.423"></a>
-<span class="sourceLineNo">424</span>     * @param d statistic<a name="line.424"></a>
-<span class="sourceLineNo">425</span>     * @param n sample size<a name="line.425"></a>
-<span class="sourceLineNo">426</span>     * @param exact whether the probability should be calculated exact using<a name="line.426"></a>
-<span class="sourceLineNo">427</span>     *        {@link org.apache.commons.math3.fraction.BigFraction} everywhere at the expense of<a name="line.427"></a>
-<span class="sourceLineNo">428</span>     *        very slow execution time, or if {@code double} should be used convenient places to<a name="line.428"></a>
-<span class="sourceLineNo">429</span>     *        gain speed. Almost never choose {@code true} in real applications unless you are very<a name="line.429"></a>
-<span class="sourceLineNo">430</span>     *        sure; {@code true} is almost solely for verification purposes.<a name="line.430"></a>
-<span class="sourceLineNo">431</span>     * @return \(P(D_n &lt; d)\)<a name="line.431"></a>
-<span class="sourceLineNo">432</span>     * @throws MathArithmeticException if algorithm fails to convert {@code h} to a<a name="line.432"></a>
-<span class="sourceLineNo">433</span>     *         {@link org.apache.commons.math3.fraction.BigFraction} in expressing {@code d} as \((k<a name="line.433"></a>
-<span class="sourceLineNo">434</span>     *         - h) / m\) for integer {@code k, m} and \(0 \le h &lt; 1\).<a name="line.434"></a>
-<span class="sourceLineNo">435</span>     */<a name="line.435"></a>
-<span class="sourceLineNo">436</span>    public double cdf(double d, int n, boolean exact)<a name="line.436"></a>
-<span class="sourceLineNo">437</span>        throws MathArithmeticException {<a name="line.437"></a>
+<span class="sourceLineNo">024</span>import org.apache.commons.math3.util.Precision;<a name="line.24"></a>
+<span class="sourceLineNo">025</span>import org.apache.commons.math3.distribution.RealDistribution;<a name="line.25"></a>
+<span class="sourceLineNo">026</span>import org.apache.commons.math3.exception.InsufficientDataException;<a name="line.26"></a>
+<span class="sourceLineNo">027</span>import org.apache.commons.math3.exception.MathArithmeticException;<a name="line.27"></a>
+<span class="sourceLineNo">028</span>import org.apache.commons.math3.exception.NullArgumentException;<a name="line.28"></a>
+<span class="sourceLineNo">029</span>import org.apache.commons.math3.exception.NumberIsTooLargeException;<a name="line.29"></a>
+<span class="sourceLineNo">030</span>import org.apache.commons.math3.exception.OutOfRangeException;<a name="line.30"></a>
+<span class="sourceLineNo">031</span>import org.apache.commons.math3.exception.TooManyIterationsException;<a name="line.31"></a>
+<span class="sourceLineNo">032</span>import org.apache.commons.math3.exception.util.LocalizedFormats;<a name="line.32"></a>
+<span class="sourceLineNo">033</span>import org.apache.commons.math3.fraction.BigFraction;<a name="line.33"></a>
+<span class="sourceLineNo">034</span>import org.apache.commons.math3.fraction.BigFractionField;<a name="line.34"></a>
+<span class="sourceLineNo">035</span>import org.apache.commons.math3.fraction.FractionConversionException;<a name="line.35"></a>
+<span class="sourceLineNo">036</span>import org.apache.commons.math3.linear.Array2DRowFieldMatrix;<a name="line.36"></a>
+<span class="sourceLineNo">037</span>import org.apache.commons.math3.linear.FieldMatrix;<a name="line.37"></a>
+<span class="sourceLineNo">038</span>import org.apache.commons.math3.linear.MatrixUtils;<a name="line.38"></a>
+<span class="sourceLineNo">039</span>import org.apache.commons.math3.linear.RealMatrix;<a name="line.39"></a>
+<span class="sourceLineNo">040</span>import org.apache.commons.math3.random.RandomGenerator;<a name="line.40"></a>
+<span class="sourceLineNo">041</span>import org.apache.commons.math3.random.Well19937c;<a name="line.41"></a>
+<span class="sourceLineNo">042</span>import org.apache.commons.math3.util.CombinatoricsUtils;<a name="line.42"></a>
+<span class="sourceLineNo">043</span>import org.apache.commons.math3.util.FastMath;<a name="line.43"></a>
+<span class="sourceLineNo">044</span>import org.apache.commons.math3.util.MathArrays;<a name="line.44"></a>
+<span class="sourceLineNo">045</span><a name="line.45"></a>
+<span class="sourceLineNo">046</span>import static org.apache.commons.math3.util.MathUtils.PI_SQUARED;<a name="line.46"></a>
+<span class="sourceLineNo">047</span>import static org.apache.commons.math3.util.FastMath.PI;<a name="line.47"></a>
+<span class="sourceLineNo">048</span><a name="line.48"></a>
+<span class="sourceLineNo">049</span>/**<a name="line.49"></a>
+<span class="sourceLineNo">050</span> * Implementation of the &lt;a href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"&gt;<a name="line.50"></a>
+<span class="sourceLineNo">051</span> * Kolmogorov-Smirnov (K-S) test&lt;/a&gt; for equality of continuous distributions.<a name="line.51"></a>
+<span class="sourceLineNo">052</span> * &lt;p&gt;<a name="line.52"></a>
+<span class="sourceLineNo">053</span> * The K-S test uses a statistic based on the maximum deviation of the empirical distribution of<a name="line.53"></a>
+<span class="sourceLineNo">054</span> * sample data points from the distribution expected under the null hypothesis. For one-sample tests<a name="line.54"></a>
+<span class="sourceLineNo">055</span> * evaluating the null hypothesis that a set of sample data points follow a given distribution, the<a name="line.55"></a>
+<span class="sourceLineNo">056</span> * test statistic is \(D_n=\sup_x |F_n(x)-F(x)|\), where \(F\) is the expected distribution and<a name="line.56"></a>
+<span class="sourceLineNo">057</span> * \(F_n\) is the empirical distribution of the \(n\) sample data points. The distribution of<a name="line.57"></a>
+<span class="sourceLineNo">058</span> * \(D_n\) is estimated using a method based on [1] with certain quick decisions for extreme values<a name="line.58"></a>
+<span class="sourceLineNo">059</span> * given in [2].<a name="line.59"></a>
+<span class="sourceLineNo">060</span> * &lt;/p&gt;<a name="line.60"></a>
+<span class="sourceLineNo">061</span> * &lt;p&gt;<a name="line.61"></a>
+<span class="sourceLineNo">062</span> * Two-sample tests are also supported, evaluating the null hypothesis that the two samples<a name="line.62"></a>
+<span class="sourceLineNo">063</span> * {@code x} and {@code y} come from the same underlying distribution. In this case, the test<a name="line.63"></a>
+<span class="sourceLineNo">064</span> * statistic is \(D_{n,m}=\sup_t | F_n(t)-F_m(t)|\) where \(n\) is the length of {@code x}, \(m\) is<a name="line.64"></a>
+<span class="sourceLineNo">065</span> * the length of {@code y}, \(F_n\) is the empirical distribution that puts mass \(1/n\) at each of<a name="line.65"></a>
+<span class="sourceLineNo">066</span> * the values in {@code x} and \(F_m\) is the empirical distribution of the {@code y} values. The<a name="line.66"></a>
+<span class="sourceLineNo">067</span> * default 2-sample test method, {@link #kolmogorovSmirnovTest(double[], double[])} works as<a name="line.67"></a>
+<span class="sourceLineNo">068</span> * follows:<a name="line.68"></a>
+<span class="sourceLineNo">069</span> * &lt;ul&gt;<a name="line.69"></a>
+<span class="sourceLineNo">070</span> * &lt;li&gt;For very small samples (where the product of the sample sizes is less than<a name="line.70"></a>
+<span class="sourceLineNo">071</span> * {@value #SMALL_SAMPLE_PRODUCT}), the exact distribution is used to compute the p-value for the<a name="line.71"></a>
+<span class="sourceLineNo">072</span> * 2-sample test.&lt;/li&gt;<a name="line.72"></a>
+<span class="sourceLineNo">073</span> * &lt;li&gt;For mid-size samples (product of sample sizes greater than or equal to<a name="line.73"></a>
+<span class="sourceLineNo">074</span> * {@value #SMALL_SAMPLE_PRODUCT} but less than {@value #LARGE_SAMPLE_PRODUCT}), Monte Carlo<a name="line.74"></a>
+<span class="sourceLineNo">075</span> * simulation is used to compute the p-value. The simulation randomly generates partitions of \(m +<a name="line.75"></a>
+<span class="sourceLineNo">076</span> * n\) into an \(m\)-set and an \(n\)-set and reports the proportion that give \(D\) values<a name="line.76"></a>
+<span class="sourceLineNo">077</span> * exceeding the observed value.&lt;/li&gt;<a name="line.77"></a>
+<span class="sourceLineNo">078</span> * &lt;li&gt;When the product of the sample sizes exceeds {@value #LARGE_SAMPLE_PRODUCT}, the asymptotic<a name="line.78"></a>
+<span class="sourceLineNo">079</span> * distribution of \(D_{n,m}\) is used. See {@link #approximateP(double, int, int)} for details on<a name="line.79"></a>
+<span class="sourceLineNo">080</span> * the approximation.&lt;/li&gt;<a name="line.80"></a>
+<span class="sourceLineNo">081</span> * &lt;/ul&gt;<a name="line.81"></a>
+<span class="sourceLineNo">082</span> * &lt;/p&gt;<a name="line.82"></a>
+<span class="sourceLineNo">083</span> * &lt;p&gt;<a name="line.83"></a>
+<span class="sourceLineNo">084</span> * In the two-sample case, \(D_{n,m}\) has a discrete distribution. This makes the p-value<a name="line.84"></a>
+<span class="sourceLineNo">085</span> * associated with the null hypothesis \(H_0 : D_{n,m} \ge d \) differ from \(H_0 : D_{n,m} &gt; d \)<a name="line.85"></a>
+<span class="sourceLineNo">086</span> * by the mass of the observed value \(d\). To distinguish these, the two-sample tests use a boolean<a name="line.86"></a>
+<span class="sourceLineNo">087</span> * {@code strict} parameter. This parameter is ignored for large samples.<a name="line.87"></a>
+<span class="sourceLineNo">088</span> * &lt;/p&gt;<a name="line.88"></a>
+<span class="sourceLineNo">089</span> * &lt;p&gt;<a name="line.89"></a>
+<span class="sourceLineNo">090</span> * The methods used by the 2-sample default implementation are also exposed directly:<a name="line.90"></a>
+<span class="sourceLineNo">091</span> * &lt;ul&gt;<a name="line.91"></a>
+<span class="sourceLineNo">092</span> * &lt;li&gt;{@link #exactP(double, int, int, boolean)} computes exact 2-sample p-values&lt;/li&gt;<a name="line.92"></a>
+<span class="sourceLineNo">093</span> * &lt;li&gt;{@link #monteCarloP(double, int, int, boolean, int)} computes 2-sample p-values by Monte<a name="line.93"></a>
+<span class="sourceLineNo">094</span> * Carlo simulation&lt;/li&gt;<a name="line.94"></a>
+<span class="sourceLineNo">095</span> * &lt;li&gt;{@link #approximateP(double, int, int)} uses the asymptotic distribution The {@code boolean}<a name="line.95"></a>
+<span class="sourceLineNo">096</span> * arguments in the first two methods allow the probability used to estimate the p-value to be<a name="line.96"></a>
+<span class="sourceLineNo">097</span> * expressed using strict or non-strict inequality. See<a name="line.97"></a>
+<span class="sourceLineNo">098</span> * {@link #kolmogorovSmirnovTest(double[], double[], boolean)}.&lt;/li&gt;<a name="line.98"></a>
+<span class="sourceLineNo">099</span> * &lt;/ul&gt;<a name="line.99"></a>
+<span class="sourceLineNo">100</span> * &lt;/p&gt;<a name="line.100"></a>
+<span class="sourceLineNo">101</span> * &lt;p&gt;<a name="line.101"></a>
+<span class="sourceLineNo">102</span> * References:<a name="line.102"></a>
+<span class="sourceLineNo">103</span> * &lt;ul&gt;<a name="line.103"></a>
+<span class="sourceLineNo">104</span> * &lt;li&gt;[1] &lt;a href="http://www.jstatsoft.org/v08/i18/"&gt; Evaluating Kolmogorov's Distribution&lt;/a&gt; by<a name="line.104"></a>
+<span class="sourceLineNo">105</span> * George Marsaglia, Wai Wan Tsang, and Jingbo Wang&lt;/li&gt;<a name="line.105"></a>
+<span class="sourceLineNo">106</span> * &lt;li&gt;[2] &lt;a href="http://www.jstatsoft.org/v39/i11/"&gt; Computing the Two-Sided Kolmogorov-Smirnov<a name="line.106"></a>
+<span class="sourceLineNo">107</span> * Distribution&lt;/a&gt; by Richard Simard and Pierre L'Ecuyer&lt;/li&gt;<a name="line.107"></a>
+<span class="sourceLineNo">108</span> * &lt;/ul&gt;<a name="line.108"></a>
+<span class="sourceLineNo">109</span> * &lt;br/&gt;<a name="line.109"></a>
+<span class="sourceLineNo">110</span> * Note that [1] contains an error in computing h, refer to &lt;a<a name="line.110"></a>
+<span class="sourceLineNo">111</span> * href="https://issues.apache.org/jira/browse/MATH-437"&gt;MATH-437&lt;/a&gt; for details.<a name="line.111"></a>
+<span class="sourceLineNo">112</span> * &lt;/p&gt;<a name="line.112"></a>
+<span class="sourceLineNo">113</span> *<a name="line.113"></a>
+<span class="sourceLineNo">114</span> * @since 3.3<a name="line.114"></a>
+<span class="sourceLineNo">115</span> */<a name="line.115"></a>
+<span class="sourceLineNo">116</span>public class KolmogorovSmirnovTest {<a name="line.116"></a>
+<span class="sourceLineNo">117</span><a name="line.117"></a>
+<span class="sourceLineNo">118</span>    /**<a name="line.118"></a>
+<span class="sourceLineNo">119</span>     * Bound on the number of partial sums in {@link #ksSum(double, double, int)}<a name="line.119"></a>
+<span class="sourceLineNo">120</span>     */<a name="line.120"></a>
+<span class="sourceLineNo">121</span>    protected static final int MAXIMUM_PARTIAL_SUM_COUNT = 100000;<a name="line.121"></a>
+<span class="sourceLineNo">122</span><a name="line.122"></a>
+<span class="sourceLineNo">123</span>    /** Convergence criterion for {@link #ksSum(double, double, int)} */<a name="line.123"></a>
+<span class="sourceLineNo">124</span>    protected static final double KS_SUM_CAUCHY_CRITERION = 1E-20;<a name="line.124"></a>
+<span class="sourceLineNo">125</span><a name="line.125"></a>
+<span class="sourceLineNo">126</span>    /** Convergence criterion for the sums in #pelzGood(double, double, int)} */<a name="line.126"></a>
+<span class="sourceLineNo">127</span>    protected static final double PG_SUM_RELATIVE_ERROR = 1.0e-10;<a name="line.127"></a>
+<span class="sourceLineNo">128</span><a name="line.128"></a>
+<span class="sourceLineNo">129</span>    /** When product of sample sizes is less than this value, 2-sample K-S test is exact */<a name="line.129"></a>
+<span class="sourceLineNo">130</span>    protected static final int SMALL_SAMPLE_PRODUCT = 200;<a name="line.130"></a>
+<span class="sourceLineNo">131</span><a name="line.131"></a>
+<span class="sourceLineNo">132</span>    /**<a name="line.132"></a>
+<span class="sourceLineNo">133</span>     * When product of sample sizes exceeds this value, 2-sample K-S test uses asymptotic<a name="line.133"></a>
+<span class="sourceLineNo">134</span>     * distribution for strict inequality p-value.<a name="line.134"></a>
+<span class="sourceLineNo">135</span>     */<a name="line.135"></a>
+<span class="sourceLineNo">136</span>    protected static final int LARGE_SAMPLE_PRODUCT = 10000;<a name="line.136"></a>
+<span class="sourceLineNo">137</span><a name="line.137"></a>
+<span class="sourceLineNo">138</span>    /** Default number of iterations used by {@link #monteCarloP(double, int, int, boolean, int)} */<a name="line.138"></a>
+<span class="sourceLineNo">139</span>    protected static final int MONTE_CARLO_ITERATIONS = 1000000;<a name="line.139"></a>
+<span class="sourceLineNo">140</span><a name="line.140"></a>
+<span class="sourceLineNo">141</span>    /** Random data generator used by {@link #monteCarloP(double, int, int, boolean, int)} */<a name="line.141"></a>
+<span class="sourceLineNo">142</span>    private final RandomGenerator rng;<a name="line.142"></a>
+<span class="sourceLineNo">143</span><a name="line.143"></a>
+<span class="sourceLineNo">144</span>    /**<a name="line.144"></a>
+<span class="sourceLineNo">145</span>     * Construct a KolmogorovSmirnovTest instance with a default random data generator.<a name="line.145"></a>
+<span class="sourceLineNo">146</span>     */<a name="line.146"></a>
+<span class="sourceLineNo">147</span>    public KolmogorovSmirnovTest() {<a name="line.147"></a>
+<span class="sourceLineNo">148</span>        rng = new Well19937c();<a name="line.148"></a>
+<span class="sourceLineNo">149</span>    }<a name="line.149"></a>
+<span class="sourceLineNo">150</span><a name="line.150"></a>
+<span class="sourceLineNo">151</span>    /**<a name="line.151"></a>
+<span class="sourceLineNo">152</span>     * Construct a KolmogorovSmirnovTest with the provided random data generator.<a name="line.152"></a>
+<span class="sourceLineNo">153</span>     *<a name="line.153"></a>
+<span class="sourceLineNo">154</span>     * @param rng random data generator used by {@link #monteCarloP(double, int, int, boolean, int)}<a name="line.154"></a>
+<span class="sourceLineNo">155</span>     */<a name="line.155"></a>
+<span class="sourceLineNo">156</span>    public KolmogorovSmirnovTest(RandomGenerator rng) {<a name="line.156"></a>
+<span class="sourceLineNo">157</span>        this.rng = rng;<a name="line.157"></a>
+<span class="sourceLineNo">158</span>    }<a name="line.158"></a>
+<span class="sourceLineNo">159</span><a name="line.159"></a>
+<span class="sourceLineNo">160</span>    /**<a name="line.160"></a>
+<span class="sourceLineNo">161</span>     * Computes the &lt;i&gt;p-value&lt;/i&gt;, or &lt;i&gt;observed significance level&lt;/i&gt;, of a one-sample &lt;a<a name="line.161"></a>
+<span class="sourceLineNo">162</span>     * href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"&gt; Kolmogorov-Smirnov test&lt;/a&gt;<a name="line.162"></a>
+<span class="sourceLineNo">163</span>     * evaluating the null hypothesis that {@code data} conforms to {@code distribution}. If<a name="line.163"></a>
+<span class="sourceLineNo">164</span>     * {@code exact} is true, the distribution used to compute the p-value is computed using<a name="line.164"></a>
+<span class="sourceLineNo">165</span>     * extended precision. See {@link #cdfExact(double, int)}.<a name="line.165"></a>
+<span class="sourceLineNo">166</span>     *<a name="line.166"></a>
+<span class="sourceLineNo">167</span>     * @param distribution reference distribution<a name="line.167"></a>
+<span class="sourceLineNo">168</span>     * @param data sample being being evaluated<a name="line.168"></a>
+<span class="sourceLineNo">169</span>     * @param exact whether or not to force exact computation of the p-value<a name="line.169"></a>
+<span class="sourceLineNo">170</span>     * @return the p-value associated with the null hypothesis that {@code data} is a sample from<a name="line.170"></a>
+<span class="sourceLineNo">171</span>     *         {@code distribution}<a name="line.171"></a>
+<span class="sourceLineNo">172</span>     * @throws InsufficientDataException if {@code data} does not have length at least 2<a name="line.172"></a>
+<span class="sourceLineNo">173</span>     * @throws NullArgumentException if {@code data} is null<a name="line.173"></a>
+<span class="sourceLineNo">174</span>     */<a name="line.174"></a>
+<span class="sourceLineNo">175</span>    public double kolmogorovSmirnovTest(RealDistribution distribution, double[] data, boolean exact) {<a name="line.175"></a>
+<span class="sourceLineNo">176</span>        return 1d - cdf(kolmogorovSmirnovStatistic(distribution, data), data.length, exact);<a name="line.176"></a>
+<span class="sourceLineNo">177</span>    }<a name="line.177"></a>
+<span class="sourceLineNo">178</span><a name="line.178"></a>
+<span class="sourceLineNo">179</span>    /**<a name="line.179"></a>
+<span class="sourceLineNo">180</span>     * Computes the one-sample Kolmogorov-Smirnov test statistic, \(D_n=\sup_x |F_n(x)-F(x)|\) where<a name="line.180"></a>
+<span class="sourceLineNo">181</span>     * \(F\) is the distribution (cdf) function associated with {@code distribution}, \(n\) is the<a name="line.181"></a>
+<span class="sourceLineNo">182</span>     * length of {@code data} and \(F_n\) is the empirical distribution that puts mass \(1/n\) at<a name="line.182"></a>
+<span class="sourceLineNo">183</span>     * each of the values in {@code data}.<a name="line.183"></a>
+<span class="sourceLineNo">184</span>     *<a name="line.184"></a>
+<span class="sourceLineNo">185</span>     * @param distribution reference distribution<a name="line.185"></a>
+<span class="sourceLineNo">186</span>     * @param data sample being evaluated<a name="line.186"></a>
+<span class="sourceLineNo">187</span>     * @return Kolmogorov-Smirnov statistic \(D_n\)<a name="line.187"></a>
+<span class="sourceLineNo">188</span>     * @throws InsufficientDataException if {@code data} does not have length at least 2<a name="line.188"></a>
+<span class="sourceLineNo">189</span>     * @throws NullArgumentException if {@code data} is null<a name="line.189"></a>
+<span class="sourceLineNo">190</span>     */<a name="line.190"></a>
+<span class="sourceLineNo">191</span>    public double kolmogorovSmirnovStatistic(RealDistribution distribution, double[] data) {<a name="line.191"></a>
+<span class="sourceLineNo">192</span>        checkArray(data);<a name="line.192"></a>
+<span class="sourceLineNo">193</span>        final int n = data.length;<a name="line.193"></a>
+<span class="sourceLineNo">194</span>        final double nd = n;<a name="line.194"></a>
+<span class="sourceLineNo">195</span>        final double[] dataCopy = new double[n];<a name="line.195"></a>
+<span class="sourceLineNo">196</span>        System.arraycopy(data, 0, dataCopy, 0, n);<a name="line.196"></a>
+<span class="sourceLineNo">197</span>        Arrays.sort(dataCopy);<a name="line.197"></a>
+<span class="sourceLineNo">198</span>        double d = 0d;<a name="line.198"></a>
+<span class="sourceLineNo">199</span>        for (int i = 1; i &lt;= n; i++) {<a name="line.199"></a>
+<span class="sourceLineNo">200</span>            final double yi = distribution.cumulativeProbability(dataCopy[i - 1]);<a name="line.200"></a>
+<span class="sourceLineNo">201</span>            final double currD = FastMath.max(yi - (i - 1) / nd, i / nd - yi);<a name="line.201"></a>
+<span class="sourceLineNo">202</span>            if (currD &gt; d) {<a name="line.202"></a>
+<span class="sourceLineNo">203</span>                d = currD;<a name="line.203"></a>
+<span class="sourceLineNo">204</span>            }<a name="line.204"></a>
+<span class="sourceLineNo">205</span>        }<a name="line.205"></a>
+<span class="sourceLineNo">206</span>        return d;<a name="line.206"></a>
+<span class="sourceLineNo">207</span>    }<a name="line.207"></a>
+<span class="sourceLineNo">208</span><a name="line.208"></a>
+<span class="sourceLineNo">209</span>    /**<a name="line.209"></a>
+<span class="sourceLineNo">210</span>     * Computes the &lt;i&gt;p-value&lt;/i&gt;, or &lt;i&gt;observed significance level&lt;/i&gt;, of a two-sample &lt;a<a name="line.210"></a>
+<span class="sourceLineNo">211</span>     * href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"&gt; Kolmogorov-Smirnov test&lt;/a&gt;<a name="line.211"></a>
+<span class="sourceLineNo">212</span>     * evaluating the null hypothesis that {@code x} and {@code y} are samples drawn from the same<a name="line.212"></a>
+<span class="sourceLineNo">213</span>     * probability distribution. Specifically, what is returned is an estimate of the probability<a name="line.213"></a>
+<span class="sourceLineNo">214</span>     * that the {@link #kolmogorovSmirnovStatistic(double[], double[])} associated with a randomly<a name="line.214"></a>
+<span class="sourceLineNo">215</span>     * selected partition of the combined sample into subsamples of sizes {@code x.length} and<a name="line.215"></a>
+<span class="sourceLineNo">216</span>     * {@code y.length} will strictly exceed (if {@code strict} is {@code true}) or be at least as<a name="line.216"></a>
+<span class="sourceLineNo">217</span>     * large as {@code strict = false}) as {@code kolmogorovSmirnovStatistic(x, y)}.<a name="line.217"></a>
+<span class="sourceLineNo">218</span>     * &lt;ul&gt;<a name="line.218"></a>
+<span class="sourceLineNo">219</span>     * &lt;li&gt;For very small samples (where the product of the sample sizes is less than<a name="line.219"></a>
+<span class="sourceLineNo">220</span>     * {@value #SMALL_SAMPLE_PRODUCT}), the exact distribution is used to compute the p-value. This<a name="line.220"></a>
+<span class="sourceLineNo">221</span>     * is accomplished by enumerating all partitions of the combined sample into two subsamples of<a name="line.221"></a>
+<span class="sourceLineNo">222</span>     * the respective sample sizes, computing \(D_{n,m}\) for each partition and returning the<a name="line.222"></a>
+<span class="sourceLineNo">223</span>     * proportion of partitions that give \(D\) values exceeding the observed value.&lt;/li&gt;<a name="line.223"></a>
+<span class="sourceLineNo">224</span>     * &lt;li&gt;For mid-size samples (product of sample sizes greater than or equal to<a name="line.224"></a>
+<span class="sourceLineNo">225</span>     * {@value #SMALL_SAMPLE_PRODUCT} but less than {@value #LARGE_SAMPLE_PRODUCT}), Monte Carlo<a name="line.225"></a>
+<span class="sourceLineNo">226</span>     * simulation is used to compute the p-value. The simulation randomly generates partitions and<a name="line.226"></a>
+<span class="sourceLineNo">227</span>     * reports the proportion that give \(D\) values exceeding the observed value.&lt;/li&gt;<a name="line.227"></a>
+<span class="sourceLineNo">228</span>     * &lt;li&gt;When the product of the sample sizes exceeds {@value #LARGE_SAMPLE_PRODUCT}, the<a name="line.228"></a>
+<span class="sourceLineNo">229</span>     * asymptotic distribution of \(D_{n,m}\) is used. See {@link #approximateP(double, int, int)}<a name="line.229"></a>
+<span class="sourceLineNo">230</span>     * for details on the approximation.&lt;/li&gt;<a name="line.230"></a>
+<span class="sourceLineNo">231</span>     * &lt;/ul&gt;<a name="line.231"></a>
+<span class="sourceLineNo">232</span>     *<a name="line.232"></a>
+<span class="sourceLineNo">233</span>     * @param x first sample dataset<a name="line.233"></a>
+<span class="sourceLineNo">234</span>     * @param y second sample dataset<a name="line.234"></a>
+<span class="sourceLineNo">235</span>     * @param strict whether or not the probability to compute is expressed as a strict inequality<a name="line.235"></a>
+<span class="sourceLineNo">236</span>     *        (ignored for large samples)<a name="line.236"></a>
+<span class="sourceLineNo">237</span>     * @return p-value associated with the null hypothesis that {@code x} and {@code y} represent<a name="line.237"></a>
+<span class="sourceLineNo">238</span>     *         samples from the same distribution<a name="line.238"></a>
+<span class="sourceLineNo">239</span>     * @throws InsufficientDataException if either {@code x} or {@code y} does not have length at<a name="line.239"></a>
+<span class="sourceLineNo">240</span>     *         least 2<a name="line.240"></a>
+<span class="sourceLineNo">241</span>     * @throws NullArgumentException if either {@code x} or {@code y} is null<a name="line.241"></a>
+<span class="sourceLineNo">242</span>     */<a name="line.242"></a>
+<span class="sourceLineNo">243</span>    public double kolmogorovSmirnovTest(double[] x, double[] y, boolean strict) {<a name="line.243"></a>
+<span class="sourceLineNo">244</span>        final long lengthProduct = (long) x.length * y.length;<a name="line.244"></a>
+<span class="sourceLineNo">245</span>        if (lengthProduct &lt; SMALL_SAMPLE_PRODUCT) {<a name="line.245"></a>
+<span class="sourceLineNo">246</span>            return exactP(kolmogorovSmirnovStatistic(x, y), x.length, y.length, strict);<a name="line.246"></a>
+<span class="sourceLineNo">247</span>        }<a name="line.247"></a>
+<span class="sourceLineNo">248</span>        if (lengthProduct &lt; LARGE_SAMPLE_PRODUCT) {<a name="line.248"></a>
+<span class="sourceLineNo">249</span>            return monteCarloP(kolmogorovSmirnovStatistic(x, y), x.length, y.length, strict, MONTE_CARLO_ITERATIONS);<a name="line.249"></a>
+<span class="sourceLineNo">250</span>        }<a name="line.250"></a>
+<span class="sourceLineNo">251</span>        return approximateP(kolmogorovSmirnovStatistic(x, y), x.length, y.length);<a name="line.251"></a>
+<span class="sourceLineNo">252</span>    }<a name="line.252"></a>
+<span class="sourceLineNo">253</span><a name="line.253"></a>
+<span class="sourceLineNo">254</span>    /**<a name="line.254"></a>
+<span class="sourceLineNo">255</span>     * Computes the &lt;i&gt;p-value&lt;/i&gt;, or &lt;i&gt;observed significance level&lt;/i&gt;, of a two-sample &lt;a<a name="line.255"></a>
+<span class="sourceLineNo">256</span>     * href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"&gt; Kolmogorov-Smirnov test&lt;/a&gt;<a name="line.256"></a>
+<span class="sourceLineNo">257</span>     * evaluating the null hypothesis that {@code x} and {@code y} are samples drawn from the same<a name="line.257"></a>
+<span class="sourceLineNo">258</span>     * probability distribution. Assumes the strict form of the inequality used to compute the<a name="line.258"></a>
+<span class="sourceLineNo">259</span>     * p-value. See {@link #kolmogorovSmirnovTest(RealDistribution, double[], boolean)}.<a name="line.259"></a>
+<span class="sourceLineNo">260</span>     *<a name="line.260"></a>
+<span class="sourceLineNo">261</span>     * @param x first sample dataset<a name="line.261"></a>
+<span class="sourceLineNo">262</span>     * @param y second sample dataset<a name="line.262"></a>
+<span class="sourceLineNo">263</span>     * @return p-value associated with the null hypothesis that {@code x} and {@code y} represent<a name="line.263"></a>
+<span class="sourceLineNo">264</span>     *         samples from the same distribution<a name="line.264"></a>
+<span class="sourceLineNo">265</span>     * @throws InsufficientDataException if either {@code x} or {@code y} does not have length at<a name="line.265"></a>
+<span class="sourceLineNo">266</span>     *         least 2<a name="line.266"></a>
+<span class="sourceLineNo">267</span>     * @throws NullArgumentException if either {@code x} or {@code y} is null<a name="line.267"></a>
+<span class="sourceLineNo">268</span>     */<a name="line.268"></a>
+<span class="sourceLineNo">269</span>    public double kolmogorovSmirnovTest(double[] x, double[] y) {<a name="line.269"></a>
+<span class="sourceLineNo">270</span>        return kolmogorovSmirnovTest(x, y, true);<a name="line.270"></a>
+<span class="sourceLineNo">271</span>    }<a name="line.271"></a>
+<span class="sourceLineNo">272</span><a name="line.272"></a>
+<span class="sourceLineNo">273</span>    /**<a name="line.273"></a>
+<span class="sourceLineNo">274</span>     * Computes the two-sample Kolmogorov-Smirnov test statistic, \(D_{n,m}=\sup_x |F_n(x)-F_m(x)|\)<a name="line.274"></a>
+<span class="sourceLineNo">275</span>     * where \(n\) is the length of {@code x}, \(m\) is the length of {@code y}, \(F_n\) is the<a name="line.275"></a>
+<span class="sourceLineNo">276</span>     * empirical distribution that puts mass \(1/n\) at each of the values in {@code x} and \(F_m\)<a name="line.276"></a>
+<span class="sourceLineNo">277</span>     * is the empirical distribution of the {@code y} values.<a name="line.277"></a>
+<span class="sourceLineNo">278</span>     *<a name="line.278"></a>
+<span class="sourceLineNo">279</span>     * @param x first sample<a name="line.279"></a>
+<span class="sourceLineNo">280</span>     * @param y second sample<a name="line.280"></a>
+<span class="sourceLineNo">281</span>     * @return test statistic \(D_{n,m}\) used to evaluate the null hypothesis that {@code x} and<a name="line.281"></a>
+<span class="sourceLineNo">282</span>     *         {@code y} represent samples from the same underlying distribution<a name="line.282"></a>
+<span class="sourceLineNo">283</span>     * @throws InsufficientDataException if either {@code x} or {@code y} does not have length at<a name="line.283"></a>
+<span class="sourceLineNo">284</span>     *         least 2<a name="line.284"></a>
+<span class="sourceLineNo">285</span>     * @throws NullArgumentException if either {@code x} or {@code y} is null<a name="line.285"></a>
+<span class="sourceLineNo">286</span>     */<a name="line.286"></a>
+<span class="sourceLineNo">287</span>    public double kolmogorovSmirnovStatistic(double[] x, double[] y) {<a name="line.287"></a>
+<span class="sourceLineNo">288</span>        checkArray(x);<a name="line.288"></a>
+<span class="sourceLineNo">289</span>        checkArray(y);<a name="line.289"></a>
+<span class="sourceLineNo">290</span>        // Copy and sort the sample arrays<a name="line.290"></a>
+<span class="sourceLineNo">291</span>        final double[] sx = MathArrays.copyOf(x);<a name="line.291"></a>
+<span class="sourceLineNo">292</span>        final double[] sy = MathArrays.copyOf(y);<a name="line.292"></a>
+<span class="sourceLineNo">293</span>        Arrays.sort(sx);<a name="line.293"></a>
+<span class="sourceLineNo">294</span>        Arrays.sort(sy);<a name="line.294"></a>
+<span class="sourceLineNo">295</span>        final int n = sx.length;<a name="line.295"></a>
+<span class="sourceLineNo">296</span>        final int m = sy.length;<a name="line.296"></a>
+<span class="sourceLineNo">297</span><a name="line.297"></a>
+<span class="sourceLineNo">298</span>        int rankX = 0;<a name="line.298"></a>
+<span class="sourceLineNo">299</span>        int rankY = 0;<a name="line.299"></a>
+<span class="sourceLineNo">300</span><a name="line.300"></a>
+<span class="sourceLineNo">301</span>        // Find the max difference between cdf_x and cdf_y<a name="line.301"></a>
+<span class="sourceLineNo">302</span>        double supD = 0d;<a name="line.302"></a>
+<span class="sourceLineNo">303</span>        do {<a name="line.303"></a>
+<span class="sourceLineNo">304</span>            double z = Double.compare(sx[rankX], sy[rankY]) &lt;= 0 ? sx[rankX] : sy[rankY];<a name="line.304"></a>
+<span class="sourceLineNo">305</span>            while(rankX &lt; n &amp;&amp; Double.compare(sx[rankX], z) == 0) {<a name="line.305"></a>
+<span class="sourceLineNo">306</span>                rankX += 1;<a name="line.306"></a>
+<span class="sourceLineNo">307</span>            }<a name="line.307"></a>
+<span class="sourceLineNo">308</span>            while(rankY &lt; m &amp;&amp; Double.compare(sy[rankY], z) == 0) {<a name="line.308"></a>
+<span class="sourceLineNo">309</span>                rankY += 1;<a name="line.309"></a>
+<span class="sourceLineNo">310</span>            }<a name="line.310"></a>
+<span class="sourceLineNo">311</span>            final double cdf_x = rankX / (double) n;<a name="line.311"></a>
+<span class="sourceLineNo">312</span>            final double cdf_y = rankY / (double) m;<a name="line.312"></a>
+<span class="sourceLineNo">313</span>            final double curD = FastMath.abs(cdf_x - cdf_y);<a name="line.313"></a>
+<span class="sourceLineNo">314</span>            if (curD &gt; supD) {<a name="line.314"></a>
+<span class="sourceLineNo">315</span>                supD = curD;<a name="line.315"></a>
+<span class="sourceLineNo">316</span>            }<a name="line.316"></a>
+<span class="sourceLineNo">317</span>        } while(rankX &lt; n &amp;&amp; rankY &lt; m);<a name="line.317"></a>
+<span class="sourceLineNo">318</span>        return supD;<a name="line.318"></a>
+<span class="sourceLineNo">319</span>    }<a name="line.319"></a>
+<span class="sourceLineNo">320</span><a name="line.320"></a>
+<span class="sourceLineNo">321</span>    /**<a name="line.321"></a>
+<span class="sourceLineNo">322</span>     * Computes the &lt;i&gt;p-value&lt;/i&gt;, or &lt;i&gt;observed significance level&lt;/i&gt;, of a one-sample &lt;a<a name="line.322"></a>
+<span class="sourceLineNo">323</span>     * href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"&gt; Kolmogorov-Smirnov test&lt;/a&gt;<a name="line.323"></a>
+<span class="sourceLineNo">324</span>     * evaluating the null hypothesis that {@code data} conforms to {@code distribution}.<a name="line.324"></a>
+<span class="sourceLineNo">325</span>     *<a name="line.325"></a>
+<span class="sourceLineNo">326</span>     * @param distribution reference distribution<a name="line.326"></a>
+<span class="sourceLineNo">327</span>     * @param data sample being being evaluated<a name="line.327"></a>
+<span class="sourceLineNo">328</span>     * @return the p-value associated with the null hypothesis that {@code data} is a sample from<a name="line.328"></a>
+<span class="sourceLineNo">329</span>     *         {@code distribution}<a name="line.329"></a>
+<span class="sourceLineNo">330</span>     * @throws InsufficientDataException if {@code data} does not have length at least 2<a name="line.330"></a>
+<span class="sourceLineNo">331</span>     * @throws NullArgumentException if {@code data} is null<a name="line.331"></a>
+<span class="sourceLineNo">332</span>     */<a name="line.332"></a>
+<span class="sourceLineNo">333</span>    public double kolmogorovSmirnovTest(RealDistribution distribution, double[] data) {<a name="line.333"></a>
+<span class="sourceLineNo">334</span>        return kolmogorovSmirnovTest(distribution, data, false);<a name="line.334"></a>
+<span class="sourceLineNo">335</span>    }<a name="line.335"></a>
+<span class="sourceLineNo">336</span><a name="line.336"></a>
+<span class="sourceLineNo">337</span>    /**<a name="line.337"></a>
+<span class="sourceLineNo">338</span>     * Performs a &lt;a href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test"&gt; Kolmogorov-Smirnov<a name="line.338"></a>
+<span class="sourceLineNo">339</span>     * test&lt;/a&gt; evaluating the null hypothesis that {@code data} conforms to {@code distribution}.<a name="line.339"></a>
+<span class="sourceLineNo">340</span>     *<a name="line.340"></a>
+<span class="sourceLineNo">341</span>     * @param distribution reference distribution<a name="line.341"></a>
+<span class="sourceLineNo">342</span>     * @param data sample being being evaluated<a name="line.342"></a>
+<span class="sourceLineNo">343</span>     * @param alpha significance level of the test<a name="line.343"></a>
+<span class="sourceLineNo">344</span>     * @return true iff the null hypothesis that {@code data} is a sample from {@code distribution}<a name="line.344"></a>
+<span class="sourceLineNo">345</span>     *         can be rejected with confidence 1 - {@code alpha}<a name="line.345"></a>
+<span class="sourceLineNo">346</span>     * @throws InsufficientDataException if {@code data} does not have length at least 2<a name="line.346"></a>
+<span class="sourceLineNo">347</span>     * @throws NullArgumentException if {@code data} is null<a name="line.347"></a>
+<span class="sourceLineNo">348</span>     */<a name="line.348"></a>
+<span class="sourceLineNo">349</span>    public boolean kolmogorovSmirnovTest(RealDistribution distribution, double[] data, double alpha) {<a name="line.349"></a>
+<span class="sourceLineNo">350</span>        if ((alpha &lt;= 0) || (alpha &gt; 0.5)) {<a name="line.350"></a>
+<span class="sourceLineNo">351</span>            throw new OutOfRangeException(LocalizedFormats.OUT_OF_BOUND_SIGNIFICANCE_LEVEL, alpha, 0, 0.5);<a name="line.351"></a>
+<span class="sourceLineNo">352</span>        }<a name="line.352"></a>
+<span class="sourceLineNo">353</span>        return kolmogorovSmirnovTest(distribution, data) &lt; alpha;<a name="line.353"></a>
+<span class="sourceLineNo">354</span>    }<a name="line.354"></a>
+<span class="sourceLineNo">355</span><a name="line.355"></a>
+<span class="sourceLineNo">356</span>    /**<a name="line.356"></a>
+<span class="sourceLineNo">357</span>     * Calculates \(P(D_n &lt; d)\) using the method described in [1] with quick decisions for extreme<a name="line.357"></a>
+<span class="sourceLineNo">358</span>     * values given in [2] (see above). The result is not exact as with<a name="line.358"></a>
+<span class="sourceLineNo">359</span>     * {@link #cdfExact(double, int)} because calculations are based on<a name="line.359"></a>
+<span class="sourceLineNo">360</span>     * {@code double} rather than {@link org.apache.commons.math3.fraction.BigFraction}.<a name="line.360"></a>
+<span class="sourceLineNo">361</span>     *<a name="line.361"></a>
+<span class="sourceLineNo">362</span>     * @param d statistic<a name="line.362"></a>
+<span class="sourceLineNo">363</span>     * @param n sample size<a name="line.363"></a>
+<span class="sourceLineNo">364</span>     * @return \(P(D_n &lt; d)\)<a name="line.364"></a>
+<span class="sourceLineNo">365</span>     * @throws MathArithmeticException if algorithm fails to convert {@code h} to a<a name="line.365"></a>
+<span class="sourceLineNo">366</span>     *         {@link org.apache.commons.math3.fraction.BigFraction} in expressing {@code d} as \((k<a name="line.366"></a>
+<span class="sourceLineNo">367</span>     *         - h) / m\) for integer {@code k, m} and \(0 \le h &lt; 1\)<a name="line.367"></a>
+<span class="sourceLineNo">368</span>     */<a name="line.368"></a>
+<span class="sourceLineNo">369</span>    public double cdf(double d, int n)<a name="line.369"></a>
+<span class="sourceLineNo">370</span>        throws MathArithmeticException {<a name="line.370"></a>
+<span class="sourceLineNo">371</span>        return cdf(d, n, false);<a name="line.371"></a>
+<span class="sourceLineNo">372</span>    }<a name="line.372"></a>
+<span class="sourceLineNo">373</span><a name="line.373"></a>
+<span class="sourceLineNo">374</span>    /**<a name="line.374"></a>
+<span class="sourceLineNo">375</span>     * Calculates {@code P(D_n &lt; d)}. The result is exact in the sense that BigFraction/BigReal is<a name="line.375"></a>
+<span class="sourceLineNo">376</span>     * used everywhere at the expense of very slow execution time. Almost never choose this in real<a name="line.376"></a>
+<span class="sourceLineNo">377</span>     * applications unless you are very sure; this is almost solely for verification purposes.<a name="line.377"></a>
+<span class="sourceLineNo">378</span>     * Normally, you would choose {@link #cdf(double, int)}. See the class<a name="line.378"></a>
+<span class="sourceLineNo">379</span>     * javadoc for definitions and algorithm description.<a name="line.379"></a>
+<span class="sourceLineNo">380</span>     *<a name="line.380"></a>
+<span class="sourceLineNo">381</span>     * @param d statistic<a name="line.381"></a>
+<span class="sourceLineNo">382</span>     * @param n sample size<a name="line.382"></a>
+<span class="sourceLineNo">383</span>     * @return \(P(D_n &lt; d)\)<a name="line.383"></a>
+<span class="sourceLineNo">384</span>     * @throws MathArithmeticException if the algorithm fails to convert {@code h} to a<a name="line.384"></a>
+<span class="sourceLineNo">385</span>     *         {@link org.apache.commons.math3.fraction.BigFraction} in expressing {@code d} as \((k<a name="line.385"></a>
+<span class="sourceLineNo">386</span>     *         - h) / m\) for integer {@code k, m} and \(0 \le h &lt; 1\)<a name="line.386"></a>
+<span class="sourceLineNo">387</span>     */<a name="line.387"></a>
+<span class="sourceLineNo">388</span>    public double cdfExact(double d, int n)<a name="line.388"></a>
+<span class="sourceLineNo">389</span>        throws MathArithmeticException {<a name="line.389"></a>
+<span class="sourceLineNo">390</span>        return cdf(d, n, true);<a name="line.390"></a>
+<span class="sourceLineNo">391</span>    }<a name="line.391"></a>
+<span class="sourceLineNo">392</span><a name="line.392"></a>
+<span class="sourceLineNo">393</span>    /**<a name="line.393"></a>
+<span class="sourceLineNo">394</span>     * Calculates {@code P(D_n &lt; d)} using method described in [1] with quick decisions for extreme<a name="line.394"></a>
+<span class="sourceLineNo">395</span>     * values given in [2] (see above).<a name="line.395"></a>
+<span class="sourceLineNo">396</span>     *<a name="line.396"></a>
+<span class="sourceLineNo">397</span>     * @param d statistic<a name="line.397"></a>
+<span class="sourceLineNo">398</span>     * @param n sample size<a name="line.398"></a>
+<span class="sourceLineNo">399</span>     * @param exact whether the probability should be calculated exact using<a name="line.399"></a>
+<span class="sourceLineNo">400</span>     *        {@link org.apache.commons.math3.fraction.BigFraction} everywhere at the expense of<a name="line.400"></a>

[... 1205 lines stripped ...]


Mime
View raw message