blob: 868dc53f741ae53430223fdbbc1eb9cba3133b6a [file] [log] [blame]
<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- Generated by javadoc (17) -->
<title>Source code</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="source: package: org.apache.hadoop.hbase.util, class: RandomDistribution">
<meta name="generator" content="javadoc/SourceToHTMLConverter">
<link rel="stylesheet" type="text/css" href="../../../../../../stylesheet.css" title="Style">
</head>
<body class="source-page">
<main role="main">
<div class="source-container">
<pre><span class="source-line-no">001</span><span id="line-1">/*</span>
<span class="source-line-no">002</span><span id="line-2"> * Licensed to the Apache Software Foundation (ASF) under one</span>
<span class="source-line-no">003</span><span id="line-3"> * or more contributor license agreements. See the NOTICE file</span>
<span class="source-line-no">004</span><span id="line-4"> * distributed with this work for additional information</span>
<span class="source-line-no">005</span><span id="line-5"> * regarding copyright ownership. The ASF licenses this file</span>
<span class="source-line-no">006</span><span id="line-6"> * to you under the Apache License, Version 2.0 (the</span>
<span class="source-line-no">007</span><span id="line-7"> * "License"); you may not use this file except in compliance</span>
<span class="source-line-no">008</span><span id="line-8"> * with the License. You may obtain a copy of the License at</span>
<span class="source-line-no">009</span><span id="line-9"> *</span>
<span class="source-line-no">010</span><span id="line-10"> * http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="source-line-no">011</span><span id="line-11"> *</span>
<span class="source-line-no">012</span><span id="line-12"> * Unless required by applicable law or agreed to in writing, software</span>
<span class="source-line-no">013</span><span id="line-13"> * distributed under the License is distributed on an "AS IS" BASIS,</span>
<span class="source-line-no">014</span><span id="line-14"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="source-line-no">015</span><span id="line-15"> * See the License for the specific language governing permissions and</span>
<span class="source-line-no">016</span><span id="line-16"> * limitations under the License.</span>
<span class="source-line-no">017</span><span id="line-17"> */</span>
<span class="source-line-no">018</span><span id="line-18">package org.apache.hadoop.hbase.util;</span>
<span class="source-line-no">019</span><span id="line-19"></span>
<span class="source-line-no">020</span><span id="line-20">import java.util.ArrayList;</span>
<span class="source-line-no">021</span><span id="line-21">import java.util.Arrays;</span>
<span class="source-line-no">022</span><span id="line-22">import java.util.Collections;</span>
<span class="source-line-no">023</span><span id="line-23">import java.util.Random;</span>
<span class="source-line-no">024</span><span id="line-24"></span>
<span class="source-line-no">025</span><span id="line-25">/**</span>
<span class="source-line-no">026</span><span id="line-26"> * A class that generates random numbers that follow some distribution.</span>
<span class="source-line-no">027</span><span id="line-27"> * &lt;p&gt;</span>
<span class="source-line-no">028</span><span id="line-28"> * Copied from &lt;a href="https://issues.apache.org/jira/browse/HADOOP-3315"&gt;hadoop-3315 tfile&lt;/a&gt;.</span>
<span class="source-line-no">029</span><span id="line-29"> * Remove after tfile is committed and use the tfile version of this class instead.</span>
<span class="source-line-no">030</span><span id="line-30"> * &lt;/p&gt;</span>
<span class="source-line-no">031</span><span id="line-31"> */</span>
<span class="source-line-no">032</span><span id="line-32">public class RandomDistribution {</span>
<span class="source-line-no">033</span><span id="line-33"> /**</span>
<span class="source-line-no">034</span><span id="line-34"> * Interface for discrete (integer) random distributions.</span>
<span class="source-line-no">035</span><span id="line-35"> */</span>
<span class="source-line-no">036</span><span id="line-36"> public interface DiscreteRNG {</span>
<span class="source-line-no">037</span><span id="line-37"> /**</span>
<span class="source-line-no">038</span><span id="line-38"> * Get the next random number</span>
<span class="source-line-no">039</span><span id="line-39"> * @return the next random number.</span>
<span class="source-line-no">040</span><span id="line-40"> */</span>
<span class="source-line-no">041</span><span id="line-41"> int nextInt();</span>
<span class="source-line-no">042</span><span id="line-42"> }</span>
<span class="source-line-no">043</span><span id="line-43"></span>
<span class="source-line-no">044</span><span id="line-44"> /**</span>
<span class="source-line-no">045</span><span id="line-45"> * P(i)=1/(max-min)</span>
<span class="source-line-no">046</span><span id="line-46"> */</span>
<span class="source-line-no">047</span><span id="line-47"> public static final class Flat implements DiscreteRNG {</span>
<span class="source-line-no">048</span><span id="line-48"> private final Random random;</span>
<span class="source-line-no">049</span><span id="line-49"> private final int min;</span>
<span class="source-line-no">050</span><span id="line-50"> private final int max;</span>
<span class="source-line-no">051</span><span id="line-51"></span>
<span class="source-line-no">052</span><span id="line-52"> /**</span>
<span class="source-line-no">053</span><span id="line-53"> * Generate random integers from min (inclusive) to max (exclusive) following even distribution.</span>
<span class="source-line-no">054</span><span id="line-54"> * The basic random number generator. Minimum integer maximum integer (exclusive).</span>
<span class="source-line-no">055</span><span id="line-55"> */</span>
<span class="source-line-no">056</span><span id="line-56"> public Flat(Random random, int min, int max) {</span>
<span class="source-line-no">057</span><span id="line-57"> if (min &gt;= max) {</span>
<span class="source-line-no">058</span><span id="line-58"> throw new IllegalArgumentException("Invalid range");</span>
<span class="source-line-no">059</span><span id="line-59"> }</span>
<span class="source-line-no">060</span><span id="line-60"> this.random = random;</span>
<span class="source-line-no">061</span><span id="line-61"> this.min = min;</span>
<span class="source-line-no">062</span><span id="line-62"> this.max = max;</span>
<span class="source-line-no">063</span><span id="line-63"> }</span>
<span class="source-line-no">064</span><span id="line-64"></span>
<span class="source-line-no">065</span><span id="line-65"> /**</span>
<span class="source-line-no">066</span><span id="line-66"> * @see DiscreteRNG#nextInt()</span>
<span class="source-line-no">067</span><span id="line-67"> */</span>
<span class="source-line-no">068</span><span id="line-68"> @Override</span>
<span class="source-line-no">069</span><span id="line-69"> public int nextInt() {</span>
<span class="source-line-no">070</span><span id="line-70"> return random.nextInt(max - min) + min;</span>
<span class="source-line-no">071</span><span id="line-71"> }</span>
<span class="source-line-no">072</span><span id="line-72"> }</span>
<span class="source-line-no">073</span><span id="line-73"></span>
<span class="source-line-no">074</span><span id="line-74"> /**</span>
<span class="source-line-no">075</span><span id="line-75"> * Zipf distribution. The ratio of the probabilities of integer i and j is defined as follows:</span>
<span class="source-line-no">076</span><span id="line-76"> * P(i)/P(j)=((j-min+1)/(i-min+1))^sigma.</span>
<span class="source-line-no">077</span><span id="line-77"> */</span>
<span class="source-line-no">078</span><span id="line-78"> public static final class Zipf implements DiscreteRNG {</span>
<span class="source-line-no">079</span><span id="line-79"> private static final double DEFAULT_EPSILON = 0.001;</span>
<span class="source-line-no">080</span><span id="line-80"> private final Random random;</span>
<span class="source-line-no">081</span><span id="line-81"> private final ArrayList&lt;Integer&gt; k;</span>
<span class="source-line-no">082</span><span id="line-82"> private final ArrayList&lt;Double&gt; v;</span>
<span class="source-line-no">083</span><span id="line-83"></span>
<span class="source-line-no">084</span><span id="line-84"> /**</span>
<span class="source-line-no">085</span><span id="line-85"> * Constructor The random number generator. minimum integer (inclusvie) maximum integer</span>
<span class="source-line-no">086</span><span id="line-86"> * (exclusive) parameter sigma. (sigma &gt; 1.0)</span>
<span class="source-line-no">087</span><span id="line-87"> */</span>
<span class="source-line-no">088</span><span id="line-88"> public Zipf(Random r, int min, int max, double sigma) {</span>
<span class="source-line-no">089</span><span id="line-89"> this(r, min, max, sigma, DEFAULT_EPSILON);</span>
<span class="source-line-no">090</span><span id="line-90"> }</span>
<span class="source-line-no">091</span><span id="line-91"></span>
<span class="source-line-no">092</span><span id="line-92"> /**</span>
<span class="source-line-no">093</span><span id="line-93"> * Constructor. The random number generator. minimum integer (inclusvie) maximum integer</span>
<span class="source-line-no">094</span><span id="line-94"> * (exclusive) parameter sigma. (sigma &gt; 1.0) Allowable error percentage (0 &lt; epsilon &lt; 1.0).</span>
<span class="source-line-no">095</span><span id="line-95"> */</span>
<span class="source-line-no">096</span><span id="line-96"> public Zipf(Random r, int min, int max, double sigma, double epsilon) {</span>
<span class="source-line-no">097</span><span id="line-97"> if ((max &lt;= min) || (sigma &lt;= 1) || (epsilon &lt;= 0) || (epsilon &gt;= 0.5)) {</span>
<span class="source-line-no">098</span><span id="line-98"> throw new IllegalArgumentException("Invalid arguments");</span>
<span class="source-line-no">099</span><span id="line-99"> }</span>
<span class="source-line-no">100</span><span id="line-100"> random = r;</span>
<span class="source-line-no">101</span><span id="line-101"> k = new ArrayList&lt;&gt;();</span>
<span class="source-line-no">102</span><span id="line-102"> v = new ArrayList&lt;&gt;();</span>
<span class="source-line-no">103</span><span id="line-103"></span>
<span class="source-line-no">104</span><span id="line-104"> double sum = 0;</span>
<span class="source-line-no">105</span><span id="line-105"> int last = -1;</span>
<span class="source-line-no">106</span><span id="line-106"> for (int i = min; i &lt; max; ++i) {</span>
<span class="source-line-no">107</span><span id="line-107"> sum += Math.exp(-sigma * Math.log(i - min + 1));</span>
<span class="source-line-no">108</span><span id="line-108"> if ((last == -1) || i * (1 - epsilon) &gt; last) {</span>
<span class="source-line-no">109</span><span id="line-109"> k.add(i);</span>
<span class="source-line-no">110</span><span id="line-110"> v.add(sum);</span>
<span class="source-line-no">111</span><span id="line-111"> last = i;</span>
<span class="source-line-no">112</span><span id="line-112"> }</span>
<span class="source-line-no">113</span><span id="line-113"> }</span>
<span class="source-line-no">114</span><span id="line-114"></span>
<span class="source-line-no">115</span><span id="line-115"> if (last != max - 1) {</span>
<span class="source-line-no">116</span><span id="line-116"> k.add(max - 1);</span>
<span class="source-line-no">117</span><span id="line-117"> v.add(sum);</span>
<span class="source-line-no">118</span><span id="line-118"> }</span>
<span class="source-line-no">119</span><span id="line-119"></span>
<span class="source-line-no">120</span><span id="line-120"> v.set(v.size() - 1, 1.0);</span>
<span class="source-line-no">121</span><span id="line-121"></span>
<span class="source-line-no">122</span><span id="line-122"> for (int i = v.size() - 2; i &gt;= 0; --i) {</span>
<span class="source-line-no">123</span><span id="line-123"> v.set(i, v.get(i) / sum);</span>
<span class="source-line-no">124</span><span id="line-124"> }</span>
<span class="source-line-no">125</span><span id="line-125"> }</span>
<span class="source-line-no">126</span><span id="line-126"></span>
<span class="source-line-no">127</span><span id="line-127"> /**</span>
<span class="source-line-no">128</span><span id="line-128"> * @see DiscreteRNG#nextInt()</span>
<span class="source-line-no">129</span><span id="line-129"> */</span>
<span class="source-line-no">130</span><span id="line-130"> @Override</span>
<span class="source-line-no">131</span><span id="line-131"> public int nextInt() {</span>
<span class="source-line-no">132</span><span id="line-132"> double d = random.nextDouble();</span>
<span class="source-line-no">133</span><span id="line-133"> int idx = Collections.binarySearch(v, d);</span>
<span class="source-line-no">134</span><span id="line-134"></span>
<span class="source-line-no">135</span><span id="line-135"> if (idx &gt; 0) {</span>
<span class="source-line-no">136</span><span id="line-136"> ++idx;</span>
<span class="source-line-no">137</span><span id="line-137"> } else {</span>
<span class="source-line-no">138</span><span id="line-138"> idx = -(idx + 1);</span>
<span class="source-line-no">139</span><span id="line-139"> }</span>
<span class="source-line-no">140</span><span id="line-140"></span>
<span class="source-line-no">141</span><span id="line-141"> if (idx &gt;= v.size()) {</span>
<span class="source-line-no">142</span><span id="line-142"> idx = v.size() - 1;</span>
<span class="source-line-no">143</span><span id="line-143"> }</span>
<span class="source-line-no">144</span><span id="line-144"></span>
<span class="source-line-no">145</span><span id="line-145"> if (idx == 0) {</span>
<span class="source-line-no">146</span><span id="line-146"> return k.get(0);</span>
<span class="source-line-no">147</span><span id="line-147"> }</span>
<span class="source-line-no">148</span><span id="line-148"></span>
<span class="source-line-no">149</span><span id="line-149"> int ceiling = k.get(idx);</span>
<span class="source-line-no">150</span><span id="line-150"> int lower = k.get(idx - 1);</span>
<span class="source-line-no">151</span><span id="line-151"></span>
<span class="source-line-no">152</span><span id="line-152"> return ceiling - random.nextInt(ceiling - lower);</span>
<span class="source-line-no">153</span><span id="line-153"> }</span>
<span class="source-line-no">154</span><span id="line-154"> }</span>
<span class="source-line-no">155</span><span id="line-155"></span>
<span class="source-line-no">156</span><span id="line-156"> /**</span>
<span class="source-line-no">157</span><span id="line-157"> * Binomial distribution. P(k)=select(n, k)*p^k*(1-p)^(n-k) (k = 0, 1, ..., n)</span>
<span class="source-line-no">158</span><span id="line-158"> * P(k)=select(max-min-1, k-min)*p^(k-min)*(1-p)^(k-min)*(1-p)^(max-k-1)</span>
<span class="source-line-no">159</span><span id="line-159"> */</span>
<span class="source-line-no">160</span><span id="line-160"> public static final class Binomial implements DiscreteRNG {</span>
<span class="source-line-no">161</span><span id="line-161"> private final Random random;</span>
<span class="source-line-no">162</span><span id="line-162"> private final int min;</span>
<span class="source-line-no">163</span><span id="line-163"> private final int n;</span>
<span class="source-line-no">164</span><span id="line-164"> private final double[] v;</span>
<span class="source-line-no">165</span><span id="line-165"></span>
<span class="source-line-no">166</span><span id="line-166"> private static double select(int n, int k) {</span>
<span class="source-line-no">167</span><span id="line-167"> double ret = 1.0;</span>
<span class="source-line-no">168</span><span id="line-168"> for (int i = k + 1; i &lt;= n; ++i) {</span>
<span class="source-line-no">169</span><span id="line-169"> ret *= (double) i / (i - k);</span>
<span class="source-line-no">170</span><span id="line-170"> }</span>
<span class="source-line-no">171</span><span id="line-171"> return ret;</span>
<span class="source-line-no">172</span><span id="line-172"> }</span>
<span class="source-line-no">173</span><span id="line-173"></span>
<span class="source-line-no">174</span><span id="line-174"> private static double power(double p, int k) {</span>
<span class="source-line-no">175</span><span id="line-175"> return Math.exp(k * Math.log(p));</span>
<span class="source-line-no">176</span><span id="line-176"> }</span>
<span class="source-line-no">177</span><span id="line-177"></span>
<span class="source-line-no">178</span><span id="line-178"> /**</span>
<span class="source-line-no">179</span><span id="line-179"> * Generate random integers from min (inclusive) to max (exclusive) following Binomial</span>
<span class="source-line-no">180</span><span id="line-180"> * distribution. The basic random number generator. Minimum integer maximum integer (exclusive).</span>
<span class="source-line-no">181</span><span id="line-181"> * parameter.</span>
<span class="source-line-no">182</span><span id="line-182"> */</span>
<span class="source-line-no">183</span><span id="line-183"> public Binomial(Random random, int min, int max, double p) {</span>
<span class="source-line-no">184</span><span id="line-184"> if (min &gt;= max) {</span>
<span class="source-line-no">185</span><span id="line-185"> throw new IllegalArgumentException("Invalid range");</span>
<span class="source-line-no">186</span><span id="line-186"> }</span>
<span class="source-line-no">187</span><span id="line-187"> this.random = random;</span>
<span class="source-line-no">188</span><span id="line-188"> this.min = min;</span>
<span class="source-line-no">189</span><span id="line-189"> this.n = max - min - 1;</span>
<span class="source-line-no">190</span><span id="line-190"> if (n &gt; 0) {</span>
<span class="source-line-no">191</span><span id="line-191"> v = new double[n + 1];</span>
<span class="source-line-no">192</span><span id="line-192"> double sum = 0.0;</span>
<span class="source-line-no">193</span><span id="line-193"> for (int i = 0; i &lt;= n; ++i) {</span>
<span class="source-line-no">194</span><span id="line-194"> sum += select(n, i) * power(p, i) * power(1 - p, n - i);</span>
<span class="source-line-no">195</span><span id="line-195"> v[i] = sum;</span>
<span class="source-line-no">196</span><span id="line-196"> }</span>
<span class="source-line-no">197</span><span id="line-197"> for (int i = 0; i &lt;= n; ++i) {</span>
<span class="source-line-no">198</span><span id="line-198"> v[i] /= sum;</span>
<span class="source-line-no">199</span><span id="line-199"> }</span>
<span class="source-line-no">200</span><span id="line-200"> } else {</span>
<span class="source-line-no">201</span><span id="line-201"> v = null;</span>
<span class="source-line-no">202</span><span id="line-202"> }</span>
<span class="source-line-no">203</span><span id="line-203"> }</span>
<span class="source-line-no">204</span><span id="line-204"></span>
<span class="source-line-no">205</span><span id="line-205"> /**</span>
<span class="source-line-no">206</span><span id="line-206"> * @see DiscreteRNG#nextInt()</span>
<span class="source-line-no">207</span><span id="line-207"> */</span>
<span class="source-line-no">208</span><span id="line-208"> @Override</span>
<span class="source-line-no">209</span><span id="line-209"> public int nextInt() {</span>
<span class="source-line-no">210</span><span id="line-210"> if (v == null) {</span>
<span class="source-line-no">211</span><span id="line-211"> return min;</span>
<span class="source-line-no">212</span><span id="line-212"> }</span>
<span class="source-line-no">213</span><span id="line-213"> double d = random.nextDouble();</span>
<span class="source-line-no">214</span><span id="line-214"> int idx = Arrays.binarySearch(v, d);</span>
<span class="source-line-no">215</span><span id="line-215"> if (idx &gt; 0) {</span>
<span class="source-line-no">216</span><span id="line-216"> ++idx;</span>
<span class="source-line-no">217</span><span id="line-217"> } else {</span>
<span class="source-line-no">218</span><span id="line-218"> idx = -(idx + 1);</span>
<span class="source-line-no">219</span><span id="line-219"> }</span>
<span class="source-line-no">220</span><span id="line-220"></span>
<span class="source-line-no">221</span><span id="line-221"> if (idx &gt;= v.length) {</span>
<span class="source-line-no">222</span><span id="line-222"> idx = v.length - 1;</span>
<span class="source-line-no">223</span><span id="line-223"> }</span>
<span class="source-line-no">224</span><span id="line-224"> return idx + min;</span>
<span class="source-line-no">225</span><span id="line-225"> }</span>
<span class="source-line-no">226</span><span id="line-226"> }</span>
<span class="source-line-no">227</span><span id="line-227">}</span>
</pre>
</div>
</main>
</body>
</html>