Title: Probability Mass Functions
Slug: probability_mass_functions
Summary: Probability Mass Functions in Python.
Date: 2016-02-08 12:00
Category: Statistics
Tags: Basics
Authors: Chris Albon
In [1]:
# Load libraries
import matplotlib.pyplot as plt
In [2]:
# Create some random integer data
data = [3,2,3,4,2,3,5,2,2,3,3,5,2,2,5,6,2,2,2,3,6,6,2,4,3,2,3]
In [3]:
# Create a dictionary to store the counts
count = {}
# For each value in the data
for observation in data:
# An a key, value pair, with the observation being the key
# and the value being +1
count[observation] = count.get(observation, 0) + 1
In [4]:
# Calculate the number of observations
n = len(data)
# Create a dictionary
probability_mass_function = {}
# For each unique value,
for unique_value, count in count.items():
# Normalize the count by dividing by the length of data, add to the PMC dictionary
probability_mass_function[unique_value] = count / n
In [5]:
# Plot the probability mass function
plt.bar(list(probability_mass_function.keys()), probability_mass_function.values(), color='g')
plt.show()