Skip to content

Instantly share code, notes, and snippets.

@msharp
Created April 23, 2013 08:33
Show Gist options
  • Save msharp/5441805 to your computer and use it in GitHub Desktop.
Save msharp/5441805 to your computer and use it in GitHub Desktop.
command-line app to calculate probabilities from a normal distribution. uses scipy.stats for calculations
#!/usr/bin/python
import sys
import os
import math
import scipy.stats
normal_distribution_calculations =[
(1, "Probability that score is less than x (area below)", ["Enter score: "], lambda dist,score: dist.cdf(score)),
(2, "Maximum score to satisfy Pr(x) (area below)", ["Enter probability: "], lambda dist,prob: dist.ppf(prob)),
(3, "Probability that score is more than x (area above)", ["Enter score: "], lambda dist,score: dist.sf(score)),
(4, "Minimum score to satisfy Pr(x) (area above)", ["Enter probability: "], lambda dist,prob: dist.sf(score)),
(5, "Probability score lies within range (area between)", ["Enter low score: ", "Enter high score :"], lambda dist,scores: dist.cdf(scores[1]) - dist.cdf(scores[0])),
(6, "Confidence interval of Pr(x)", ["not available"], lambda dist,prob: "not implemented")
]
def prob_types():
ret = [" {0}. {1}".format(c[0],c[1]) for c in normal_distribution_calculations]
return "\n".join(ret)
def get_prob(prob_type):
for c in normal_distribution_calculations:
if c[0] == prob_type: return c
def get_args(prob):
args = [float(raw_input(a)) for a in prob[2]]
return args[0] if len(args) == 1 else args
# #####
# start program
# #####
print "Define a normal distribution"
dist_mean = float(raw_input("mean = "))
dist_stdv = float(raw_input("standard deviation = "))
# set up our normal distribution
dist = scipy.stats.norm(loc=dist_mean,scale=dist_stdv)
calculate = True
while calculate:
result = 'invalid selection'
prob_type = int(raw_input("\nChoose probability calculation type: \n" + prob_types() + "\nEnter choice: "))
prob = get_prob(prob_type)
args = get_args(prob)
result = prob[3](dist,args)
print result
calculate = raw_input("\nMake another calculation? Y/n ").upper() == "Y"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment