samedi 11 février 2017

How to calculate mean, mode, variance, standard deviation etc. of output in python?

I have a simple game which is based on probabilities, every day we toss a coin and if we get heads then we win and we get $20 and if we toss the coin and we get tails then we lose $19, at the end of the month (28 days) we see how much we have lost or made.

def coin_tossing_game():
    random_numbers = [random.randint(0, 1) for x in range(500)] #generate 500 random numbers
    for x in random_numbers:
        if x == 0: #if we get heads
            return 20 #we win $20
        elif x == 1: #if we get tails
            return -19 #we lose $19


for a in range(1, 28): #for each day of the month
    print(coin_tossing_game())

This returns the output 20 20 -19 -19 -19 -19 -19 20 -19 20 -19 20 -19 20 20 -19 -19 20 20 -19 -19 -19 20 20 20 -19 -19 -19 20 20

This output is exactly what I expected. I want to find the sum of the output and other descriptive statistics like the mean, mode, median, standard deviation, confidence intervals etc. I have had to copy and paste this data to excel to do this data analysis. I was hoping there was a way to easily do this in python quickly.




Aucun commentaire:

Enregistrer un commentaire