From aa8f6c498adece570488832e364a59163c380c61 Mon Sep 17 00:00:00 2001 From: kengz Date: Thu, 25 Jun 2015 00:20:30 -0400 Subject: [PATCH] added _.histogram, improved stats methods --- README.md | 21 +++- docs/README.md | 228 +++++++++++++++++++++++++++++----- docs/annotated/index.html | 234 +++++++++++++++++++++++++++++++---- docs/index.html | 244 ++++++++++++++++++++++++++++++------- docs/tests.html | 53 ++++++-- index.js | 249 +++++++++++++++++++++++++++++++++----- package.json | 5 +- templates/index.ejs.html | 6 +- test/test.js | 75 +++++++++--- 9 files changed, 946 insertions(+), 169 deletions(-) diff --git a/README.md b/README.md index 538bf71..10cd65f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![npm version](https://badge.fury.io/js/lomath.svg)](http://badge.fury.io/js/lomath) [![Build Status](https://travis-ci.org/kengz/lomath.svg?branch=master)](https://travis-ci.org/kengz/lomath) [![Coverage Status](https://coveralls.io/repos/kengz/lomath/badge.svg?branch=master)](https://coveralls.io/r/kengz/lomath?branch=master) [![Dependency Status](https://gemnasium.com/kengz/lomath.svg)](https://gemnasium.com/kengz/lomath) -[**Lomath**](https://github.com/kengz/lomath) is the data analysts' module in `Node JS` - data analysis and visualization in `Node` is now possible with `lomath`. +[**Lomath**](https://github.com/kengz/lomath) is the data analysts' module in `Node JS` - data analysis and visualization in `Node` is now possible with `lomath`. It is the mathematical extension of `lodash` with many high performance functions, generalized and applicable to tensors (multi-arrays). It comes with a standalone plotting module that using `HighCharts` and `BrowserSync`. @@ -14,9 +14,15 @@ See the [API documentation](http://kengz.github.io/lomath/). ## Installation -**Installation**: `npm install lomath` +**Installation**: + +``` +npm install lomath +``` + To use the plotting module, do: + ``` // in the terminal at your project's root, do: cd node_modules/lomath @@ -33,9 +39,9 @@ For clearer terminology, we call `tensors` the generic data structures: | data structure | terminology | |:---|:---| -| 0 | scalar = rank-0 tensor | -| [1, 2, 3] | vector = rank-1 tensor | -| [ [1, 2], [3,4] ] | matrix = rank-2 tensor | +| `0` | scalar = rank-0 tensor | +| `[1, 2, 3]` | vector = rank-1 tensor | +| `[[1, 2], [3, 4]]` | matrix = rank-2 tensor | | ...and so on | rank-n tensor | You can also extend `lomath` and define your own function that applies to tensors, using the function composition module such as `_.distribute, _.asso`, etc. @@ -88,10 +94,13 @@ hc.plot( "Title 2" ) +//optionally you can use the original HighCharts plot options object by: +hc.advPlot(HighChartsOptions); + // Magic here! Finally, the command to render all the plots above. // Pulls up a browser (default to chrome for better support) with the charts. // calling hc.render(true) will autosave all plots to your downloads folder. -hc.render(); +hc.render(); ``` diff --git a/docs/README.md b/docs/README.md index 3735a99..b254f0d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -89,7 +89,9 @@ ## `plotting` +* `advPlot` * `hc` +* `plot` @@ -188,6 +190,7 @@ ## `Methods` +* `histogram` @@ -1468,8 +1471,77 @@ Generalized JS Math applicable to tensor using function composition. +### `advPlot(options)` +# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L2218 "View in source") [Ⓣ][1] + +Method of the constructed `hc` object. +Advanced plotting for users familiar with HighCharts (see http://www.highcharts.com). +This is a highcharts wrapper; takes in a complete HighCharts plot options object. + +#### Arguments +1. `options` *(Object)*: The HighCharts options object. + +#### Returns +*(Object)*: options The options passed, for reference. + +#### Example +```js +// Plots using the highcharts options +hc.advPlot( + chart: { + type: 'column' + }, + title: { + text: 'Monthly Average Rainfall' + }, + subtitle: { + text: 'Source: WorldClimate.com' + }, + xAxis: { + categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'], + crosshair: true + }, + yAxis: { + min: 0, + title: { + text: 'Rainfall (mm)' + } + }, + plotOptions: { + column: { + pointPadding: 0.2, + borderWidth: 0 + } + }, + series: [{ + name: 'Tokyo', + data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0] + + }, { + name: 'New York', + data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5] + + }, { + name: 'London', + data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3] + + }, { + name: 'Berlin', + data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5] + + }] + ) +// renders the plot +hc.render() +``` +* * * + + + + + ### `hc()` -# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L2037 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L2115 "View in source") [Ⓣ][1] The plotting module constructor. Uses `HighCharts` to plot and `browserSync`. Pulls up browser directly showing your charts like magic! @@ -1523,6 +1595,54 @@ hc.render(); + + +### `plot(seriesArr, [title=""], [yLabel=""], [xLabel=""])` +# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L2156 "View in source") [Ⓣ][1] + +Method of the constructed `hc` object. +A simplified wrapper of the HighCharts plot options object. +Allows one to use simple data plot by specifying data sets in objects consisting of data name and data. +The data specified can be array of y-values, or array of x-y values. + +#### Arguments +1. `seriesArr` *(Array)*: The array of data series, i.e. the series objects in the HighCharts options. +2. `[title=""]` *(string)*: The title of this plot. +3. `[yLabel=""]` *(string)*: The y-axis label. +4. `[xLabel=""]` *(string)*: The x-axis label. + +#### Returns +*(Object)*: options The options passed, for reference. + +#### Example +```js +// Plots two data sets using y-values (x-values start from 0). +hc.plot( + [{ + name: "linear", + data: [1, 2, 3, 4, 5, 6] + }, { + name: "square", + data: [1, 4, 9, 16, 25, 36] + }], + "Title 1" + ) + +// Plots a data set using x-y values. +hc.plot( + [{ + name: "square", + data: [[3, 9], [4, 16], [5, 25], [6, 36]] + }], + "Title 2" + ) +// renders the plot +hc.render() +``` +* * * + + + @@ -2082,7 +2202,7 @@ _.sameSig([1, 2, 3], _.isPositive) ### `expGRate(m_f, m_i, t)` -# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L1962 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L2040 "View in source") [Ⓣ][1] Returns the rate of return r in % of an exponential growth, given final value m_f, initial value m_i, and time interval t. Formula: `100 * (Math.exp(Math.log(m_f / m_i) / t) - 1)` @@ -2106,27 +2226,33 @@ _.expGRate(8, 2, 2) // 100% growth rate over 2 years -### `expVal(pV, xV, [fn=identity])` -# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L1895 "View in source") [Ⓣ][1] +### `expVal(X, [P], [fn])` +# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L1901 "View in source") [Ⓣ][1] -Returns the expectation value `E(fn(X))` of a random variable vector, with the corresponding probability vector, using the random variable function (defaulted to identity). +Returns the expectation value `E(fn(X))` of a random variable vector, optionally with the corresponding probability vector, using the random variable function (defaulted to identity). #### Arguments -1. `pV` *(Array)*: The probability vector. -2. `xV` *(Array)*: The corresponding random variable vector. -3. `[fn=identity]` *(Function)*: The random variable function. +1. `X` *(Array)*: The random variable vector. +2. `[P]` *(Array)*: The corresponding probability vector. +3. `[fn]` *(Function)*: The random variable function. #### Returns *(number)*: E(fn(X)) #### Example ```js -var P = [0.1, 0.2, 0.3, 0.4] var X = [-1, 0, 1, 2] -_.expVal(P, X) // E(X) +var Y = [-1,0,0,1,1,1,2,2,2,2] +var P = [0.1, 0.2, 0.3, 0.4] + +_.expVal(Y) // using a raw data array, E(X) +// → ((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4) +_.expVal(X, P) // equivalent to Y, but using X and P: E(X) // → ((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4) -_.expVal(P, X, _.square) // E(X^2) +_.expVal(Y, _.square) // using raw data array, E(X^2) +// → (1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4) +_.expVal(X, P, _.square) // equivalent to Y, but using X and P: E(X^2) // → (1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4) ``` * * * @@ -2160,28 +2286,34 @@ _.mean([[1, 2], [3, 4]]) -### `stdev(pV, xV, [fn=identity])` -# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L1944 "View in source") [Ⓣ][1] +### `stdev(X, [P], [fn])` +# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L1987 "View in source") [Ⓣ][1] Returns the standard deviation `sigma(fn(X))` of a random variable vector, with the corresponding probability vector, using the random variable function (defaulted to identity). Simply calles `_.variance` internally and returns its square root. #### Arguments -1. `pV` *(Array)*: The probability vector. -2. `xV` *(Array)*: The corresponding random variable vector. -3. `[fn=identity]` *(Function)*: The random variable function. +1. `X` *(Array)*: The corresponding random variable vector. +2. `[P]` *(Array)*: The corresponding probability vector. +3. `[fn]` *(Function)*: The random variable function. #### Returns *(number)*: sigma(fn(X)) #### Example ```js -var P = [0.1, 0.2, 0.3, 0.4] var X = [-1, 0, 1, 2] -_.stdev(P, X) // sigma(X) +var Y = [-1,0,0,1,1,1,2,2,2,2] +var P = [0.1, 0.2, 0.3, 0.4] + +_.stdev(Y) // using a raw data array, sigma(X) // → 1 +_.stdev(X, P) // equivalent to Y, but using X and P: sigma(X) +// → ((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4) -_.stdev(P, X, _.square) // sigma(X^2) +_.stdev(Y, _.square) // using raw data array, sigma(X^2) +// → 1.673 +_.stdev(X, P, _.square) // equivalent to Y, but using X and P: sigma(X^2) // → 1.673 ``` * * * @@ -2191,7 +2323,7 @@ _.stdev(P, X, _.square) // sigma(X^2) ### `trailExpGRate(v, t)` -# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L1985 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L2063 "View in source") [Ⓣ][1] Returns the trailing exponential rate of return in the last t years given a vector. Calls `_.expGRate` internally. @@ -2220,27 +2352,33 @@ _.trailExpGRate(v, 3) -### `variance(pV, xV, [fn=identity])` -# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L1919 "View in source") [Ⓣ][1] +### `variance(X, [P], [fn])` +# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L1948 "View in source") [Ⓣ][1] Returns the variance `Var(fn(X))` of a random variable vector, with the corresponding probability vector, using the random variable function (defaulted to identity). #### Arguments -1. `pV` *(Array)*: The probability vector. -2. `xV` *(Array)*: The corresponding random variable vector. -3. `[fn=identity]` *(Function)*: The random variable function. +1. `X` *(Array)*: The random variable vector. +2. `[P]` *(Array)*: The corresponding probability vector. +3. `[fn]` *(Function)*: The random variable function. #### Returns *(number)*: Var(fn(X)) #### Example ```js -var P = [0.1, 0.2, 0.3, 0.4] var X = [-1, 0, 1, 2] -_.variance(P, X) // Var(X) +var Y = [-1,0,0,1,1,1,2,2,2,2] +var P = [0.1, 0.2, 0.3, 0.4] + +_.variance(Y) // using a raw data array, Var(X) // → 1 +_.variance(X, P) // equivalent to Y, but using X and P: Var(X) +// → ((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4) -_.variance(P, X, _.square) // Var(X^2) +_.variance(Y, _.square) // using raw data array, Var(X^2) +// → 2.8 +_.variance(X, P, _.square) // equivalent to Y, but using X and P: Var(X^2) // → 2.8 ``` * * * @@ -2807,6 +2945,40 @@ _.rescale([3, 4]) ## `Methods` + + +### `histogram(data, [fn=_.identity])` +# [Ⓢ](https://github.com/kengz/lomath/blob/master/index.js#L2016 "View in source") [Ⓣ][1] + +Returns a histogram/distribution from the data. This internally calls `_.countBy` to group data by bins, using the function if specified. +Returns the object containing values, frequencies and probabilities as separate array for ease of using them with the statistics methods. + +#### Arguments +1. `data` *(Array)*: An array of data. +2. `[fn=_.identity]` *(Function)*: An optional function to group the data by. + +#### Example +```js +var hist = _.histogram(['a', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'd']); +hist.value +// → ['a', 'b', 'c', 'd'] +hist.freq +// → [1, 2, 3, 4] +hist.prob // normalized freq as probabiltiy distribution +// → [0.1, 0.2, 0.3, 0.4] + +var histfloor = _.histogram([1.1, 2.1, 2.2, 3.1, 3.2, 3.3, 4.1, 4.2, 4.3, 4.4], Math.floor); +histfloor.value +// → [ '1', '2', '3', '4' ] // Note the keys from _.countBy are strings +hist.freq +// → [1, 2, 3, 4] +histfloor.prob +// → [0.1, 0.2, 0.3, 0.4] +``` +* * * + + + diff --git a/docs/annotated/index.html b/docs/annotated/index.html index 13bc396..1c27845 100644 --- a/docs/annotated/index.html +++ b/docs/annotated/index.html @@ -1350,11 +1350,11 @@

index.js

  extend: function(arr, toLen, val) {
     var lendiff = toLen - arr.length,
-      repVal = (val == undefined ? 0 : val);
+      rePal = (val == undefined ? 0 : val);
     if (lendiff < 0)
       throw new Error("Array longer than the length to extend to")
     while (lendiff--)
-      arr.push(repVal);
+      arr.push(rePal);
     return arr;
   },
@@ -2081,11 +2081,8 @@

index.js

-
  expVal: function(pV, xV, fn) {
-    if (fn != undefined)
-      return lomath.dot(pV, lomath.distributeSingle(fn, xV));
-    return lomath.dot(pV, xV);
-  },
+
  expVal: function(X, P, fn) {
+    var val, prob, func;
@@ -2096,42 +2093,163 @@

index.js

+

if only X is specified

+ + + +
    if (arguments.length == 1) {
+      var hist = lomath.histogram(X);
+      val = hist.value,
+      prob = hist.prob;
+    }
+ + + + +
  • +
    + +
    + +
    +

    if X, P specified (maybe fn too)

    + +
    + +
        else if (typeof P === 'object') {
    +      val = X;
    +      prob = P;
    +      func = fn;
    +    }
    + +
  • + + +
  • +
    + +
    + +
    +

    if X, fn specified

    + +
    + +
        else if (typeof P === 'function'){
    +      var hist = lomath.histogram(X);
    +      val = hist.value,
    +      prob = hist.prob;
    +      func = P;
    +    }
    +    if (func != undefined)
    +      return lomath.dot(lomath.distributeSingle(func, val), prob);
    +    return lomath.dot(val, prob);
    +  },
    + +
  • + + +
  • +
    + +
    + +

    return the variance, given probability and value vectors alias Var

    -
      variance: function(pV, xV, fn) {
    -    return fn == undefined ?
    -      lomath.expVal(pV, xV, lomath.a_square) - lomath.a_square(lomath.expVal(pV, xV)) :
    -      lomath.expVal(pV, xV, _.flow(fn, lomath.a_square)) - lomath.a_square(lomath.expVal(pV, xV, fn));
    +            
      variance: function(X, P, fn) {
    + +
  • + + +
  • +
    + +
    + +
    +

    if only X is specified

    + +
    + +
        if (arguments.length == 1) {
    +      return lomath.expVal(X, lomath.a_square) - lomath.a_square(lomath.expVal(X))
    +    }
    + +
  • + + +
  • +
    + +
    + +
    +

    if X, P specified (maybe fn too)

    + +
    + +
        else if (typeof P === 'object') {
    +      return fn == undefined ?
    +        lomath.expVal(X, P, lomath.a_square) - lomath.a_square(lomath.expVal(X, P)) :
    +        lomath.expVal(X, P, _.flow(fn, lomath.a_square)) - lomath.a_square(lomath.expVal(X, P, fn));
    +    }
    + +
  • + + +
  • +
    + +
    + +
    +

    if X, fn specified

    + +
    + +
        else if (typeof P === 'function'){
    +      return lomath.expVal(X, _.flow(P, lomath.a_square)) - lomath.a_square(lomath.expVal(X, P));
    +    }
       },
  • -
  • +
  • - +

    return the variance, given probability and value vectors

    -
      stdev: function(pV, xV, fn) {
    -    return Math.sqrt(lomath.variance(pV, xV, fn));
    +            
      stdev: function(X, P, fn) {
    +    return Math.sqrt(lomath.variance(X, P, fn));
    +  },
    +  histogram: function(data, fn) {
    +    var bin = _.countBy(data, fn),
    +    freq = _.values(bin);
    +    return {
    +      value: _.keys(bin),
    +      freq: freq,
    +      prob: lomath.rescale(freq)
    +    }
       },
  • -
  • +
  • - +

    Calculate the rate of return r in % of an exp growth, given final value m_f, initial value m_i, and time interval t

    @@ -2144,11 +2262,11 @@

    index.js

  • -
  • +
  • - +

    Calculate the trailing exp rate of return in the last t years given a vector v

    @@ -2162,11 +2280,11 @@

    index.js

  • -
  • +
  • - +

    //////////////////////////////////////// Plotting modules: normal and dynamic // @@ -2193,11 +2311,11 @@

    index.js

  • -
  • +
  • - +

    hc: require(__dirname+’/chart/plot.js’).hc

    @@ -2206,17 +2324,83 @@

    index.js

      hc: function() {
         var p = require(__dirname + '/chart/plot.js').p;
         return new p();
    +  },
    +       [{
    +           name: "linear",
    +           data: [1, 2, 3, 4, 5, 6]
    +       }, {
    +           name: "square",
    +           data: [1, 4, 9, 16, 25, 36]
    +       }],
    +       "Title 1"
    +       )
    +       [{
    +           name: "square",
    +           data: [[3, 9], [4, 16], [5, 25], [6, 36]]
    +       }],
    +       "Title 2"
    +       )
    +  plot: function(seriesArr, title, yLabel, xLabel) {
    +    console.log("Please call this method by _.hc.plot");
    +    return 0;
    +  },
    +   chart: {
    +        type: 'column'
    +    },
    +    title: {
    +        text: 'Monthly Average Rainfall'
    +    },
    +    subtitle: {
    +        text: 'Source: WorldClimate.com'
    +    },
    +    xAxis: {
    +        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
    +        crosshair: true
    +    },
    +    yAxis: {
    +        min: 0,
    +        title: {
    +            text: 'Rainfall (mm)'
    +        }
    +    },
    +    plotOptions: {
    +        column: {
    +            pointPadding: 0.2,
    +            borderWidth: 0
    +        }
    +    },
    +    series: [{
    +        name: 'Tokyo',
    +        data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0]
    +
    +    }, {
    +        name: 'New York',
    +        data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5]
    +
    +    }, {
    +        name: 'London',
    +        data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3]
    +
    +    }, {
    +        name: 'Berlin',
    +        data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5]
    +
    +    }]
    +       )
    +  advPlot: function(options) {
    +    console.log("Please call this method by _.hc.advPlot");
    +    return 0;
       }
     })
  • -
  • +
  • - +

    Export lomath as _

    diff --git a/docs/index.html b/docs/index.html index 73f71fe..9d5db45 100644 --- a/docs/index.html +++ b/docs/index.html @@ -9,7 +9,7 @@
    -

    lomath.js

    +

    lomath.js

    • Introduction
    • Github Repository
    • @@ -99,7 +99,9 @@

      native-Math

      plotting

      @@ -189,6 +191,9 @@

      vector

      Methods

      +

      Properties

      @@ -199,14 +204,15 @@

      Properties

      npm version Build Status Coverage Status Dependency Status

      -

      Lomath is the data analysts’ module in Node JS - data analysis and visualization in Node is now possible with lomath.

      +

      Lomath is the data analysts’ module in Node JS - data analysis and visualization in Node is now possible with lomath.

      It is the mathematical extension of lodash with many high performance functions, generalized and applicable to tensors (multi-arrays). It comes with a standalone plotting module that using HighCharts and BrowserSync.

      See the API documentation.

      Community

      Join the chat at https://gitter.im/kengz/lomath

      Installation

      -

      Installation: npm install lomath

      -

      To use the plotting module, do:

      +

      Installation:

      +
      npm install lomath
      +

      To use the plotting module, do:

      // in the terminal at your project's root, do:
       cd node_modules/lomath
       npm install
      @@ -223,15 +229,15 @@ 

      Installation

      -0 +0 scalar = rank-0 tensor -[1, 2, 3] +[1, 2, 3] vector = rank-1 tensor -[ [1, 2], [3,4] ] +[[1, 2], [3, 4]] matrix = rank-2 tensor @@ -242,6 +248,7 @@

      Installation

      You can also extend lomath and define your own function that applies to tensors, using the function composition module such as _.distribute, _.asso, etc.

      Plotting Module

      +

      Sample plot

      lomath comes with a standalone plotting module that using HighCharts and BrowserSync. Just run your JS file normally when you plot (example below), and it will automatically pull up a browser showing you the charts; you can save them!

      Demo: see demo/demo.js for magic.

      var _ = require('lomath');
      @@ -283,20 +290,17 @@ 

      Plotting Module

      "Title 2" ) +//optionally you can use the original HighCharts plot options object by: +hc.advPlot(HighChartsOptions); + // Magic here! Finally, the command to render all the plots above. // Pulls up a browser (default to chrome for better support) with the charts. // calling hc.render(true) will autosave all plots to your downloads folder. hc.render();

      Roadmap

        -
      • Add a demo gif
      • -
      • ✓instruction for using plot
      • add aliases
      • -
      • ✓sample usage
      • -
      • ✓docs
      • -
      • ✓tests
      • performance benchmark
      • -
      • ✓data visualization

      API documentation

      @@ -1250,8 +1254,71 @@

      Returns

      “plotting” Methods

      +

      advPlot(options)

      +

      #

      +

      Method of the constructed hc object. +Advanced plotting for users familiar with HighCharts (see http://www.highcharts.com). +This is a highcharts wrapper; takes in a complete HighCharts plot options object.

      +

      Arguments

      +
        +
      1. options (Object): The HighCharts options object.
      2. +
      +

      Returns

      +

      (Object): options The options passed, for reference.

      +

      Example

      +
      // Plots using the highcharts options
      +hc.advPlot(
      +   chart: {
      +        type: 'column'
      +    },
      +    title: {
      +        text: 'Monthly Average Rainfall'
      +    },
      +    subtitle: {
      +        text: 'Source: WorldClimate.com'
      +    },
      +    xAxis: {
      +        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
      +        crosshair: true
      +    },
      +    yAxis: {
      +        min: 0,
      +        title: {
      +            text: 'Rainfall (mm)'
      +        }
      +    },
      +    plotOptions: {
      +        column: {
      +            pointPadding: 0.2,
      +            borderWidth: 0
      +        }
      +    },
      +    series: [{
      +        name: 'Tokyo',
      +        data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0]
      +
      +    }, {
      +        name: 'New York',
      +        data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5]
      +
      +    }, {
      +        name: 'London',
      +        data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3]
      +
      +    }, {
      +        name: 'Berlin',
      +        data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5]
      +
      +    }]
      +       )
      +// renders the plot
      +hc.render()
      +
      +
      +
      +

      hc()

      -

      #

      +

      #

      The plotting module constructor. Uses HighCharts to plot and browserSync. Pulls up browser directly showing your charts like magic! To use this, go into node_modules/lomath and do npm install there to install the dev dependencies.

      @@ -1299,6 +1366,48 @@

      Example


      +
      +

      plot(seriesArr, [title=""], [yLabel=""], [xLabel=""])

      +

      #

      +

      Method of the constructed hc object. +A simplified wrapper of the HighCharts plot options object. +Allows one to use simple data plot by specifying data sets in objects consisting of data name and data. +The data specified can be array of y-values, or array of x-y values.

      +

      Arguments

      +
        +
      1. seriesArr (Array): The array of data series, i.e. the series objects in the HighCharts options.
      2. +
      3. [title=""] (string): The title of this plot.
      4. +
      5. [yLabel=""] (string): The y-axis label.
      6. +
      7. [xLabel=""] (string): The x-axis label.
      8. +
      +

      Returns

      +

      (Object): options The options passed, for reference.

      +

      Example

      +
      // Plots two data sets using y-values (x-values start from 0).
      +hc.plot(
      +       [{
      +           name: "linear",
      +           data: [1, 2, 3, 4, 5, 6]
      +       }, {
      +           name: "square",
      +           data: [1, 4, 9, 16, 25, 36]
      +       }],
      +       "Title 1"
      +       )
      +
      +// Plots a data set using x-y values.
      +hc.plot(
      +       [{
      +           name: "square",
      +           data: [[3, 9], [4, 16], [5, 25], [6, 36]]
      +       }],
      +       "Title 2"
      +       )
      +// renders the plot
      +hc.render()
      +
      +
      +

      “properties” Methods

      @@ -1720,7 +1829,7 @@

      Example

      “statistics” Methods

      expGRate(m_f, m_i, t)

      -

      #

      +

      #

      Returns the rate of return r in % of an exponential growth, given final value m_f, initial value m_i, and time interval t. Formula: 100 * (Math.exp(Math.log(m_f / m_i) / t) - 1)

      Arguments

      @@ -1738,24 +1847,30 @@

      Example


      -

      expVal(pV, xV, [fn=identity])

      -

      #

      -

      Returns the expectation value E(fn(X)) of a random variable vector, with the corresponding probability vector, using the random variable function (defaulted to identity).

      +

      expVal(X, [P], [fn])

      +

      #

      +

      Returns the expectation value E(fn(X)) of a random variable vector, optionally with the corresponding probability vector, using the random variable function (defaulted to identity).

      Arguments

        -
      1. pV (Array): The probability vector.
      2. -
      3. xV (Array): The corresponding random variable vector.
      4. -
      5. [fn=identity] (Function): The random variable function.
      6. +
      7. X (Array): The random variable vector.
      8. +
      9. [P] (Array): The corresponding probability vector.
      10. +
      11. [fn] (Function): The random variable function.

      Returns

      (number): E(fn(X))

      Example

      -
      var P = [0.1, 0.2, 0.3, 0.4]
      -var X = [-1, 0, 1, 2]
      -_.expVal(P, X) // E(X)
      +
      var X = [-1, 0, 1, 2]
      +var Y = [-1,0,0,1,1,1,2,2,2,2]
      +var P = [0.1, 0.2, 0.3, 0.4]
      +
      +_.expVal(Y) // using a raw data array, E(X)
      +// → ((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4)
      +_.expVal(X, P) // equivalent to Y, but using X and P: E(X)
       // → ((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4)
       
      -_.expVal(P, X, _.square) // E(X^2)
      +_.expVal(Y, _.square) // using raw data array, E(X^2)
      +// → (1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4)
      +_.expVal(X, P, _.square) // equivalent to Y, but using X and P: E(X^2)
       // → (1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4)
       

      @@ -1780,32 +1895,38 @@

      Example


      -

      stdev(pV, xV, [fn=identity])

      -

      #

      +

      stdev(X, [P], [fn])

      +

      #

      Returns the standard deviation sigma(fn(X)) of a random variable vector, with the corresponding probability vector, using the random variable function (defaulted to identity). Simply calles _.variance internally and returns its square root.

      Arguments

        -
      1. pV (Array): The probability vector.
      2. -
      3. xV (Array): The corresponding random variable vector.
      4. -
      5. [fn=identity] (Function): The random variable function.
      6. +
      7. X (Array): The corresponding random variable vector.
      8. +
      9. [P] (Array): The corresponding probability vector.
      10. +
      11. [fn] (Function): The random variable function.

      Returns

      (number): sigma(fn(X))

      Example

      -
      var P = [0.1, 0.2, 0.3, 0.4]
      -var X = [-1, 0, 1, 2]
      -_.stdev(P, X) // sigma(X)
      +
      var X = [-1, 0, 1, 2]
      +var Y = [-1,0,0,1,1,1,2,2,2,2]
      +var P = [0.1, 0.2, 0.3, 0.4]
      +
      +_.stdev(Y) // using a raw data array, sigma(X)
       // → 1
      +_.stdev(X, P) // equivalent to Y, but using X and P: sigma(X)
      +// → ((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4)
       
      -_.stdev(P, X, _.square) // sigma(X^2)
      +_.stdev(Y, _.square) // using raw data array, sigma(X^2)
      +// → 1.673
      +_.stdev(X, P, _.square) // equivalent to Y, but using X and P: sigma(X^2)
       // → 1.673
       

      trailExpGRate(v, t)

      -

      #

      +

      #

      Returns the trailing exponential rate of return in the last t years given a vector. Calls _.expGRate internally.

      Arguments

        @@ -1828,24 +1949,30 @@

        Example


      -

      variance(pV, xV, [fn=identity])

      -

      #

      +

      variance(X, [P], [fn])

      +

      #

      Returns the variance Var(fn(X)) of a random variable vector, with the corresponding probability vector, using the random variable function (defaulted to identity).

      Arguments

        -
      1. pV (Array): The probability vector.
      2. -
      3. xV (Array): The corresponding random variable vector.
      4. -
      5. [fn=identity] (Function): The random variable function.
      6. +
      7. X (Array): The random variable vector.
      8. +
      9. [P] (Array): The corresponding probability vector.
      10. +
      11. [fn] (Function): The random variable function.

      Returns

      (number): Var(fn(X))

      Example

      -
      var P = [0.1, 0.2, 0.3, 0.4]
      -var X = [-1, 0, 1, 2]
      -_.variance(P, X) // Var(X)
      +
      var X = [-1, 0, 1, 2]
      +var Y = [-1,0,0,1,1,1,2,2,2,2]
      +var P = [0.1, 0.2, 0.3, 0.4]
      +
      +_.variance(Y) // using a raw data array, Var(X)
       // → 1
      +_.variance(X, P) // equivalent to Y, but using X and P: Var(X)
      +// → ((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4)
       
      -_.variance(P, X, _.square) // Var(X^2)
      +_.variance(Y, _.square) // using raw data array, Var(X^2)
      +// → 2.8
      +_.variance(X, P, _.square) // equivalent to Y, but using X and P: Var(X^2)
       // → 2.8
       

      @@ -2270,6 +2397,35 @@

      Example

      Methods

      +
      +

      histogram(data, [fn=_.identity])

      +

      #

      +

      Returns a histogram/distribution from the data. This internally calls _.countBy to group data by bins, using the function if specified. +Returns the object containing values, frequencies and probabilities as separate array for ease of using them with the statistics methods.

      +

      Arguments

      +
        +
      1. data (Array): An array of data.
      2. +
      3. [fn=_.identity] (Function): An optional function to group the data by.
      4. +
      +

      Example

      +
      var hist = _.histogram(['a', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'd']);
      +hist.value
      +// → ['a', 'b', 'c', 'd']
      +hist.freq
      +// → [1, 2, 3, 4]
      +hist.prob // normalized freq as probabiltiy distribution
      +// → [0.1, 0.2, 0.3, 0.4]
      +
      +var histfloor = _.histogram([1.1, 2.1, 2.2, 3.1, 3.2, 3.3, 4.1, 4.2, 4.3, 4.4], Math.floor);
      +histfloor.value
      +// → [ '1', '2', '3', '4' ] // Note the keys from _.countBy are strings
      +hist.freq
      +// → [1, 2, 3, 4]
      +histfloor.prob
      +// → [0.1, 0.2, 0.3, 0.4]
      +
      +
      +

      Properties

      diff --git a/docs/tests.html b/docs/tests.html index 353fbaa..34542f9 100644 --- a/docs/tests.html +++ b/docs/tests.html @@ -1111,33 +1111,46 @@

      mean(v)

      -

      expVal(pV, xV, [fn])

      +

      expVal(X, P, [fn])

      -
      default function: identity
      -
      fn(p, v).should.equal((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4)
      -
      specified atomic function: square
      -
      fn(p, v, _.a_square).should.equal(1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4)
      +
      if only X is specified
      +
      fn(vv).should.equal((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4)
      +
      if X, P specified; no fn
      +
      fn(v, p).should.equal((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4)
      +
      if X, fn specified; no P
      +
      fn(vv, _.a_square).should.equal(1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4)
      +
      if X, P, fn specified: atomic function: square
      +
      fn(v, p, _.a_square).should.equal(1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4)
      -

      variance(pV, xV, [fn])

      +

      variance(P, X, [fn])

      -
      default function: identity
      -
      fn(p, v).should.equal(
      +            
      if only X is specified
      +
      fn(vv).should.equal(
         (1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4) -
         _.a_square((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4)
       )
      -
      specified atomic function: square
      -
      fn(p, v, _.a_square).should.be.closeTo(
      +            
      if X, P specified; no fn
      +
      fn(v, p).should.equal(
      +  (1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4) -
      +  _.a_square((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4)
      +)
      +
      if X, fn specified; no P
      +
      fn(vv, _.a_square).should.be.closeTo(
      +  (1 * 0.1 + 0 + 1 * 0.3 + 16 * 0.4) -
      +  _.a_square(1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4), 0.0001)
      +
      if X, P, fn specified: atomic function: square
      +
      fn(v, p, _.a_square).should.be.closeTo(
         (1 * 0.1 + 0 + 1 * 0.3 + 16 * 0.4) -
         _.a_square(1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4), 0.0001)
      -

      stdev(pV, xV, [fn])

      +

      stdev(P, X, [fn])

      trvial sqrt of variance
      -
      _.stdev(p,v).should.equal(
      +            
      _.stdev(v, p).should.equal(
         Math.sqrt(
           (1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4) -
           _.a_square((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4)
      @@ -1145,6 +1158,16 @@ 

      stdev(pV, xV, [fn])

      )
      +
      +

      histogram(data, [fn])

      +
      +
      rate of expGrowth return
      +
      var hist = fn(['a', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'd'])
      +hist.value.should.deep.equal(['a', 'b', 'c', 'd'])
      +hist.freq.should.deep.equal([1, 2, 3, 4])
      +hist.prob.should.deep.equal([0.1, 0.2, 0.3, 0.4])
      +
      +

      expGRate(m_f, m_i, t)

      @@ -1168,6 +1191,12 @@

      plotter

      call constructor
      _.hc().should.not.equal(0)
      +Please call this method by _.hc.plot +
      call plot
      +
      _.plot().should.equal(0)
      +Please call this method by _.hc.advPlot +
      call advPlot
      +
      _.advPlot().should.equal(0)
      diff --git a/index.js b/index.js index ec241ff..8230c23 100644 --- a/index.js +++ b/index.js @@ -1365,11 +1365,11 @@ var lomath = _.mixin({ // Mutates the array extend: function(arr, toLen, val) { var lendiff = toLen - arr.length, - repVal = (val == undefined ? 0 : val); + rePal = (val == undefined ? 0 : val); if (lendiff < 0) throw new Error("Array longer than the length to extend to") while (lendiff--) - arr.push(repVal); + arr.push(rePal); return arr; }, /** @@ -2060,81 +2060,165 @@ var lomath = _.mixin({ return _.sum(v) / v.length; }, /** - * Returns the expectation value `E(fn(X))` of a random variable vector, with the corresponding probability vector, using the random variable function (defaulted to identity). + * Returns the expectation value `E(fn(X))` of a random variable vector, optionally with the corresponding probability vector, using the random variable function (defaulted to identity). * * @category statistics - * @param {Array} pV The probability vector. - * @param {Array} xV The corresponding random variable vector. - * @param {Function} [fn=identity] The random variable function. + * @param {Array} X The random variable vector. + * @param {Array} [P] The corresponding probability vector. + * @param {Function} [fn] The random variable function. * @returns {number} E(fn(X)) * * @example - * var P = [0.1, 0.2, 0.3, 0.4] * var X = [-1, 0, 1, 2] - * _.expVal(P, X) // E(X) + * var Y = [-1,0,0,1,1,1,2,2,2,2] + * var P = [0.1, 0.2, 0.3, 0.4] + * + * _.expVal(Y) // using a raw data array, E(X) + * // → ((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4) + * _.expVal(X, P) // equivalent to Y, but using X and P: E(X) * // → ((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4) * - * _.expVal(P, X, _.square) // E(X^2) + * _.expVal(Y, _.square) // using raw data array, E(X^2) + * // → (1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4) + * _.expVal(X, P, _.square) // equivalent to Y, but using X and P: E(X^2) * // → (1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4) * */ // return the expectation value E(fn(x)), given probability and value vectors, and an optional atomic fn, defaulted to identity E(x). // Note: fn must be atomic // alias E - expVal: function(pV, xV, fn) { - if (fn != undefined) - return lomath.dot(pV, lomath.distributeSingle(fn, xV)); - return lomath.dot(pV, xV); + expVal: function(X, P, fn) { + var val, prob, func; + // if only X is specified + if (arguments.length == 1) { + var hist = lomath.histogram(X); + val = hist.value, + prob = hist.prob; + } + // if X, P specified (maybe fn too) + else if (typeof P === 'object') { + val = X; + prob = P; + func = fn; + } + // if X, fn specified + else if (typeof P === 'function'){ + var hist = lomath.histogram(X); + val = hist.value, + prob = hist.prob; + func = P; + } + if (func != undefined) + return lomath.dot(lomath.distributeSingle(func, val), prob); + return lomath.dot(val, prob); }, /** * Returns the variance `Var(fn(X))` of a random variable vector, with the corresponding probability vector, using the random variable function (defaulted to identity). * * @category statistics - * @param {Array} pV The probability vector. - * @param {Array} xV The corresponding random variable vector. - * @param {Function} [fn=identity] The random variable function. + * @param {Array} X The random variable vector. + * @param {Array} [P] The corresponding probability vector. + * @param {Function} [fn] The random variable function. * @returns {number} Var(fn(X)) * * @example - * var P = [0.1, 0.2, 0.3, 0.4] * var X = [-1, 0, 1, 2] - * _.variance(P, X) // Var(X) + * var Y = [-1,0,0,1,1,1,2,2,2,2] + * var P = [0.1, 0.2, 0.3, 0.4] + * + * _.variance(Y) // using a raw data array, Var(X) * // → 1 + * _.variance(X, P) // equivalent to Y, but using X and P: Var(X) + * // → ((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4) * - * _.variance(P, X, _.square) // Var(X^2) + * _.variance(Y, _.square) // using raw data array, Var(X^2) + * // → 2.8 + * _.variance(X, P, _.square) // equivalent to Y, but using X and P: Var(X^2) * // → 2.8 * */ // return the variance, given probability and value vectors // alias Var - variance: function(pV, xV, fn) { - return fn == undefined ? - lomath.expVal(pV, xV, lomath.a_square) - lomath.a_square(lomath.expVal(pV, xV)) : - lomath.expVal(pV, xV, _.flow(fn, lomath.a_square)) - lomath.a_square(lomath.expVal(pV, xV, fn)); + variance: function(X, P, fn) { + // if only X is specified + if (arguments.length == 1) { + return lomath.expVal(X, lomath.a_square) - lomath.a_square(lomath.expVal(X)) + } + // if X, P specified (maybe fn too) + else if (typeof P === 'object') { + return fn == undefined ? + lomath.expVal(X, P, lomath.a_square) - lomath.a_square(lomath.expVal(X, P)) : + lomath.expVal(X, P, _.flow(fn, lomath.a_square)) - lomath.a_square(lomath.expVal(X, P, fn)); + } + // if X, fn specified + else if (typeof P === 'function'){ + return lomath.expVal(X, _.flow(P, lomath.a_square)) - lomath.a_square(lomath.expVal(X, P)); + } }, /** * Returns the standard deviation `sigma(fn(X))` of a random variable vector, with the corresponding probability vector, using the random variable function (defaulted to identity). * Simply calles `_.variance` internally and returns its square root. * * @category statistics - * @param {Array} pV The probability vector. - * @param {Array} xV The corresponding random variable vector. - * @param {Function} [fn=identity] The random variable function. + * @param {Array} X The corresponding random variable vector. + * @param {Array} [P] The corresponding probability vector. + * @param {Function} [fn] The random variable function. * @returns {number} sigma(fn(X)) * * @example - * var P = [0.1, 0.2, 0.3, 0.4] * var X = [-1, 0, 1, 2] - * _.stdev(P, X) // sigma(X) + * var Y = [-1,0,0,1,1,1,2,2,2,2] + * var P = [0.1, 0.2, 0.3, 0.4] + * + * _.stdev(Y) // using a raw data array, sigma(X) * // → 1 + * _.stdev(X, P) // equivalent to Y, but using X and P: sigma(X) + * // → ((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4) * - * _.stdev(P, X, _.square) // sigma(X^2) + * _.stdev(Y, _.square) // using raw data array, sigma(X^2) + * // → 1.673 + * _.stdev(X, P, _.square) // equivalent to Y, but using X and P: sigma(X^2) * // → 1.673 * */ // return the variance, given probability and value vectors - stdev: function(pV, xV, fn) { - return Math.sqrt(lomath.variance(pV, xV, fn)); + stdev: function(X, P, fn) { + return Math.sqrt(lomath.variance(X, P, fn)); + }, + /** + * Returns a histogram/distribution from the data. This internally calls `_.countBy` to group data by bins, using the function if specified. + * Returns the object containing values, frequencies and probabilities as separate array for ease of using them with the statistics methods. + * + * @param {Array} data An array of data. + * @param {Function} [fn=_.identity] An optional function to group the data by. + * @return {Object} histogram {value, freq, prob}. + * + * @example + * var hist = _.histogram(['a', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'd']); + * hist.value + * // → ['a', 'b', 'c', 'd'] + * hist.freq + * // → [1, 2, 3, 4] + * hist.prob // normalized freq as probabiltiy distribution + * // → [0.1, 0.2, 0.3, 0.4] + * + * var histfloor = _.histogram([1.1, 2.1, 2.2, 3.1, 3.2, 3.3, 4.1, 4.2, 4.3, 4.4], Math.floor); + * histfloor.value + * // → [ '1', '2', '3', '4' ] // Note the keys from _.countBy are strings + * hist.freq + * // → [1, 2, 3, 4] + * histfloor.prob + * // → [0.1, 0.2, 0.3, 0.4] + * + */ + histogram: function(data, fn) { + var bin = _.countBy(data, fn), + freq = _.values(bin); + return { + value: _.keys(bin), + freq: freq, + prob: lomath.rescale(freq) + } }, /** * Returns the rate of return r in % of an exponential growth, given final value m_f, initial value m_i, and time interval t. @@ -2236,6 +2320,109 @@ var lomath = _.mixin({ hc: function() { var p = require(__dirname + '/chart/plot.js').p; return new p(); + }, + /** + * Method of the constructed `hc` object. + * A simplified wrapper of the HighCharts plot options object. + * Allows one to use simple data plot by specifying data sets in objects consisting of data name and data. + * The data specified can be array of y-values, or array of x-y values. + * + * @category plotting + * @param {Array} seriesArr The array of data series, i.e. the series objects in the HighCharts options. + * @param {string} [title=""] The title of this plot. + * @param {string} [yLabel=""] The y-axis label. + * @param {string} [xLabel=""] The x-axis label. + * @returns {Object} options The options passed, for reference. + * + * @example + * // Plots two data sets using y-values (x-values start from 0). + * hc.plot( + [{ + name: "linear", + data: [1, 2, 3, 4, 5, 6] + }, { + name: "square", + data: [1, 4, 9, 16, 25, 36] + }], + "Title 1" + ) + + * // Plots a data set using x-y values. + * hc.plot( + [{ + name: "square", + data: [[3, 9], [4, 16], [5, 25], [6, 36]] + }], + "Title 2" + ) + * // renders the plot + * hc.render() + */ + plot: function(seriesArr, title, yLabel, xLabel) { + console.log("Please call this method by _.hc.plot"); + return 0; + }, + /** + * Method of the constructed `hc` object. + * Advanced plotting for users familiar with HighCharts (see http://www.highcharts.com). + * This is a highcharts wrapper; takes in a complete HighCharts plot options object. + * + * @category plotting + * @param {Object} options The HighCharts options object. + * @returns {Object} options The options passed, for reference. + * + * @example + * // Plots using the highcharts options + * hc.advPlot( + chart: { + type: 'column' + }, + title: { + text: 'Monthly Average Rainfall' + }, + subtitle: { + text: 'Source: WorldClimate.com' + }, + xAxis: { + categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'], + crosshair: true + }, + yAxis: { + min: 0, + title: { + text: 'Rainfall (mm)' + } + }, + plotOptions: { + column: { + pointPadding: 0.2, + borderWidth: 0 + } + }, + series: [{ + name: 'Tokyo', + data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0] + + }, { + name: 'New York', + data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5] + + }, { + name: 'London', + data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3] + + }, { + name: 'Berlin', + data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5] + + }] + ) + * // renders the plot + * hc.render() + */ + advPlot: function(options) { + console.log("Please call this method by _.hc.advPlot"); + return 0; } }) diff --git a/package.json b/package.json index 32cc98d..36bbde7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lomath", - "version": "0.1.0", + "version": "0.1.1", "description": "Lomath is the data analysts’ module in Node JS - data analysis and visualization in Node is now possible with lomath.", "main": "index.js", "scripts": { @@ -35,10 +35,11 @@ "lodash", "math", "data", + "analysis", + "analytics", "visualization", "plot", "graph", - "graphing", "chart", "highcharts", "live", diff --git a/templates/index.ejs.html b/templates/index.ejs.html index e934270..27a5e75 100644 --- a/templates/index.ejs.html +++ b/templates/index.ejs.html @@ -9,12 +9,12 @@ diff --git a/test/test.js b/test/test.js index d7e0df6..52f280a 100644 --- a/test/test.js +++ b/test/test.js @@ -1482,52 +1482,71 @@ suite('statistical', function() { }) //---------------------------------------------- - suite('expVal(pV, xV, [fn])', function() { - var fn, p, v; + suite('expVal(X, P, [fn])', function() { + var fn, p, v, vv; before(function() { fn = _.expVal - p = [0.1, 0.2, 0.3, 0.4] v = [-1, 0, 1, 2] + vv = [-1,0,0,1,1,1,2,2,2,2] + p = [0.1, 0.2, 0.3, 0.4] + }) + test('if only X is specified', function(){ + fn(vv).should.equal((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4) + }) + test('if X, P specified; no fn', function() { + fn(v, p).should.equal((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4) }) - test('default function: identity', function() { - fn(p, v).should.equal((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4) + test('if X, fn specified; no P', function() { + fn(vv, _.a_square).should.equal(1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4) }) - test('specified atomic function: square', function() { - fn(p, v, _.a_square).should.equal(1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4) + test('if X, P, fn specified: atomic function: square', function() { + fn(v, p, _.a_square).should.equal(1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4) }) }) //---------------------------------------------- - suite('variance(pV, xV, [fn])', function() { - var fn, p, v; + suite('variance(P, X, [fn])', function() { + var fn, p, v, vv; before(function() { fn = _.variance - p = [0.1, 0.2, 0.3, 0.4] v = [-1, 0, 1, 2] + vv = [-1,0,0,1,1,1,2,2,2,2] + p = [0.1, 0.2, 0.3, 0.4] }) - test('default function: identity', function() { - fn(p, v).should.equal( + test('if only X is specified', function() { + fn(vv).should.equal( (1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4) - _.a_square((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4) ) }) - test('specified atomic function: square', function() { - fn(p, v, _.a_square).should.be.closeTo( + test('if X, P specified; no fn', function() { + fn(v, p).should.equal( + (1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4) - + _.a_square((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4) + ) + }) + test('if X, fn specified; no P', function() { + fn(vv, _.a_square).should.be.closeTo( + (1 * 0.1 + 0 + 1 * 0.3 + 16 * 0.4) - + _.a_square(1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4), 0.0001) + }) + test('if X, P, fn specified: atomic function: square', function() { + fn(v, p, _.a_square).should.be.closeTo( (1 * 0.1 + 0 + 1 * 0.3 + 16 * 0.4) - _.a_square(1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4), 0.0001) }) }) //---------------------------------------------- - suite('stdev(pV, xV, [fn])', function() { - var fn, p, v; + suite('stdev(P, X, [fn])', function() { + var fn, v, p; before(function() { fn = _.variance - p = [0.1, 0.2, 0.3, 0.4] v = [-1, 0, 1, 2] + p = [0.1, 0.2, 0.3, 0.4] }) test('trvial sqrt of variance', function() { - _.stdev(p,v).should.equal( + _.stdev(v, p).should.equal( Math.sqrt( (1 * 0.1 + 0 + 1 * 0.3 + 4 * 0.4) - _.a_square((-1) * 0.1 + 0 + 1 * 0.3 + 2 * 0.4) @@ -1536,6 +1555,20 @@ suite('statistical', function() { }) }) + //---------------------------------------------- + suite('histogram(data, [fn])', function() { + var fn; + before(function() { + fn = _.histogram + }) + test('rate of expGrowth return', function() { + var hist = fn(['a', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd', 'd']) + hist.value.should.deep.equal(['a', 'b', 'c', 'd']) + hist.freq.should.deep.equal([1, 2, 3, 4]) + hist.prob.should.deep.equal([0.1, 0.2, 0.3, 0.4]) + }) + }) + //---------------------------------------------- suite('expGRate(m_f, m_i, t)', function() { var fn; @@ -1567,4 +1600,10 @@ suite('plotter', function() { test('call constructor', function(){ _.hc().should.not.equal(0) }) + test('call plot', function(){ + _.plot().should.equal(0) + }) + test('call advPlot', function(){ + _.advPlot().should.equal(0) + }) })