UNPKG

qminer

Version:

A C++ based data analytics platform for processing large-scale real-time streams containing structured and unstructured data

339 lines (327 loc) 14.7 kB
<!doctype html> <html> <head> <meta name="generator" content="JSDoc 3"> <meta charset="utf-8"> <title>Class: CountWindowGk</title> <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Karla:400,400i,700,700i" type="text/css"> <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Noto+Serif:400,400i,700,700i" type="text/css"> <link rel="stylesheet" href="https://brick.a.ssl.fastly.net/Inconsolata:500" type="text/css"> <link href="css/baseline.css" rel="stylesheet"> </head> <body onload="prettyPrint()"> <nav id="jsdoc-navbar" role="navigation" class="jsdoc-navbar"> <div id="jsdoc-navbar-container"> <div id="jsdoc-navbar-content"> <a href="index.html" class="jsdoc-navbar-package-name">QMiner JavaScript API v9.1.2</a> </div> </div> </nav> <div id="jsdoc-body-container"> <div id="jsdoc-content"> <div id="jsdoc-content-container"> <div id="jsdoc-main" role="main"> <header class="page-header"> <h1><div class="symbol-detail-labels"><span class="label label-kind">class</span>&nbsp;<span class="label label-static">static</span></div><small><a href="module-analytics.html">analytics</a>.<wbr></small><span class="symbol-name">CountWindowGk</span></h1> <p class="source-link">Source: <a href="analyticsdoc.js.html#source-line-2630">analyticsdoc.<wbr>js:2630</a></p> <div class="symbol-classdesc"> <p>Greenwald - Khanna algorithm for quantile estimation on sliding windows. Given a cumulative probability p, the algorithm returns the approximate value of the p-th quantile of all the values in a sliding window.</p> <p> The algorithm works by keeping a summary of buckets. Each bucket summarizes a range of values. Through the run of the algorithm, new buckets are created and old ones merged. To allow for the computation on a sliding window, each bucket uses an Exponential Histogram structure to remember how many values it summarizes.</p> <p> It is summarized in: &quot;Online Algorithm for Approximate Quantile Queries on Sliding Windows&quot; http://dl.acm.org/citation.cfm?id=2954329 </p> <p> The error is not bounded by the absolute value of the output, but by the error in rank of the output element. For instance, if we have 100 elements and query the median, we could get the 48-th (p=0.48), 50-th (p=0.5), 51-th (p=0.51), etc. element.</p> <p> The algorithms error is bounded by two factors. The first is the quantile estimation factor <code>quantileEps</code> occurs because of the summary structure and defines the maximum size of the buckets. The second type of error <code>countEps</code> occurs because of the Exponential Histograms inside the buckets.</p> <p> The worst-case error is bounded by (quantileEps + 2*countEps + O(countEps^2)), although in practice the error is lower.</p> <p> This version of the algorithm uses a count-based fixed size sliding window.</p> </div> <dl class="dl-compact"> </dl> </header> <section id="summary"> <div class="summary-callout"> <h2 class="summary-callout-heading">Methods</h2> <div class="summary-content"> <div class="summary-column"> <dl class="dl-summary-callout"> <dt><a href="module-analytics.CountWindowGk.html#.partialFit">partialFit(val)</a></dt> <dd> </dd> <dt><a href="module-analytics.CountWindowGk.html#.save">save(fout)</a></dt> <dd> </dd> </dl> </div> <div class="summary-column"> <dl class="dl-summary-callout"> <dt><a href="module-analytics.CountWindowGk.html#getParams">getParams()</a></dt> <dd> </dd> <dt><a href="module-analytics.CountWindowGk.html#predict">predict(p)</a></dt> <dd> </dd> </dl> </div> <div class="summary-column"> </div> </div> </div> </section> <section> <h2 id="CountWindowGk">new&nbsp;<span class="symbol-name">CountWindowGk</span><span class="signature"><span class="signature-params">([arg])</span></span></h2> <section> <h3> Example </h3> <div> <pre class="prettyprint"><code>// import modules var qm = require(&#x27;qminer&#x27;); var fs = qm.fs; var analytics = qm.analytics; // create the default TDigest object var gk = new analytics.CountWindowGk({ windowSize: 5, quantileEps: 0.001, countEps: 0.0005 }); // create the data used for calculating quantiles var inputs = [10, 1, 2, 8, 9, 5, 6, 4, 7, 3]; // fit the TDigest model for (var i = 0; i &lt; inputs.length; i++) { gk.partialFit(inputs[i]); } // make the prediction for the 0.1 quantile var prediction = gk.predict(0.1); // save the model gk.save(fs.openWrite(&#x27;gk.bin&#x27;)).close(); // open the gk model under a new variable var gk2 = new analytics.CountWindowGk(fs.openRead(&#x27;gk.bin&#x27;));</code></pre> </div> </section> <section> <h3>Parameter</h3> <table class="jsdoc-details-table"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Optional</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td> <p>arg</p> </td> <td> <p>(module:analytics~FixedWindowGkParam or <a href="module-fs.FIn.html">module:fs.FIn</a>)</p> </td> <td> <p>Yes</p> </td> <td> <p>Construction arguments. There are two ways of constructing: <br>1. Using the module:analytics~FixedWindowGkParam object, <br>2. using the file input stream <a href="module-fs.FIn.html">module:fs.FIn</a>.</p> </td> </tr> </tbody> </table> </section> <dl class="dl-compact"> </dl> </section> <section> <h2>Methods</h2> <section> <h3 id=".partialFit"><div class="symbol-detail-labels"><span class="label label-static">static</span></div><span class="symbol-name">partialFit</span><span class="signature"><span class="signature-params">(val)</span>&nbsp;&rarr; <span class="signature-returns"> <a href="module-analytics.CountWindowGk.html">module:analytics.CountWindowGk</a></span></span></h3> <p>Appends a new value to the sliding window. If an old value falls outside the sliding window, it is forgotten.</p> <section> <h4> Example </h4> <div> <pre class="prettyprint"><code>var qm = require(&#x27;qminer&#x27;); var gk = new qm.analytics.CountWindowGk(); gk.partialFit(1.0); gk.partialFit(2.0);</code></pre> </div> </section> <section> <h4>Parameter</h4> <table class="jsdoc-details-table"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Optional</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td> <p>val</p> </td> <td> <p>number</p> </td> <td> <p>&nbsp;</p> </td> <td> <p>the value</p> </td> </tr> </tbody> </table> </section> <dl class="dl-compact"> <dt>Returns</dt> <dd> <p><code><a href="module-analytics.CountWindowGk.html">module:analytics.CountWindowGk</a></code>&nbsp;reference to self</p> </dd> </dl> <h3 id=".save"><div class="symbol-detail-labels"><span class="label label-static">static</span></div><span class="symbol-name">save</span><span class="signature"><span class="signature-params">(fout)</span>&nbsp;&rarr; <span class="signature-returns"> <a href="module-fs.FOut.html">module:fs.FOut</a></span></span></h3> <p>Saves the objects state into a binary file.</p> <section> <h4> Example </h4> <div> <pre class="prettyprint"><code>var qm = require(&#x27;qminer&#x27;); var fs = qm.fs; var gk = new qm.analytics.CountWindowGk(); // save the model gk.save(fs.openWrite(&#x27;gk.bin&#x27;)).close(); // open the model under a new variable var gk = new analytics.CountWindowGk(fs.openRead(&#x27;gk.bin&#x27;));</code></pre> </div> </section> <section> <h4>Parameter</h4> <table class="jsdoc-details-table"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Optional</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td> <p>fout</p> </td> <td> <p><a href="module-fs.FOut.html">module:fs.FOut</a></p> </td> <td> <p>&nbsp;</p> </td> <td> <p>the output stream</p> </td> </tr> </tbody> </table> </section> <dl class="dl-compact"> <dt>Returns</dt> <dd> <p><code><a href="module-fs.FOut.html">module:fs.FOut</a></code>&nbsp;the output stream <code>fout</code></p> </dd> </dl> <h3 id="getParams"><span class="symbol-name">getParams</span><span class="signature"><span class="signature-params">()</span>&nbsp;&rarr; <span class="signature-returns"> module:analytics~FixedWindowGkParam</span></span></h3> <p>Returns the models' parameters as a JavaScript object (JSON). These parameters are the same as are set through the constructor.</p> <dl class="dl-compact"> <dt>Returns</dt> <dd> <p><code>module:analytics~FixedWindowGkParam</code>&nbsp;The construction parameters.</p> <p>var analytics = qm.analytics; var gk = new qm.analytics.CountWindowGk({ windowSize: 100 }); // window 100 elements long gk.partialFit(1.0); gk.partialFit(2.0); gk.partialFit(1.0); gk.partialFit(3.0); gk.partialFit(2.0); var params = gk.getParams();</p> <p>console.log(params.windowSize); console.log(params.quantileEps); console.log(params.countEps); </p> </dd> </dl> <h3 id="predict"><span class="symbol-name">predict</span><span class="signature"><span class="signature-params">(p)</span>&nbsp;&rarr; <span class="signature-returns"> number</span></span></h3> <p>Given an input cumulative probability, returns a quantile associated with that probability (e.g. for input 0.5 it will return the median).</p> <section> <h4> Example </h4> <div> <pre class="prettyprint"><code>var qm = require(&#x27;qminer&#x27;); var gk = new qm.analytics.CountWindowGk({ windowSize: 100 // window 100 elements long }); gk.partialFit(1.0); gk.partialFit(2.0); gk.partialFit(1.0); gk.partialFit(3.0); gk.partialFit(2.0); console.log(gk.predict(0.01)); // prints the first percentile console.log(gk.predict(0.25)); // prints the first quartile console.log(gk.predict(0.5)); // prints the median</code></pre> </div> </section> <section> <h4>Parameter</h4> <table class="jsdoc-details-table"> <thead> <tr> <th>Name</th> <th>Type</th> <th>Optional</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td> <p>p</p> </td> <td> <p>number</p> </td> <td> <p>&nbsp;</p> </td> <td> <p>cumulative probability between 0 and 1 (both inclusive)</p> </td> </tr> </tbody> </table> </section> <dl class="dl-compact"> <dt>Returns</dt> <dd> <p><code>number</code>&nbsp;quantile associated with p</p> </dd> </dl> </section> </section> </div> </div> <nav id="jsdoc-toc-nav" role="navigation"></nav> </div> </div> <footer id="jsdoc-footer" class="jsdoc-footer"> <div id="jsdoc-footer-container"> <p> </p> </div> </footer> <script src="scripts/jquery.min.js"></script> <script src="scripts/jquery.cookie.js"></script> <script src="scripts/tree.jquery.js"></script> <script src="scripts/prettify.js"></script> <script src="scripts/jsdoc-toc.js"></script> <script src="scripts/linenumber.js"></script> <script src="scripts/scrollanchor.js"></script> </body> </html>