Univariate cost function
Machine learning course on coursera
JavaScript
// univariate cost function
function cost(x, y, theta_0, theta_1) {
var m = x.length;
var prediction = 0;
x.forEach(function(xi, i) {
prediction += Math.pow((theta_0 + xi * theta_1) - y[i], 2)
});
return prediction / (2 * m)
}
console.log(cost([1, 2], [2, 4], 0, 2))