Built with blockbuilder.org
Last active
October 25, 2016 18:28
-
-
Save herbps10/c54922c40f61edb95f0d6e040b0b3738 to your computer and use it in GitHub Desktop.
IQ Distribution Master
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
license: mit |
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.line { | |
fill: none; | |
stroke: steelblue; | |
stroke-width: 1.5px; | |
} | |
.area { | |
fill: red; | |
} | |
.label { | |
fill: red; | |
} | |
</style> | |
<label>Mean: </label><input id='mean' type='range' min='70' max='130' value='100'><span id='mean-output'>100</span> | |
<br /> | |
<label>Spread: </label><input id='sd' type='range' min='10' max='30' value='15'><span id='sd-output'>15</span> | |
<br/> | |
<svg></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/jstat/latest/jstat.min.js"></script> | |
<script type='text/javascript'> | |
console.log("here"); | |
//setting up empty data array | |
var data = []; | |
var dataBelowCutoff = []; | |
var cutoffCdf = 0; | |
getData(); // popuate data | |
// line chart based on http://bl.ocks.org/mbostock/3883245 | |
var margin = { | |
top: 20, | |
right: 20, | |
bottom: 30, | |
left: 50 | |
}, | |
width = 960 - margin.left - margin.right, | |
height = 400 - margin.top - margin.bottom; | |
var x = d3.scaleLinear() | |
.range([0, width]); | |
var y = d3.scaleLinear() | |
.range([height, 0]); | |
var xAxis = d3.axisBottom() | |
.scale(x); | |
var yAxis = d3.axisLeft() | |
.scale(y); | |
var line = d3.line() | |
.x(function(d) { | |
return x(d.q); | |
}) | |
.y(function(d) { | |
return y(d.p); | |
}); | |
var svg = d3.select("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
x.domain([40, 160]); | |
y.domain(d3.extent(data, function(d) { | |
return d.p; | |
})); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis); | |
var path = svg.append("path") | |
.datum(data) | |
.attr("class", "line") | |
.attr("d", line); | |
var area = d3.area() | |
.x(function(d) { | |
return x(d.q); | |
}) | |
.y(function(d) { | |
return y(d.p); | |
}) | |
.y1(y(0)); | |
var areaPath = svg.append('path') | |
.attr('class', 'area'); | |
var textLabel = svg.append('text') | |
.attr('class', 'label') | |
.attr('x', x(70)); | |
update(); | |
function update() { | |
path.datum(data).attr("d", line); | |
areaPath.datum(dataBelowCutoff).attr("d", area); | |
textLabel | |
.attr('y', y(dataBelowCutoff[dataBelowCutoff.length - 1].p / 2)) | |
.text(d3.format(".1%")(cutoffCdf)); | |
document.getElementById('mean-output').innerHTML = document.getElementById('mean').value; | |
document.getElementById('sd-output').innerHTML = document.getElementById('sd').value; | |
} | |
d3.select('#mean').on('input', function() { | |
getData(); | |
update(); | |
}); | |
d3.select('#sd').on('input', function() { | |
getData(); | |
update(); | |
}) | |
function getData() { | |
var mean = parseFloat(document.getElementById('mean').value); | |
var sd = parseFloat(document.getElementById('sd').value); | |
data = []; | |
// loop to populate data array with | |
// probabily - quantile pairs | |
for (var i = 40; i < 160; i += 1) { | |
q = i | |
p = jStat.normal.pdf(i, mean, sd); | |
el = { | |
"q": q, | |
"p": p | |
} | |
data.push(el); | |
}; | |
dataBelowCutoff = data.slice(0, 30); | |
cutoffCdf = jStat.normal.cdf(70, mean, sd); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment