Compering 2 normal distributions.
Last active
November 30, 2016 08:24
-
-
Save EfratVil/99462577a475bbad4be1146caca75a58 to your computer and use it in GitHub Desktop.
Normal Distributions
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; | |
} | |
.line { | |
stroke: #000; | |
stroke-width: 1.5px; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
</style> | |
<body> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/jstat/latest/jstat.min.js"></script> | |
<script> | |
var margin = {top: 20, right: 30, bottom: 30, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var array1 = Random_normal_Dist(30, 15); | |
var array2 = Random_normal_Dist(30, 10); | |
var x = d3.scaleLinear() | |
.rangeRound([0, width]); | |
//Min q | |
var d1 = d3.min(array1, function (d) { return d.q; }); | |
var d2 = d3.min(array2, function (d) { return d.q; }); | |
var min_d = d3.min([d1, d2]); | |
//Max q | |
d1 = d3.max(array1, function (d) { return d.q; }); | |
d2 = d3.max(array2, function (d) { return d.q; }); | |
var max_d = d3.max([d1, d2]); | |
//Max p | |
d1 = d3.max(array1, function (d) { return d.p; }); | |
d2 = d3.max(array2, function (d) { return d.p; }); | |
var max_p = d3.max([d1, d2]); | |
x.domain([min_d, max_d]).nice; | |
var y = d3.scaleLinear() | |
.domain([0, max_p]) | |
.range([height, 0]); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
var gX = svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(d3.axisBottom(x)); | |
//var gY = svg.append("g") | |
// .attr("class", "y axis") | |
// .call(d3.axisLeft(y)); | |
var line = d3.line() | |
.x(function (d) { return x(d.q); }) | |
.y(function (d) { return y(d.p); }); | |
svg.append("path") | |
.datum(array1) | |
.attr("class", "line") | |
.attr("d", line) | |
.style("fill", "#fdae61") | |
.style("opacity", "0.5"); | |
svg.append("path") | |
.datum(array2) | |
.attr("class", "line") | |
.attr("d", line) | |
.style("fill", "#4393c3") | |
.style("opacity", "0.5"); | |
function Random_normal_Dist(mean, sd) { | |
data = []; | |
for (var i = mean - 4 * sd; i < mean + 4 * sd; i += 1) { | |
q = i | |
p = jStat.normal.pdf(i, mean, sd); | |
arr = { | |
"q": q, | |
"p": p | |
} | |
data.push(arr); | |
}; | |
return data; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment