Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
142 lines (111 sloc) 4.5 KB
import numpy
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.mlab as mlab
from scipy.stats import kurtosis
def main():
ntrials = 1000000
questions = 100
choices = 4
scores_guessing = numpy.zeros(ntrials)
scores_constant = numpy.zeros(ntrials)
for x in range(ntrials):
# generate the test
answer_key = numpy.random.randint(low = 1, high = 1+choices, size = questions)
# come up with our answers
responses_guessing = numpy.random.randint(low = 1, high = 1+choices, size = questions)
# "grade" the tests
scores_constant[x] = (100.0*sum(answer_key == 1))/questions
scores_guessing[x] = (100.0*sum(answer_key == responses_guessing))/questions
print "guessing", numpy.mean(scores_guessing), numpy.std(scores_guessing)
print "constant", numpy.mean(scores_constant), numpy.std(scores_constant)
'''
plt.hist(scores_constant, bins = range(0,100), facecolor='red', alpha=.95)
plt.hist(scores_guessing, bins = range(0,100), facecolor='blue', alpha=0.65)
red_patch = mpatches.Patch(color='red', label='Always C')
blue_patch = mpatches.Patch(color='blue', label='Guess Randomly')
'''
x = numpy.linspace(100.0/choices-20,100.0/choices+20,1000)
# Constant
plt.plot(x,mlab.normpdf(x,numpy.mean(scores_constant),numpy.std(scores_constant)), color = 'blue')
red_patch = mpatches.Patch(color='blue', label='Always C')
# Random Guessing
plt.plot(x,mlab.normpdf(x,numpy.mean(scores_guessing),numpy.std(scores_guessing)), color= 'red')
blue_patch = mpatches.Patch(color='red', label='Random')
plt.legend(handles=[red_patch, blue_patch])
plt.xlabel('Score')
plt.ylabel('Frequency')
plt.grid(True)
plt.show()
def teacher_make_key(questions, choices, maxrepeats = 3):
answer_key = numpy.zeros(questions)
dup_count = 0
prev_ans = -1
for i in range(questions):
answer_key[i] = numpy.random.randint(low = 1, high = 1+choices)
if answer_key[i] == prev_ans:
dup_count += 1
else:
dup_count = 0
if dup_count >= maxrepeats:
while answer_key[i] == prev_ans:
answer_key[i] = numpy.random.randint(low = 1, high = 1+choices)
prev_ans = answer_key[i]
return answer_key
def pseudorandom():
ntrials = 100000
questions = 100
choices = 2
maxrep = 2
scores_guessing1 = numpy.zeros(ntrials)
scores_guessing2 = numpy.zeros(ntrials)
scores_constant = numpy.zeros(ntrials)
for x in range(ntrials):
# generate the test
answer_key = teacher_make_key(questions, choices, maxrep)
# come up with our answers
responses_guessing1 = numpy.random.randint(low = 1, high = 1+choices, size = questions)
responses_guessing2 = teacher_make_key(questions, choices, maxrep)
# "grade" the tests
scores_constant[x] = (100.0*sum(answer_key == 1))/questions
scores_guessing1[x] = (100.0*sum(answer_key == responses_guessing1))/questions
scores_guessing2[x] = (100.0*sum(answer_key == responses_guessing2))/questions
#for x in zip(answer_key,responses_guessing1, responses_guessing2):
# print x
print "guessing random", numpy.mean(scores_guessing1), numpy.var(scores_guessing1), kurtosis(scores_guessing1)
print "guessing pseudo", numpy.mean(scores_guessing2), numpy.var(scores_guessing2), kurtosis(scores_guessing2)
print "constant", numpy.mean(scores_constant), numpy.var(scores_constant), kurtosis(scores_constant)
'''
plt.hist(scores_constant, bins = range(0,100), facecolor='red', alpha=.95)
plt.hist(scores_guessing, bins = range(0,100), facecolor='blue', alpha=0.65)
red_patch = mpatches.Patch(color='red', label='Always C')
blue_patch = mpatches.Patch(color='blue', label='Guess Randomly')
plt.legend(handles=[red_patch, blue_patch])
plt.xlabel('Score')
plt.ylabel('Frequency')
plt.grid(True)
'''
x = numpy.linspace(100.0/choices-20,100.0/choices+20,1000)
# Constant
plt.plot(x,mlab.normpdf(x,numpy.mean(scores_constant),numpy.std(scores_constant)), color = 'blue')
red_patch = mpatches.Patch(color='blue', label='Always C')
# Random Guessing
plt.plot(x,mlab.normpdf(x,numpy.mean(scores_guessing1),numpy.std(scores_guessing1)), color= 'red')
blue_patch = mpatches.Patch(color='red', label='Random')
# Pseudorandom Guessing
plt.plot(x,mlab.normpdf(x,numpy.mean(scores_guessing2),numpy.std(scores_guessing2)), color="green")
green_patch = mpatches.Patch(color='green', label='Pseudorandom')
'''
mean = 0
variance = 1
sigma = math.sqrt(variance)
x = np.linspace(-3,3,100)
plt.plot(x,mlab.normpdf(x,mean,sigma))
'''
plt.legend(handles=[red_patch, blue_patch, green_patch])
plt.xlabel('Score')
plt.ylabel('Frequency')
plt.grid(True)
plt.show()
pseudorandom()
#main()