Display chart of future spot prices

This commit is contained in:
Kevin
2017-09-18 23:41:34 -04:00
parent 04279d77f0
commit a5b5f64428
4 changed files with 102 additions and 3 deletions

View File

@@ -14,7 +14,7 @@ $(document).ready(function() {
function serializeForm(id) {
//
return $(id).serializeArray()
.reduce(function(a, x) { a[x.name] = x.value; return a; }, {});
.reduce(function(a, x) { a[x.name] = x.value; return a; }, {});
}
function sendRequest() {
@@ -42,13 +42,15 @@ function sendRequest() {
success: function(e) {
optionResults = e;
updateTable();
updateGraph();
let requestTime = new Date().getTime() - startTime;
console.log("Request time: ", requestTime);
console.log("Request time(s):", requestTime / 1000);
},
});
}
function updateTable() {
// TODO
$("#results-table > tbody > tr").each(function(idx, el) {
$($(el).children()[1])[0].innerText =
optionResults["ClosedForm"][$($(el).children()[0]).text()].toFixed(4);
@@ -57,3 +59,83 @@ function updateTable() {
});
}
function updateGraph() {
// TODO
var strippedData = optionResults.MonteCarlo.Levels.map(function(i) {
return Number(i.toFixed(0));
});
// Count the elements
var counts = {};
var minY = 0;
for (var i=0; i<strippedData.length; i++) {
var num = strippedData[i];
counts[num] = counts[num] ? counts[num] + 1 : 1;
}
var summaryData = [];
Object.keys(counts).forEach( function(key) {
summaryData.push({'key': key, 'value': counts[key]});
});
var margin = {top: 10, right: 20, bottom: 20, left: 40},
width = 500 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
$("#right-upper").empty();
var svg = d3.select("#right-upper").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 x = d3.scaleLinear()
.range([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
x.domain([0, Math.max.apply(null, strippedData)]).nice();
var maxY = Object.values(counts).sort((prev, next) => next - prev)[0];
y.domain([0, maxY]).nice();
svg.selectAll(".bin")
.data(summaryData)
.enter().append("line")
.attr("class", "bin")
.attr("x1", function(d) { return x(d.key) })
.attr("x2", function(d) { return x(d.key) })
.attr("y1", height)
.attr("y2", function(d) { return y(d.value ) });
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom()
.scale(x));
svg.append("g")
.attr("class", "y axis")
.call(d3.axisLeft()
.scale(y));
var logNormalLine = d3.line()
.x(function(d, i) { return x(d); })
.y(function(d) { return logNormal(d, 100, 0.4); })
.curve(d3.curveBasis);
svg.append("g")
.attr("d", logNormalLine(d3.range(x.domain()[0], x.domain()[1], 1)))
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none")
}
function logNormal(x, mean, stddev) {
var y = (1 / (x * Math.sqrt(2 * Math.PI * Math.pow(stddev, 2)))) * Math.pow(Math.E, - (Math.pow(Math.log(x) - mean, 2) / (2 * Math.pow(stddev, 2))));
// console.log('logNormal', x, mean, stddev, y);
y = isFinite(y) ? y : 0;
return y;
}