Last active
August 29, 2015 14:08
-
-
Save mattm/b761b8f13332552f1ed9 to your computer and use it in GitHub Desktop.
Normal Distribution as Approximation to Binomial Distribution
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
experiments = 1000 | |
visitors = 250 | |
conversion_rate = 0.3 | |
expected_conversions = visitors * conversion_rate | |
expected_sd = sqrt( visitors * conversion_rate * ( 1 - conversion_rate ) ) | |
sd_for_axis_range = 4.5 | |
axis_divisions = 5 | |
results = vector() | |
for ( experiment in 1:experiments ) { | |
conversions = 0 | |
for ( visitor in 1:visitors ) { | |
if ( runif( 1 ) <= conversion_rate ) { | |
conversions = conversions + 1 | |
} | |
} | |
results = c( results, conversions ) | |
} | |
par( oma = c( 0, 2, 0, 0 ) ) | |
axis_min = floor( ( expected_conversions - sd_for_axis_range * expected_sd ) / axis_divisions ) * axis_divisions | |
axis_max = ceiling( ( expected_conversions + sd_for_axis_range * expected_sd ) / axis_divisions ) * axis_divisions | |
hist( results, axes = FALSE, breaks = seq( axis_min, axis_max, by = 1 ), ylab = 'Probability', xlab = 'Conversions', freq = FALSE, col = '#4B85ED', main = 'Distribution of Results' ) | |
axis( side = 1, at = seq( axis_min, axis_max, by = axis_divisions ), pos = 0, col = "#666666", col.axis = "#666666", lwd = 1, tck = -0.015 ) | |
axis( side = 2, col = "#666666", col.axis = "#666666", lwd = 1, tck = -0.015 ) | |
curve( dnorm(x, mean = conversion_rate * visitors, sd = expected_sd ), add = TRUE, col = "red", lwd = 4 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment