Chris Mendez in Statistics, For Developers, ES6

Calculating Probabilities using ES6

Suppose you wanted to calculate the percentage of men that weigh inbetween 140 and 170 lbs. This is possible if you carry a few data points such as the average weight of men (mean), the standard deviation, and the specific weight you want to measure (data point).

Step 0 - Install Normal Distribution Package

The normal distribution package contains a "Standard Normal Distribution" table that will help us with our calculations.

npm i normal-distribution --save

Step 1 - Let's Start Solving

'use strict';
const util = require('util');
const NormalDistribution = require("normal-distribution");

// This class will make it easy for us to get the values that are left of the z-score.
class StandardNormalDistTable {
    // Provide the Z Table Object
    static get zTable(){
        return NormalDistribution.default.zTable;
    }
    // Based on the z-score, provide the chart value
    static chartValue(zScore){
        let absZScore = Math.abs(zScore);
        console.log(absZScore)
        let zRow = Math.floor(absZScore * 10) / 10;
        //let zCol = ._round((Math.round(absZScore * 100) % 10) / 100, 2);
        let zColIndex = (Math.round(absZScore * 100) % 10);
        let chartValue = this.zTable[zRow][zColIndex];
        return chartValue;
    }
}

// Problem #1
// Which percentage of men weigh more than 211 lbs?
// // // // // // // // // // // // // // // // // //
function overWeight(mean, standardDeviation, dataPoint){
    // A. First create a normal distribution object.
    var normDist = new NormalDistribution.default(mean, standardDeviation)
    // B. The zscore will tell you how far you are from the mean.
    var zScore = normDist.zScore(dataPoint);
    // C. Use the zscore to get the value from the distribution table.
    var chartValue = StandardNormalDistTable.chartValue(zScore);
    // D. Subscract from 1
    let value = Number( (1 - chartValue).toFixed(3) );
    // E. We're done.
    let solution = util.format('Around %s of men weight more than %s pounds.', value, dataPoint);
    console.log( solution);
}
overWeight(150, 25, 211)


// Problem #2
// What is the probability that a man weighs between 170 and 140 lbs
// // // // // // // // // // // // // // // // // //
function inBetween(mean, standardDeviation, lowDataPoint, highDataPoint){
    // A. Find the Z Score for the low data point.
    var zScore = (lowDataPoint - mean) / standardDeviation;
    var chartValue = StandardNormalDistTable.chartValue( zScore );
    var valueLow = Number( (1 - chartValue).toFixed(3) );

    // B. Find the Z Score for the high data point.
    var zScore = (highDataPoint - mean) / standardDeviation;
    var chartValue = StandardNormalDistTable.chartValue( zScore );
    var valueHigh = Number( chartValue.toFixed(3) );

    // C. Subtract the two values to get the center & make it a percentage
    var value = Number( (valueHigh - valueLow ).toFixed(3) * 100 );
    let solution = util.format('About %s% of the men are between %s and %s pounds.', value, lowDataPoint, highDataPoint);
    console.log(solution)
}
inBetween(150, 25, 140, 170)

Resources