$\DeclareMathOperator{\p}{Pr}$ $\DeclareMathOperator{\P}{Pr}$ $\DeclareMathOperator{\c}{^C}$ $\DeclareMathOperator{\or}{ or}$ $\DeclareMathOperator{\and}{ and}$ $\DeclareMathOperator{\var}{Var}$ $\DeclareMathOperator{\E}{E}$ $\DeclareMathOperator{\std}{Std}$ $\DeclareMathOperator{\Ber}{Bern}$ $\DeclareMathOperator{\Bin}{Bin}$ $\DeclareMathOperator{\Poi}{Poi}$ $\DeclareMathOperator{\Uni}{Uni}$ $\DeclareMathOperator{\Exp}{Exp}$ $\DeclareMathOperator{\N}{N}$ $\DeclareMathOperator{\R}{\mathbb{R}}$ $\newcommand{\d}{\, d}$

Python for Probability

We'll hold two Python review sessions throughout the quarter to get you up to speed on what you'll need for the problem sets.

If you want to get more python practice, you can also check out our resource (which we'll update from time to time). Python tutorial notebook. This is what we'll be using durring the review sessions. Also, make sure to be logged into your Stanford Account!

  • Session #1: Friday June 28th, 5pm Pacific Time. Intro, running programs, Python basics.
    Zoom Recording

This handout only goes over probability functions for Python. We'll cover these concepts throughout the quarter. For a tutorial on the basics of python, there are many good online tutorials. As we continue, we'll update this doc with all the things you need to know for the psets!

  1. Installing Python
  2. Probability Basics

Installing Python

If you haven't already installed Python on your computer, don't worry! This section will guide you through the process step-by-step. There are a few ways to get Python up and running on your machine, and we'll cover the most common methods here:

  1. Install Python from Source and Use Visual Studio Code

    You can install Python directly from the official Python website. After installing Python, you can use Visual Studio Code, a popular and user-friendly code editor, to write your code. Running your Python scripts is easy using the terminal.

  2. Use Google Colab

    If you prefer a quick and easy way to write and run Python code in your browser, you can use Google Colab. This is an online platform that lets you write and run Python code without needing to install anything on your computer.

  3. Use Your Existing Installation of Python 3

    If you already have Python 3.7 or higher installed on your computer, you're all set! You can use your existing setup to write and run Python code.


Option 1: Install Python from Source and Use Visual Studio Code

To install Python on your computer, follow these steps:

  1. Visit the Python downloads page and download the latest version of Python for your operating system (Windows, macOS, or Linux).
  2. Run the installer and follow the on-screen instructions to complete the installation. Make sure to check the option to add Python to your system's PATH during the installation process.
  3. Once Python is installed, download and install Visual Studio Code (VS Code), a powerful and flexible code editor.
  4. Open Visual Studio Code and create a new Python file by clicking on "File" -> "New File" and saving it with a .py extension, for example, hello.py.
  5. Write your Python code in the newly created file. For example, you can start with a simple print statement:
  6. print('Hello, world!')
  7. To run your code, open the terminal in Visual Studio Code by selecting "Terminal" -> "New Terminal". In the terminal, navigate to the directory where your Python file is located using the cd command. Then, run your Python script by typing python yourfile.py (replace yourfile.py with the name of your file) and pressing Enter.
  8. You should see the output of your code in the terminal. Congratulations, you've successfully run your first Python script using Visual Studio Code!

Option 2: Use Google Colab

If you'd like to try a quick and easy way to write and run Python code without installing anything on your computer, Google Colab is a great option. Follow these steps to get started:

  1. Go to Google Colab in your web browser.
  2. If you don't already have a Google account, you'll need to create one to use Google Colab.
  3. Once you're signed in, click on "File" -> "New Notebook" to create a new Colab notebook.
  4. In the new notebook, you can write Python code in the code cells. For example, type the following code in the first cell:
  5. print('Hello, world!')
  6. To run the code, click the "Run" button (the play icon) next to the cell, or press Shift + Enter. You should see the output of your code below the cell.
  7. That's it! You can now use Google Colab to write and run Python code directly in your browser. This is especially useful for quick experiments and sharing code with others.

Python Basics


Counting Operations

Factorial

Compute n! as an integer. This example computes 20!:

import math
print(math.factorial(20))

Choose

As of Python 3.8, you can compute n choose m using the math module. This example computes 10 choose 5:

import math
print(math.comb(10, 5))

Power

Compute n^m using the pow function or the exponentiation operator **. This example computes 2^10:

print(pow(2, 10))  # Using the pow function
print(2 ** 10)     # Using the exponentiation operator

Using SciPy

SciPy is a free and open-source library for scientific computing built on top of NumPy. It provides additional functionality that is very useful for CS109, including the ability to compute probabilities, expectations, variances, and more for various random variables. Below are some examples of how to use SciPy for different distributions:

Binomial

Create a Binomial Random variable X and compute its probability mass function (PMF) or cumulative density function (CDF). This example declares X to be a binomial random variable with parameters n=10 and p=0.2. It calculates a few statistics on X, computes P(X = 3) and P(X ≤ 4), and generates random samples from X:

from scipy import stats
X = stats.binom(10, 0.2)  # Declare X to be a binomial random variable
print(X.pmf(3))           # P(X = 3)
print(X.cdf(4))           # P(X <= 4)
print(X.mean())           # E[X]
print(X.var())            # Var(X)
print(X.std())            # Std[X]
print(X.rvs())            # Get a random sample from X
print(X.rvs(10))          # Get 10 random samples from X

From a terminal, you can use the "help" command to see a full list of methods defined on a variable (or for a package):

from scipy import stats
X = stats.binom(10, 0.2)  # Declare X to be a binomial random variable
help(X)                   # List all methods defined for X

Poisson

Create a Poisson Random variable Y. This example declares Y to be a Poisson random variable with parameter λ=2. It then calculates P(Y = 3):

from scipy import stats
Y = stats.poisson(2)  # Declare Y to be a poisson random variable
print(Y.pmf(3))       # P(Y = 3)
print(Y.rvs())        # Get a random sample from Y

Geometric

Create a Geometric Random variable X, the number of trials until a success. This example declares X to be a geometric random variable with parameter p=0.75:

from scipy import stats
X = stats.geom(0.75)  # Declare X to be a geometric random variable
print(X.pmf(3))       # P(X = 3)
print(X.rvs())        # Get a random sample from X

Normal

Create a Normal Random variable A. This example declares A to be a normal random variable with mean μ=3 and variance σ^2=16. It calculates the probability density function (PDF) and cumulative distribution function (CDF) at specific points. Note that in the SciPy library, the second parameter is the standard deviation (σ), not the variance:

import math
from scipy import stats
A = stats.norm(3, math.sqrt(16)) # Declare A to be a normal random variable
print(A.pdf(4))                  # f(4), the probability density at 4
print(A.cdf(2))                  # F(2), which is also P(A < 2)
print(A.rvs())                   # Get a random sample from A

Exponential

Create an Exponential Random variable B. This example declares B to be an exponential random variable with rate parameter λ=4. In SciPy, the scale parameter is 1/λ:

from scipy import stats
B = stats.expon(scale=1/4)  # Declare B to be an exponential random variable
print(B.pdf(1))             # f(1), the probability density at 1
print(B.cdf(2))             # F(2), which is also P(B < 2)
print(B.rvs())              # Get a random sample from B

Beta

Create a Beta Random variable X. This example declares X to be a beta random variable with parameters α=1 and β=3:

from scipy import stats
X = stats.beta(1, 3)  # Declare X to be a beta random variable
print(X.pdf(0.5))     # f(0.5), the probability density at 0.5
print(X.cdf(0.7))     # F(0.7), which is also P(X < 0.7)
print(X.rvs())        # Get a random sample from X

Additional Helpful Libraries and Modules

Beyond SciPy, there are several other libraries and modules that will be helpful for completing CS109 in Python:

  • NumPy: Fundamental package for numerical computing with Python. Useful for array operations and linear algebra. Install it using pip install numpy.
  • pandas: Library for data manipulation and analysis. It provides data structures and functions needed to manipulate structured data seamlessly. Install it using pip install pandas.
  • matplotlib: Plotting library for creating static, animated, and interactive visualizations in Python. Install it using pip install matplotlib.
  • seaborn: Statistical data visualization library based on matplotlib. It provides a high-level interface for drawing attractive statistical graphics. Install it using pip install seaborn.