simple_statistics.js | |
---|---|
(function() {
var ss = {};
exports = module.exports = ss; | |
Linear RegressionSimple linear regression is a simple way to find a fitted line between a set of coordinates. | ss.linear_regression = function() {
var linreg = {},
data = []; |
Assign the data to the model. | linreg.data = function(x) {
if (!arguments.length) return data;
data = x.slice();
return linreg;
}; |
Fitting The Regression LineThis is called after | linreg.line = function() { |
if there's only one point, arbitrarily choose a slope of 0 and a y-intercept of whatever the y of the initial point is | if (data.length == 1) {
m = 0;
b = data[0][1];
} else { |
Initialize our sums and scope the | var sum_x = 0, sum_y = 0,
sum_xx = 0, sum_xy = 0,
m, b; |
Gather the sum of all x values, the sum of all y values, and the sum of x^2 and (x*y) for each value. In math notation, these would be SSx, SSy, SSxx, and SSxy | for (var i = 0; i < data.length; i++) {
sum_x += data[i][0];
sum_y += data[i][1];
sum_xx += data[i][0] * data[i][0];
sum_xy += data[i][0] * data[i][1];
} |
| m = ((data.length * sum_xy) - (sum_x * sum_y)) /
((data.length * sum_xx) - (sum_x * sum_x)); |
| b = (sum_y / data.length) - ((m * sum_x) / data.length);
} |
Return a function that computes a | return function(x) {
return b + (m * x);
};
};
return linreg;
}; |
R SquaredThe r-squared value of data compared with a function | ss.r_squared = function(data, f) {
if (data.length < 2) return 1; |
Compute the average y value for the actual data set in order to compute the total sum of squares | var sum = 0, average;
for (var i = 0; i < data.length; i++) {
sum += data[i][1];
}
average = sum / data.length; |
Compute the total sum of squares - the squared difference between each point and the average of all points. | var sum_of_squares = 0;
for (var j = 0; j < data.length; j++) {
sum_of_squares += Math.pow(average - data[j][1], 2);
} |
Finally estimate the error: the squared difference between the estimate and the actual data value at each point. | var err = 0;
for (var k = 0; k < data.length; k++) {
err += Math.pow(data[k][1] - f(data[k][0]), 2);
} |
As the error grows larger, it's ratio to the sum of squares increases and the r squared value grows lower. | return 1 - (err / sum_of_squares);
}; |
Bayesian ClassifierThis is a naïve bayesian classifier that takes singly-nested objects. | ss.bayesian = function() { |
Create the bayes_model object, that will expose methods | var bayes_model = {}, |
The number of items that are currently classified in the model | total_count = 0, |
Every item classified in the model | data = {}; |
TrainTrain the classifier with a new item, which has a single dimension of Javascript literal keys and values. | bayes_model.train = function(item, category) { |
If the data object doesn't have any values for this category, create a new object for it. | if (!data[category]) data[category] = {}; |
Iterate through each key in the item. | for (var k in item) {
var v = item[k]; |
Initialize the nested object | if (data[category][k] === undefined) data[category][k] = {};
if (data[category][k][v] === undefined) data[category][k][v] = 0; |
And increment the key for this key/value combination. | data[category][k][item[k]]++;
} |
Increment the number of items classified | total_count++;
}; |
ScoreGenerate a score of how well this item matches all possible categories based on its attributes | bayes_model.score = function(item) { |
Initialize an empty array of odds per category. | var odds = {}; |
Iterate through each key in the item,
then iterate through each category that has been used
in previous calls to | for (var k in item) {
var v = item[k];
for (var category in data) { |
Create an empty object for storing key - value combinations for this category. | if (odds[category] === undefined) odds[category] = {}; |
If this item doesn't even have a property, it counts for nothing, but if it does have the property that we're looking for from the item to categorize, it counts based on how popular it is versus the whole population. | if (data[category][k]) {
odds[category][k + '_' + v] = data[category][k][v] / total_count;
} else {
odds[category][k + '_' + v] = 0;
}
}
} |
Set up a new object that will contain sums of these odds by category | var odds_sums = {};
for (var category in odds) {
for (var combination in odds[category]) {
if (odds_sums[category] === undefined) odds_sums[category] = 0;
odds_sums[category] += odds[category][combination];
}
}
return odds_sums;
}; |
Return the completed model. | return bayes_model;
}; |
sumis simply the result of adding all numbers together, starting from zero. | ss.sum = function(x) {
var sum = 0;
for (var i = 0; i < x.length; i++) {
sum += x[i];
}
return sum;
}; |
meanis the sum over the number of values | ss.mean = function(x) {
return ss.sum(x) / x.length;
} |
varianceis the sum of squared deviations from the mean | ss.variance = function(x) {
var mean = ss.mean(x),
deviations = []; |
Make a list of squared deviations from the mean. | for (var i = 0; i < x.length; i++) {
deviations.push(Math.pow(x[i] - mean, 2));
} |
Find the mean value of that list | return ss.mean(deviations);
}; |
standard deviationis just the square root of the variance. | ss.standard_deviation = function(x) {
return Math.sqrt(ss.variance(x));
}
})(this);
|