You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

630 lines
19 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>quantile - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package quantile</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "github.com/beorn7/perks/quantile"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package quantile computes approximate quantiles over an unbounded data
  69. stream within low memory and CPU bounds.
  70. </p>
  71. <p>
  72. A small amount of accuracy is traded to achieve the above properties.
  73. </p>
  74. <p>
  75. Multiple streams can be merged before calling Query to generate a single set
  76. of results. This is meaningful when the streams represent the same type of
  77. data. See Merge and Samples.
  78. </p>
  79. <p>
  80. For more detailed information about the algorithm used, see:
  81. </p>
  82. <h3 id="hdr-Effective_Computation_of_Biased_Quantiles_over_Data_Streams">Effective Computation of Biased Quantiles over Data Streams</h3>
  83. <p>
  84. <a href="http://www.cs.rutgers.edu/~muthu/bquant.pdf">http://www.cs.rutgers.edu/~muthu/bquant.pdf</a>
  85. </p>
  86. </div>
  87. </div>
  88. <div id="example__mergeMultipleStreams" class="toggle">
  89. <div class="collapsed">
  90. <p class="exampleHeading toggleButton"><span class="text">Example (MergeMultipleStreams)</span></p>
  91. </div>
  92. <div class="expanded">
  93. <p class="exampleHeading toggleButton"><span class="text">Example (MergeMultipleStreams)</span></p>
  94. <p>Code:</p>
  95. <pre class="code">
  96. <span class="comment">// Scenario:</span>
  97. <span class="comment">// We have multiple database shards. On each shard, there is a process</span>
  98. <span class="comment">// collecting query response times from the database logs and inserting</span>
  99. <span class="comment">// them into a Stream (created via NewTargeted(0.90)), much like the</span>
  100. <span class="comment">// Simple example. These processes expose a network interface for us to</span>
  101. <span class="comment">// ask them to serialize and send us the results of their</span>
  102. <span class="comment">// Stream.Samples so we may Merge and Query them.</span>
  103. <span class="comment">//</span>
  104. <span class="comment">// NOTES:</span>
  105. <span class="comment">// * These sample sets are small, allowing us to get them</span>
  106. <span class="comment">// across the network much faster than sending the entire list of data</span>
  107. <span class="comment">// points.</span>
  108. <span class="comment">//</span>
  109. <span class="comment">// * For this to work correctly, we must supply the same quantiles</span>
  110. <span class="comment">// a priori the process collecting the samples supplied to NewTargeted,</span>
  111. <span class="comment">// even if we do not plan to query them all here.</span>
  112. ch := make(chan quantile.Samples)
  113. getDBQuerySamples(ch)
  114. q := quantile.NewTargeted(map[float64]float64{0.90: 0.001})
  115. for samples := range ch {
  116. q.Merge(samples)
  117. }
  118. fmt.Println(&#34;perc90:&#34;, q.Query(0.90))
  119. </pre>
  120. </div>
  121. </div>
  122. <div id="example__simple" class="toggle">
  123. <div class="collapsed">
  124. <p class="exampleHeading toggleButton"><span class="text">Example (Simple)</span></p>
  125. </div>
  126. <div class="expanded">
  127. <p class="exampleHeading toggleButton"><span class="text">Example (Simple)</span></p>
  128. <p>Code:</p>
  129. <pre class="code">ch := make(chan float64)
  130. go sendFloats(ch)
  131. <span class="comment">// Compute the 50th, 90th, and 99th percentile.</span>
  132. q := quantile.NewTargeted(map[float64]float64{
  133. 0.50: 0.005,
  134. 0.90: 0.001,
  135. 0.99: 0.0001,
  136. })
  137. for v := range ch {
  138. q.Insert(v)
  139. }
  140. fmt.Println(&#34;perc50:&#34;, q.Query(0.50))
  141. fmt.Println(&#34;perc90:&#34;, q.Query(0.90))
  142. fmt.Println(&#34;perc99:&#34;, q.Query(0.99))
  143. fmt.Println(&#34;count:&#34;, q.Count())
  144. <span class="comment"></pre>
  145. <p>Output:</p>
  146. <pre class="output">perc50: 5
  147. perc90: 16
  148. perc99: 223
  149. count: 2388
  150. </pre>
  151. </div>
  152. </div>
  153. <div id="example__window" class="toggle">
  154. <div class="collapsed">
  155. <p class="exampleHeading toggleButton"><span class="text">Example (Window)</span></p>
  156. </div>
  157. <div class="expanded">
  158. <p class="exampleHeading toggleButton"><span class="text">Example (Window)</span></p>
  159. <p>Code:</p>
  160. <pre class="code">
  161. <span class="comment">// Scenario: We want the 90th, 95th, and 99th percentiles for each</span>
  162. <span class="comment">// minute.</span>
  163. ch := make(chan float64)
  164. go sendStreamValues(ch)
  165. tick := time.NewTicker(1 * time.Minute)
  166. q := quantile.NewTargeted(map[float64]float64{
  167. 0.90: 0.001,
  168. 0.95: 0.0005,
  169. 0.99: 0.0001,
  170. })
  171. for {
  172. select {
  173. case t := &lt;-tick.C:
  174. flushToDB(t, q.Samples())
  175. q.Reset()
  176. case v := &lt;-ch:
  177. q.Insert(v)
  178. }
  179. }
  180. </pre>
  181. </div>
  182. </div>
  183. <div id="pkg-index" class="toggleVisible">
  184. <div class="collapsed">
  185. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  186. </div>
  187. <div class="expanded">
  188. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  189. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  190. <div id="manual-nav">
  191. <dl>
  192. <dd><a href="index.html#Sample">type Sample</a></dd>
  193. <dd><a href="index.html#Samples">type Samples</a></dd>
  194. <dd>&nbsp; &nbsp; <a href="index.html#Samples.Len">func (a Samples) Len() int</a></dd>
  195. <dd>&nbsp; &nbsp; <a href="index.html#Samples.Less">func (a Samples) Less(i, j int) bool</a></dd>
  196. <dd>&nbsp; &nbsp; <a href="index.html#Samples.Swap">func (a Samples) Swap(i, j int)</a></dd>
  197. <dd><a href="index.html#Stream">type Stream</a></dd>
  198. <dd>&nbsp; &nbsp; <a href="index.html#NewHighBiased">func NewHighBiased(epsilon float64) *Stream</a></dd>
  199. <dd>&nbsp; &nbsp; <a href="index.html#NewLowBiased">func NewLowBiased(epsilon float64) *Stream</a></dd>
  200. <dd>&nbsp; &nbsp; <a href="index.html#NewTargeted">func NewTargeted(targets map[float64]float64) *Stream</a></dd>
  201. <dd>&nbsp; &nbsp; <a href="index.html#Stream.Count">func (s *Stream) Count() int</a></dd>
  202. <dd>&nbsp; &nbsp; <a href="index.html#Stream.Insert">func (s *Stream) Insert(v float64)</a></dd>
  203. <dd>&nbsp; &nbsp; <a href="index.html#Stream.Merge">func (s *Stream) Merge(samples Samples)</a></dd>
  204. <dd>&nbsp; &nbsp; <a href="index.html#Stream.Query">func (s *Stream) Query(q float64) float64</a></dd>
  205. <dd>&nbsp; &nbsp; <a href="index.html#Stream.Reset">func (s *Stream) Reset()</a></dd>
  206. <dd>&nbsp; &nbsp; <a href="index.html#Stream.Samples">func (s *Stream) Samples() Samples</a></dd>
  207. </dl>
  208. </div><!-- #manual-nav -->
  209. <div id="pkg-examples">
  210. <h4>Examples</h4>
  211. <dl>
  212. <dd><a class="exampleLink" href="index.html#example__mergeMultipleStreams">Package (MergeMultipleStreams)</a></dd>
  213. <dd><a class="exampleLink" href="index.html#example__simple">Package (Simple)</a></dd>
  214. <dd><a class="exampleLink" href="index.html#example__window">Package (Window)</a></dd>
  215. </dl>
  216. </div>
  217. <h4>Package files</h4>
  218. <p>
  219. <span style="font-size:90%">
  220. <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go">stream.go</a>
  221. </span>
  222. </p>
  223. </div><!-- .expanded -->
  224. </div><!-- #pkg-index -->
  225. <div id="pkg-callgraph" class="toggle" style="display: none">
  226. <div class="collapsed">
  227. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  228. </div> <!-- .expanded -->
  229. <div class="expanded">
  230. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  231. <p>
  232. In the call graph viewer below, each node
  233. is a function belonging to this package
  234. and its children are the functions it
  235. calls&mdash;perhaps dynamically.
  236. </p>
  237. <p>
  238. The root nodes are the entry points of the
  239. package: functions that may be called from
  240. outside the package.
  241. There may be non-exported or anonymous
  242. functions among them if they are called
  243. dynamically from another package.
  244. </p>
  245. <p>
  246. Click a node to visit that function's source code.
  247. From there you can visit its callers by
  248. clicking its declaring <code>func</code>
  249. token.
  250. </p>
  251. <p>
  252. Functions may be omitted if they were
  253. determined to be unreachable in the
  254. particular programs or tests that were
  255. analyzed.
  256. </p>
  257. <!-- Zero means show all package entry points. -->
  258. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  259. </div>
  260. </div> <!-- #pkg-callgraph -->
  261. <h2 id="Sample">type <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=734:852#L14">Sample</a></h2>
  262. <pre>type Sample struct {
  263. Value <a href="../../../../builtin/index.html#float64">float64</a> `json:&#34;,string&#34;`
  264. Width <a href="../../../../builtin/index.html#float64">float64</a> `json:&#34;,string&#34;`
  265. Delta <a href="../../../../builtin/index.html#float64">float64</a> `json:&#34;,string&#34;`
  266. }</pre>
  267. <p>
  268. Sample holds an observed value and meta information for compression. JSON
  269. tags have been added for convenience.
  270. </p>
  271. <h2 id="Samples">type <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=926:947#L21">Samples</a></h2>
  272. <pre>type Samples []<a href="index.html#Sample">Sample</a></pre>
  273. <p>
  274. Samples represents a slice of samples. It implements sort.Interface.
  275. </p>
  276. <h3 id="Samples.Len">func (Samples) <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=949:975#L23">Len</a></h3>
  277. <pre>func (a <a href="index.html#Samples">Samples</a>) Len() <a href="../../../../builtin/index.html#int">int</a></pre>
  278. <h3 id="Samples.Less">func (Samples) <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=1004:1040#L24">Less</a></h3>
  279. <pre>func (a <a href="index.html#Samples">Samples</a>) Less(i, j <a href="../../../../builtin/index.html#int">int</a>) <a href="../../../../builtin/index.html#bool">bool</a></pre>
  280. <h3 id="Samples.Swap">func (Samples) <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=1076:1107#L25">Swap</a></h3>
  281. <pre>func (a <a href="index.html#Samples">Samples</a>) Swap(i, j <a href="../../../../builtin/index.html#int">int</a>)</pre>
  282. <h2 id="Stream">type <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=3472:3532#L91">Stream</a></h2>
  283. <pre>type Stream struct {
  284. <span class="comment">// contains filtered or unexported fields</span>
  285. }</pre>
  286. <p>
  287. Stream computes quantiles for a stream of float64s. It is not thread-safe by
  288. design. Take care when using across multiple goroutines.
  289. </p>
  290. <h3 id="NewHighBiased">func <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=2334:2377#L56">NewHighBiased</a></h3>
  291. <pre>func NewHighBiased(epsilon <a href="../../../../builtin/index.html#float64">float64</a>) *<a href="index.html#Stream">Stream</a></pre>
  292. <p>
  293. NewHighBiased returns an initialized Stream for high-biased quantiles
  294. (e.g. 0.01, 0.1, 0.5) where the needed quantiles are not known a priori, but
  295. error guarantees can still be given even for the higher ranks of the data
  296. distribution.
  297. </p>
  298. <p>
  299. The provided epsilon is a relative error, i.e. the true quantile of a value
  300. returned by a query is guaranteed to be within 1-(1±Epsilon)*(1-Quantile).
  301. </p>
  302. <p>
  303. See <a href="http://www.cs.rutgers.edu/~muthu/bquant.pdf">http://www.cs.rutgers.edu/~muthu/bquant.pdf</a> for time, space, and error
  304. properties.
  305. </p>
  306. <h3 id="NewLowBiased">func <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=1688:1730#L39">NewLowBiased</a></h3>
  307. <pre>func NewLowBiased(epsilon <a href="../../../../builtin/index.html#float64">float64</a>) *<a href="index.html#Stream">Stream</a></pre>
  308. <p>
  309. NewLowBiased returns an initialized Stream for low-biased quantiles
  310. (e.g. 0.01, 0.1, 0.5) where the needed quantiles are not known a priori, but
  311. error guarantees can still be given even for the lower ranks of the data
  312. distribution.
  313. </p>
  314. <p>
  315. The provided epsilon is a relative error, i.e. the true quantile of a value
  316. returned by a query is guaranteed to be within (1±Epsilon)*Quantile.
  317. </p>
  318. <p>
  319. See <a href="http://www.cs.rutgers.edu/~muthu/bquant.pdf">http://www.cs.rutgers.edu/~muthu/bquant.pdf</a> for time, space, and error
  320. properties.
  321. </p>
  322. <h3 id="NewTargeted">func <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=2944:2997#L70">NewTargeted</a></h3>
  323. <pre>func NewTargeted(targets map[<a href="../../../../builtin/index.html#float64">float64</a>]<a href="../../../../builtin/index.html#float64">float64</a>) *<a href="index.html#Stream">Stream</a></pre>
  324. <p>
  325. NewTargeted returns an initialized Stream concerned with a particular set of
  326. quantile values that are supplied a priori. Knowing these a priori reduces
  327. space and computation time. The targets map maps the desired quantiles to
  328. their absolute errors, i.e. the true quantile of a value returned by a query
  329. is guaranteed to be within (Quantile±Epsilon).
  330. </p>
  331. <p>
  332. See <a href="http://www.cs.rutgers.edu/~muthu/bquant.pdf">http://www.cs.rutgers.edu/~muthu/bquant.pdf</a> for time, space, and error properties.
  333. </p>
  334. <h3 id="Stream.Count">func (*Stream) <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=5246:5274#L164">Count</a></h3>
  335. <pre>func (s *<a href="index.html#Stream">Stream</a>) Count() <a href="../../../../builtin/index.html#int">int</a></pre>
  336. <p>
  337. Count returns the total number of samples observed in the stream
  338. since initialization.
  339. </p>
  340. <h3 id="Stream.Insert">func (*Stream) <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=3683:3717#L103">Insert</a></h3>
  341. <pre>func (s *<a href="index.html#Stream">Stream</a>) Insert(v <a href="../../../../builtin/index.html#float64">float64</a>)</pre>
  342. <p>
  343. Insert inserts v into the stream.
  344. </p>
  345. <h3 id="Stream.Merge">func (*Stream) <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=4764:4803#L142">Merge</a></h3>
  346. <pre>func (s *<a href="index.html#Stream">Stream</a>) Merge(samples <a href="index.html#Samples">Samples</a>)</pre>
  347. <p>
  348. Merge merges samples into the underlying streams samples. This is handy when
  349. merging multiple streams from separate threads, database shards, etc.
  350. </p>
  351. <p>
  352. ATTENTION: This method is broken and does not yield correct results. The
  353. underlying algorithm is not capable of merging streams correctly.
  354. </p>
  355. <h3 id="Stream.Query">func (*Stream) <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=4083:4124#L118">Query</a></h3>
  356. <pre>func (s *<a href="index.html#Stream">Stream</a>) Query(q <a href="../../../../builtin/index.html#float64">float64</a>) <a href="../../../../builtin/index.html#float64">float64</a></pre>
  357. <p>
  358. Query returns the computed qth percentiles value. If s was created with
  359. NewTargeted, and q is not in the set of quantiles provided a priori, Query
  360. will return an unspecified result.
  361. </p>
  362. <h3 id="Stream.Reset">func (*Stream) <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=4932:4956#L148">Reset</a></h3>
  363. <pre>func (s *<a href="index.html#Stream">Stream</a>) Reset()</pre>
  364. <p>
  365. Reset reinitializes and clears the list reusing the samples buffer memory.
  366. </p>
  367. <h3 id="Stream.Samples">func (*Stream) <a href="http://localhost:6060/src/github.com/beorn7/perks/quantile/stream.go?s=5040:5074#L154">Samples</a></h3>
  368. <pre>func (s *<a href="index.html#Stream">Stream</a>) Samples() <a href="index.html#Samples">Samples</a></pre>
  369. <p>
  370. Samples returns stream samples held by s.
  371. </p>
  372. <div id="footer">
  373. Build version go1.6.<br>
  374. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  375. the content of this page is licensed under the
  376. Creative Commons Attribution 3.0 License,
  377. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  378. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  379. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  380. </div>
  381. </div><!-- .container -->
  382. </div><!-- #page -->
  383. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  384. <script type="text/javascript" src="../../../../../lib/godoc/jquery.js"></script>
  385. <script type="text/javascript" src="../../../../../lib/godoc/jquery.treeview.js"></script>
  386. <script type="text/javascript" src="../../../../../lib/godoc/jquery.treeview.edit.js"></script>
  387. <script type="text/javascript" src="../../../../../lib/godoc/godocs.js"></script>
  388. </body>
  389. </html>