Python, MATLAB, Julia, R code: Chapter 1
Intro to Probability for Data Science
Chapters
Lecture Videos + Slides
Selected Exercise Videos
Code and Data
Problem Solutions (Instructor Only)
Purdue Courses
Acknowledgement: The Julia code is written by the contributors listed here.
Acknowledgement: The R code is written by contributors listed here.
Chapter 1.1
Visualizing a geometric series
p = 1/2; n = 1:10; X = p.^n; bar(n,X,'FaceColor',[0.8, 0.2,0.2]);
import numpy as np import matplotlib.pyplot as plt p = 1/2 n = np.arange(1,10) X = np.power(p,n) plt.bar(n,X)
using StatsPlots:bar p = 0.5 n = 1:10 X = p .^ n bar(n, X, legend=false)
p <- 1/2 n <- seq(1, 10) X <- p^n barplot(X, names.arg=1:10)
Compute N choose K
n = 10; k = 2; nchoosek(n,k) factorial(k)
from scipy.special import comb, factorial n = 10 k = 2 comb(n, k) factorial(k)
n = 10 k = 2 binomial(n, k) factorial(k)
n <- 10 k <- 2 choose(n,k) factorial(k)
Chapter 1.4
Inner product of two vectors
x = [1 0 -1]'; y = [3 2 0]'; z = x'*y;
import numpy as np x = np.array([1,0,-1]) y = np.array([3,2,0]) z = np.dot(x,y) print(z)
x = [1, 0, -1] y = [3, 2, 0] x'y
x <- c(1,0,-1) y <- c(3,2,0) z <- t(x) %*% y print(z)
Norm of a vector
x = [1 0 -1]; x_norm = norm(x);
import numpy as np x = np.array([1,0,-1]) x_norm = np.linalg.norm(x)
using LinearAlgebra:norm x = [1, 0, -1] norm(x)
x <- c(1,0,-1) x_norm <- norm(x, type = "2") print(x_norm)
Weighted norm of a vector
W = [1 2 3; 4 5 6; 7 8 9]; x = [2; -1; 1]; z = x'*W*x
import numpy as np W = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) x = np.array([[2],[-1],[1]]) z = np.dot(x.T, np.dot(W,x)) print(z)
W = [1. 2. 3.; 4. 5. 6.; 7. 8. 9.] x = [2, -1, 1] z = x'W*x
W <- matrix(1:9, ncol = 3, nrow = 3) x <- matrix(c(2,-1,1)) z <- t(x) %*% (W %*% x) print(z)
Matrix inverse
X = [1 3; -2 7; 0 1] XtXinv = inv(X'X)
import numpy as np X = np.array([[1, 3], [-2, 7], [0, 1]]) XtX = np.dot(X.T, X) XtXinv = np.linalg.inv(XtX) print(XtXinv)
X = [1 3; -2 7; 0 1] XtXinv = inv(X'X)
X <- matrix(c(1,-2,0,3,7,1), nrow = 3, ncol = 2) XtX <- t(X) %*% X Xtxinv <- solve(XtX) print(Xtxinv)
System of linear equation
X = [1 3; -2 7; 0 1]; y = [2; 1; 0]; beta = X\y;
import numpy as np X = np.array([[1, 3], [-2, 7], [0, 1]]) y = np.array([[2],[1],[0]]) beta = np.linalg.lstsq(X, y, rcond=None)[0] print(beta)
X = [1. 3.; -2. 7.; 0. 1.] y = [2., 1., 0.] beta = X\y
X <- matrix(c(1,-2,0,3,7,1), nrow = 3, ncol = 2) y <- matrix(c(2,1,0)) beta <- solve(qr(X), y) print(beta)
