Skip to content

Instantly share code, notes, and snippets.

@meetbryce
Last active June 18, 2018 06:44
Show Gist options
  • Save meetbryce/dbcbf2707029d70dbcf5dbcc2ec57dc7 to your computer and use it in GitHub Desktop.
Save meetbryce/dbcbf2707029d70dbcf5dbcc2ec57dc7 to your computer and use it in GitHub Desktop.
Calculate binary statistical significance (with 99% confidence)
// require('jStat');
function calculateSignificance(a_sessions, a_conversions, b_sessions, b_conversions) {
a_cr = a_conversions / a_sessions;
b_cr = b_conversions / b_sessions;
a_stdErr = Math.sqrt(a_cr * (1 - a_cr) / a_sessions);
b_stdErr = Math.sqrt(b_cr * (1 - b_cr) / b_sessions);
// get_z_score()
const z_score =
(a_cr - b_cr) /
Math.sqrt(Math.pow(a_stdErr, 2) + Math.pow(b_stdErr, 2));
const p_value = NORMDIST(z_score, 0, 1, true);
console.table({
a_cr: _.round(a_cr, 2),
b_cr: _.round(b_cr, 2),
a_stdErr: _.round(a_stdErr, 2),
b_stdErr: _.round(b_stdErr, 2),
z_score: _.round(z_score, 3),
p_value: _.round(p_value, 4),
});
return p_value > 0.99;
function NORMDIST(x, mean, sd, cumulative) {
// Check parameters
if (isNaN(x) || isNaN(mean) || isNaN(sd)) return "#VALUE!";
if (sd <= 0) return "#NUM!";
// Return normal distribution computed by jStat [http://jstat.org]
return cumulative
? jStat.normal.cdf(x, mean, sd)
: jStat.normal.pdf(x, mean, sd);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment