Created
August 7, 2015 20:10
-
-
Save welch/11201d132194074b30cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reducer normal(mean, sigma) { | |
// return a Box-Muller approximate normal with given mean and stddev. | |
// this is written as a custom "reducer" because Box-Muller values come | |
// in pairs, and we want to save one for next time. | |
// | |
var leftover = null; | |
function update() {} | |
function result() { | |
if (leftover != null) { | |
var result = mean + sigma * leftover; | |
leftover = null; | |
return result; | |
} else { | |
var u = 2 * Math.random() - 1; | |
var v = 2 * Math.random() - 1; | |
var r = u*u + v*v; | |
if (r == 0 || r > 1) { | |
// out of bounds, try again | |
return result(); | |
} | |
var c = Math.sqrt(-2*Math.log(r)/r); | |
leftover = u * c; | |
return mean + sigma * v * c; | |
} | |
} | |
} | |
reducer xdp(y, P) { | |
// given a list of data values and corresponding percentiles, return the | |
// derivative. This numerically differentiates the EDF to get an approximate | |
// PDF. It can be a bumpy ride. | |
var Y = []; | |
function update() { | |
Y = *y; //P = *pct; | |
} | |
function iter(X, dPdX, n) { | |
if (X == null || X[n+1] == null) { | |
return dPdX; | |
} else if (n > 0) { | |
dPdX[n] = [(X[n+1]+X[n])/2, (P[n+1] - P[n]) / (X[n+1] - X[n])]; | |
} | |
return iter(X, dPdX, n+1); | |
} | |
function result() { | |
return iter(Y, [0], 0); | |
} | |
} | |
export const PCT = [0, .01, .02, .03, .04,.05,.06,.07, .1, .15, .2, .25, .3, .35, .4, .45, .5, .55, .6, .65, .7, .75, .8, .85, .9, .92,.93,.94,.95,.96, .97, .98, .99, 1]; | |
export sub run() { | |
emit -limit 100000 -every :.01s: | |
| put -acc true x=normal(10,1), y=normal(11, 2) | |
| (@scatterchart -controlField 'x' -valueField 'y' -title 'X vs Y' | |
-display.duration :3s: -display.markerOpacity 0.5; merge) | |
| reduce -acc true -every :3s: px = percentile(x, PCT), py = percentile(y, PCT) | |
| put dpx = xdp(px, PCT), dpy = xdp(py, PCT) | |
| split dpx, dpy | |
| put x=value[0], dp=value[1] | |
| @scatterchart -controlField 'x' -valueField 'dp' -title 'density' -keyField 'name' | |
-display.duration :3s: | |
} | |
run | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment