# -*- coding: utf-8 -*- """ Created on Thu Mar 8 12:12:10 2018 @author: megan """ # alternative way to import the random library: #from random import * # imports library random, and don't have to use the name when calling commands from it import random as r import pandas as pd import matplotlib.pyplot as plt # generates a randomly number for the uniform probability distribution on the interval [0,10] # That is, generates a random real number between 0 and 10, with all numbers being equally likely num = r.uniform(0,10) print("Random number is",num) # Comparision between how to generate a real random number between 0 and 10, # and how to generate a random integer between 0 and 10 num = r.uniform(0,10) num_int = r.randint(0,10) print("Random real number is",num) print("Random integer is",num_int) # To explore the uniform distribution, we will sample (generate) 100,000 numbers from it # and plot them as a histogram. # make an empty list to hold the numbers num_list = [] # start your loop for i in range(100000): # in the loop: generate next number and add to list num = r.uniform(0,10) num_list.append(num) # convert list into series num_series = pd.Series(num_list) # plot series as histogram num_series.plot.hist(bins = 20) plt.show() # Estimates a probability density function from the data num_series.plot.density() # estimate probability a sampled number is between 2 and 4 # ~= # of numbers between 2 and 4/total # of numbers # Create our condition variables above2 = num_series >=2 below4 = num_series <= 4 # Apply the condition variables btw2and4 = num_series[above2 & below4] # compute the length of the series containing only numbers between 2 and 4 num_btw_2_and_4 = len(btw2and4) # compute the lenght of the original series total_num = len(num_series) # estimate the probability the number is between 2 and 4 by dividing the number # of numbers in our sample between 2 and 4 with the total number of numbers in our sample prob = num_btw_2_and_4/total_num print("Estimated prob. between 2 and 4:",prob)