Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created August 10, 2013 06:14
Show Gist options
  • Save enjalot/6199296 to your computer and use it in GitHub Desktop.
Save enjalot/6199296 to your computer and use it in GitHub Desktop.
[0,1] functions
{"description":"[0,1] functions","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/LxD0Hf6.png"}
var svg = d3.select("svg");
var w = 400;
var h = 367;
var n = 100;
var f = pdf(0, 0.23);
//f = d3.ease("bounce")
//f = d3.ease("cubic");
//f = d3.ease("quad");
//f = d3.ease("back")
//f = d3.ease("circle")
//f = d3.ease("sin")
//f = d3.ease("poly",4)
var line = d3.svg.line()
.x(function(d) { return d.x })
.y(function(d) { return d.y })
var yscale = d3.scale.linear()
.domain([0, 1])
.range([h/2, 0])
function pdf(Mean, StdDev) {
return function(x) {
var a = x - Mean - 0.5;
return Math.exp(-(a * a) / (2 * StdDev * StdDev)) / (Math.sqrt(2 * Math.PI) * StdDev);
}
}
function cdf(x)
{
if(x === 0) // no approximation necessary for 0
return 0.50;
var t1,t2,t3,t4,t5,qx;
var negative = false;
if(x < 0)
{
x = -x;
negative = true;
}
t1 = 1 / (1 + (0.2316419 * x));
t2 = t1 * t1;
t3 = t2 * t1;
t4 = t3 * t1;
t5 = t4 * t1;
qx = pdf(0,1)(x) * ((0.319381530 * t1) + (-0.356563782 * t2) +
(1.781477937 * t3) + (-1.821255978 * t4) + (1.330274429 * t5));
if(negative == true)
qx = 1 - qx;
return qx;
}
//-------------
var data = d3.range(n).map(function(i) {
var x = i/n;
return {
x: w/2 + w * x,
y: h + yscale(f(x))
}
})
//console.log(data)
svg.append("path")
.datum(data)
.attr("d", line)
.style({
fill: "none",
stroke: "#5283DB",
"stroke-width":4
})
var target = 50;
var random = [];
var num = 402
var count = 0;
var diff = 0;
var dt = target/num;
d3.range(num).forEach(function(i) {
//random.push(cdf(i/num))
count += dt;
diff += dt;
if(Math.floor(diff)) {
random.push(i)
diff = 0;
}
count += cdf(i/num)
})
console.log(random)
console.log(random.length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment