Skip to content

Instantly share code, notes, and snippets.

@darndt
Last active December 10, 2015 02:19
Show Gist options
  • Select an option

  • Save darndt/4366741 to your computer and use it in GitHub Desktop.

Select an option

Save darndt/4366741 to your computer and use it in GitHub Desktop.
Chart projected_performance
<!-- https://gist.github.com/4366741 -->
<!-- http://bl.ocks.org/d/4366741/ -->
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 14px sans-serif;
}
#chart {
font: 12px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
stroke-width: 1.5px;
shape-rendering: crispEdges;
}
/*.x.axis path {
display: none;
}
*/
.rect {
fill: none;
stroke: #000;
stroke-width: 0px;
}
.line {
fill: none;
stroke: #000;
stroke-width: 1.5px;
stroke-dasharray: 9, 5;
}
</style>
<body>
<div id="chart"></div>
<script src="http://d3js.org/d3.v3.js"></script>
<script>
var w = 940,
h = 300,
pad = 20,
left_pad = 100;
var svg = d3.select("#chart")
.append("svg")
.attr("width", w)
.attr("height", h);
var t_min = 0,
t_max = 10,
S_min = 50,
S_max = 150,
investment = 100;;
var x = d3.scale.linear().domain([t_min, t_max]).range([left_pad, w-pad]),
y = d3.scale.linear().domain([S_min, S_max]).range([h-pad*2, pad]);
var xAxis = d3.svg.axis().scale(x),
yAxis = d3.svg.axis().scale(y).orient("left");
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0, "+(h-pad)+")")
.call(xAxis);
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate("+(left_pad-pad)+", 0)")
.call(yAxis);
var color = d3.scale.linear()
.domain([-1, 0, 1])
.range(["red", "white", "green"]);
investment = 100;
mu = 0.02;
sigma = 7;
prob = [];
S_exp_a = [];
d3.range(t_min+0.00001, t_max, 0.1).forEach(function(t){
S_exp = investment * Math.exp(mu*t);
S_exp_a.push([t, S_exp]);
d3.range(S_min, S_max, 0.1).forEach(function(S){
sig = sigma * Math.sqrt(t);
density = 2/Math.sqrt(sig) * Math.exp(-0.5 * Math.pow((S-S_exp)/sig, 2));
if (S < investment) {
density *= -1;
}
prob.push([t,
S,
density]);
});
});
svg.selectAll("rect")
.data(prob)
.enter()
.append("rect")
.attr("class", "rect")
.attr("x", function (d) { return x(d[0]); })
.attr("y", function (d) { return y(d[1]); })
.style("fill", function (d) { return color(d[2]); })
.attr("width", 10)
.attr("height", 1);
var line = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d[0]); })
.y(function(d) { return y(d[1]); })
svg.append("svg:path")
.attr("d", line(S_exp_a))
.attr('class', 'line');
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment