Last active
August 29, 2015 13:56
-
-
Save sambatyon/9251408 to your computer and use it in GitHub Desktop.
Translates a list of numbers assumed to be distributed normally to another normal distribution with the required mean and standard deviation.
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
function redistribute(numbers, new_mean, new_std_deviation) { | |
// compute the mean of the list of numbers | |
// μ = (Σ x_i) / n | |
var sum = 0; | |
numbers.forEach(function (i) {sum += i}); | |
var mean = sum / numbers.length; | |
// compute the std deviation of the numbers: | |
// σ = √(Σ (x_i - μ)² / n) | |
sum = 0; | |
numbers.forEach(function (i) {sum += Math.pow(i - mean, 2)}); | |
var std_dev = Math.sqrt(sum / numbers.length); | |
var res = []; | |
numbers.forEach(function (i) { | |
// Move number to a standar normal distribution (µ = 0, σ = 1) | |
// x' = (x - µ) / σ | |
var normal = (i - mean) / std_dev; | |
// now move it to its new position | |
// x'' = (x * σ') + µ' | |
res.push((normal * new_std_deviation) + new_mean); | |
}); | |
return res; | |
} | |
var grades = [2.5, 3.1, 1.7, 2.2, 3.0, 2.6, 2.7, 1.9, 2.1]; | |
// The new grades will have an average of 3.5 | |
// and the 68 % of the grades will be between 3.0 and 4.0 | |
var new_grades = redistribute(grades, 3.5, 0.5); | |
// new_grades = [3.59, 4.25, 2.70, 3.25, 4.14, 3.69, 3.80, 2.92, 3.14] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment