Reputation: 2223
Working in Python 2.7.
I'm trying to plot a histogram for the numbers generated by 50 run-throughs of my random walk. But when I use pylab.hist(batting_average, bins = 10), I get a weird multi-colored histogram that goes up close to 500, but with only 50 runs of the walk, the maximum it should be able to go on the y-axis would be 50.
Here's my code:
a = ['Hit', 'Out']
b = [.3, .7]
def battingAverage(atBats, some_list=a, probabilities=b):
num_hits = 0
num_outs = 0
current_BA = []
for i in range(1,atBats):
if random_pick(a, b) == 'Hit':
num_hits += 1
else:
num_outs +=1
BA = float(num_hits)/(float(num_hits)+float(num_outs))
current_BA.append(BA)
return current_BA
def printBAs():
for i in range(50):
batting_average = battingAverage(501)
pylab.hist(batting_average, bins=10)
What's wrong with my histogram!?
Let me know if anything needs clarification, and I'll do my best.
Upvotes: 0
Views: 7642
Reputation: 12339
The argument passed to battingAverage
is 501... and is the number of at-bats. You're doing 50 histograms with 500 at-bats per histogram.
(Oh, and you need to fix the formatting of your code... the indentation is messed up.)
Your code doesn't do what you think it does.
I think you're wanting battingAverage
to return the final batting average, but it returns a list of batting averages, one for each at-bat.
Then you're plotting that list.
I think you want to return a single number from battingAverage, and you want to accumulate the list in the printBAs() function, and move pylab.hist out of the for loop.
I don't suppose this is homework?
In other words, I think you want something like this:
a = ['Hit', 'Out']
b = [.3, .7]
def battingAverage(atBats, results=a, probabilities=b):
num_hits = 0
num_outs = 0
for i in range(atBats):
if random_pick(results, probabilities) == 'Hit':
num_hits += 1
else:
num_outs +=1
BA = float(num_hits)/(float(num_hits)+float(num_outs))
return BA
def printBAs():
batting_averages = [battingAverage(500) for i in range(50)]
pylab.hist(batting_averages, bins=10)
Though that code still needs cleanup...
Upvotes: 1