# Get set up, but we'll start on the board today
import polars as pl
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt# dataset on penguins
# copy from lecture notes: ci example
url = "https://roualdes.sfo3.digitaloceanspaces.com/data/penguins.csv"
df = pl.read_csv(url, null_values = ["NA"]).select("body_mass_g").drop_nulls()x = df["body_mass_g"].to_numpy()
np.mean(x), np.std(x)(np.float64(4201.754385964912), np.float64(800.781229238452))
def ll_normal(theta, data):
mu = theta[0]
sigma = theta[1]
x = data
N = np.size(x)
d = x - mu
return N * np.log(sigma) + np.sum(d * d) / (2 * sigma * sigma)init = sp.stats.expon().rvs(size = 2)
sp.optimize.minimize(ll_normal, init, args = (x,),
method = "L-BFGS-B", bounds = [(-np.inf, np.inf), (1e-5, np.inf)]) message: CONVERGENCE: NORM OF PROJECTED GRADIENT <= PGTOL
success: True
status: 0
fun: 2457.471023626704
x: [ 4.202e+03 8.008e+02]
nit: 49
jac: [ 0.000e+00 0.000e+00]
nfev: 171
njev: 57
hess_inv: <2x2 LbfgsInvHessProduct with dtype=float64>