Last active
August 29, 2015 14:07
-
-
Save homam/ec982bc6be4c7b72b2f9 to your computer and use it in GitHub Desktop.
Statistics, Probability, Confidence Interval
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
{sqrt} = require \prelude-ls | |
{ | |
find-propability-of-population-mean-in-a-range-for-continuous-variable | |
find-confidence-interval-of-population-mean-for-continuous-variable | |
find-propability-of-population-mean-in-a-range-for-binomial-variable | |
find-confidence-interval-of-population-mean-for-binomial-variable | |
} = require \./stats | |
round = (precision, n) --> | |
tens = 10**precision | |
(Math.round <| n * tens) / tens | |
prob = find-propability-of-population-mean-in-a-range-for-continuous-variable do | |
36 # sample size | |
112 # sample mu | |
40 # sample sigma | |
[100, 124] # lower and upper | |
console.log "Prob = #prob \n#{0.93 == round 2 prob}\n" | |
[lower, upper] = find-confidence-interval-of-population-mean-for-continuous-variable do | |
36 # sample size | |
112 # sampel mu | |
40 # sample sigma | |
0.9281394664049833 # confidence | |
console.log "CI = #{[lower, upper]} \n#{100 == round 2 lower and 124 == round 2 upper}\n" | |
[lower, upper] = find-confidence-interval-of-population-mean-for-binomial-variable do | |
250 # sample size | |
142 # successes | |
0.99 # confidence | |
console.log "CI = #{[lower, upper]} \n#{0.49 == round 2 lower and 0.65 == round 2 upper}\n" | |
prob = find-propability-of-population-mean-in-a-range-for-binomial-variable do | |
250 # sample size | |
142 # successes | |
[0.4872965881321945, 0.6487034118678054] # bounds | |
console.log "Prob = #prob \n#{0.99 == round 2 prob}\n" | |
[lower, upper] = find-confidence-interval-of-population-mean-for-continuous-variable do | |
60 # sample size | |
7.177 # sample mu | |
sqrt 8.691 # sample sigma | |
0.95 # confidence | |
console.log "CI = #{[lower, upper]} \n#{6.43 == round 2 lower and 7.92 == round 2 upper}\n" | |
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
{mean, fix, map, filter, sum, sqrt, abs, exp} = require \prelude-ls | |
statistics = (arr, sample = false) -> | |
length = arr.length | |
average = mean arr | |
sigma = arr |> map (-> (it - average) * (it - average)) |> sum |> (/ (length - if sample then 1 else 0)) |> sqrt | |
[length, average, sigma] | |
random = (lower, upper) --> | |
Math.round <| Math.random! * (upper - lower) + lower | |
make-sample = (how-many, arr) --> | |
size = arr.length - 1 | |
how-many |> | |
fix (next) -> (how-many) -> | |
return [] if how-many == 0 | |
[arr[random 0, size]] ++ next how-many - 1 | |
normal-cdf = (avg, sigma, x) --> | |
z = (x - avg) / sqrt 2 * sigma * sigma | |
t = 1 / (1 + 0.3275911 * abs z) | |
a1 = 0.254829592 | |
a2 = -0.284496736 | |
a3 = 1.421413741 | |
a4 = -1.453152027 | |
a5 = 1.061405429 | |
erf = 1 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * (exp -z * z) | |
sign = if z < 0 then -1 else 1 | |
(1 / 2) * (1 + sign * erf) | |
standard-normal-cdf = normal-cdf 0, 1 | |
derivitive = (delta, f, x) --> | |
((f <| x + delta) - (f x)) / delta | |
newton = (precision, f, x0) --> | |
df = derivitive precision/10, f | |
x0 |> | |
fix (next) -> (x0) -> | |
dfx = df x0 | |
return next Math.random! if (abs dfx) < precision | |
x = x0 - ( (f x0) / dfx ) | |
return x if (abs <| x - x0) <= precision | |
next x | |
# generic functions | |
find-propability-of-population-mean-in-a-range = (sample-mu, samples-means-sigma, [lower, upper]) --> | |
standard-upper = (upper - sample-mu) / samples-means-sigma | |
standard-lower = (lower - sample-mu) / samples-means-sigma | |
(standard-normal-cdf standard-upper) - (standard-normal-cdf standard-lower) | |
find-confidence-interval-of-population-mean = (sample-mu, samples-means-sigma, confidence) --> | |
calculate-confidence = find-propability-of-population-mean-in-a-range sample-mu, samples-means-sigma | |
delta = samples-means-sigma/1000 | |
f = (d) -> | |
interval = [sample-mu - d, sample-mu + d] | |
(calculate-confidence interval) - confidence | |
d = newton 0.0001, f, 0 | |
[sample-mu - d, sample-mu + d] | |
# continuous variable | |
find-propability-of-population-mean-in-a-range-for-continuous-variable = (sample-size, sample-mu, sample-sigma, [lower, upper]) --> | |
find-propability-of-population-mean-in-a-range do | |
sample-mu | |
(sample-sigma / sqrt sample-size) | |
[lower, upper] | |
find-confidence-interval-of-population-mean-for-continuous-variable = (sample-size, sample-mu, sample-sigma, confidence) --> | |
find-confidence-interval-of-population-mean do | |
sample-mu | |
(sample-sigma / sqrt sample-size) | |
confidence | |
# binomial variable | |
find-propability-of-population-mean-in-a-range-for-binomial-variable = (sample-size, successes, [lower, upper]) --> | |
find-propability-of-population-mean-in-a-range do | |
successes / sample-size | |
sqrt(sample-size * (successes/sample-size) * (1 - (successes/sample-size))) /sample-size | |
[lower, upper] | |
find-confidence-interval-of-population-mean-for-binomial-variable = (sample-size, successes, confidence) --> | |
find-confidence-interval-of-population-mean do | |
successes / sample-size | |
sqrt(sample-size * (successes/sample-size) * (1 - (successes/sample-size))) /sample-size | |
confidence | |
exports = exports or this | |
exports <<< { | |
statistics | |
random | |
make-sample | |
normal-cdf | |
standard-normal-cdf | |
derivitive | |
newton | |
find-propability-of-population-mean-in-a-range-for-continuous-variable | |
probability-of-continuous-mean: find-propability-of-population-mean-in-a-range-for-continuous-variable | |
find-confidence-interval-of-population-mean-for-continuous-variable | |
ci-of-continuous-mean: find-confidence-interval-of-population-mean-for-continuous-variable | |
find-propability-of-population-mean-in-a-range-for-binomial-variable | |
probability-of-binomial-mean: find-propability-of-population-mean-in-a-range-for-binomial-variable | |
find-confidence-interval-of-population-mean-for-binomial-variable | |
ci-of-continuous-mean: find-confidence-interval-of-population-mean-for-binomial-variable | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment