การประมาณค่าโมเดลและ Likelihood

Introduction to Linear Modeling in Python

Jason Vestuto

Data Scientist

การประมาณค่า

กราฟฮิสโตแกรมแสดงจำนวนนับที่ปรับมาตราฐานแล้วเทียบกับช่วงระยะทาง โดยข้อมูลแสดงเป็นแท่งสีเทา และโมเดลแสดงเป็นเส้นโค้งกระดิ่งเกาส์เซียนสีแดงที่พอดีกับยอดแท่งสีเทา

Introduction to Linear Modeling in Python

การประมาณค่า

# Define gaussian model function
def gaussian_model(x, mu, sigma):
    coeff_part = 1/(np.sqrt(2 * np.pi * sigma**2))
    exp_part = np.exp( - (x - mu)**2 / (2 * sigma**2) )
    return coeff_part*exp_part
# Compute sample statistics
mean = np.mean(sample)
stdev = np.std(sample)
# Model the population using sample statistics
population_model = gaussian(sample, mu=mean, sigma=stdev)
Introduction to Linear Modeling in Python

Likelihood กับ ความน่าจะเป็น

  • ความน่าจะเป็นแบบมีเงื่อนไข: $P( \text{outcome A} | \text{given B})$
  • ความน่าจะเป็น: $P( \text{data} | \text{model} )$
  • Likelihood: $L( \text{model} | \text{data} )$
Introduction to Linear Modeling in Python

การคำนวณ Likelihood

กราฟความน่าจะเป็นเทียบกับระยะทาง แสดงเป็นเส้นโค้งกระดิ่งเกาส์เซียนสีแดง มีจุดหนึ่งใกล้ขอบซ้าย พร้อมเส้นตั้งและเส้นนอนเชื่อมจุดนั้นไปยังแต่ละแกน

Introduction to Linear Modeling in Python

การคำนวณ Likelihood

กราฟความน่าจะเป็นเทียบกับระยะทาง แสดงเป็นเส้นโค้งกระดิ่งเกาส์เซียนสีแดง มี 6 จุดจากขอบซ้ายไปทางกึ่งกลาง พร้อมเส้นตั้งและเส้นนอนเชื่อมแต่ละจุดไปยังทั้งสองแกน

Introduction to Linear Modeling in Python

การคำนวณ Likelihood จากความน่าจะเป็น

# Guess parameters
mu_guess = np.mean(sample_distances)
sigma_guess = np.std(sample_distances)
# For each sample point, compute a probability
probabilities = np.zeros(len(sample_distances))
for n, distance in enumerate(sample_distances):
    probabilities[n] = gaussian_model(distance, mu=mu_guess, sigma=sigma_guess)
likelihood = np.product(probs)
loglikelihood = np.sum(np.log(probs))
Introduction to Linear Modeling in Python

การประมาณค่า Maximum Likelihood

# Create an array of mu guesses
low_guess = sample_mean - 2*sample_stdev
high_guess = sample_mean + 2*sample_stdev
mu_guesses = np.linspace(low_guess, high_guess, 101)
# Compute the loglikelihood for each guess
loglikelihoods = np.zeros(len(mu_guesses))
for n, mu_guess in enumerate(mu_guesses):
    loglikelihoods[n] = compute_loglikelihood(sample_distances, mu=mu_guess, sigma=sample_stdev)
# Find the best guess
max_loglikelihood = np.max(loglikelihoods)
best_mu = mu_guesses[loglikelihoods == max_loglikelihood]
Introduction to Linear Modeling in Python

การประมาณค่า Maximum Likelihood

กราฟพาราโบลาหันลง มีจุดสีแดงที่จุดสูงสุด บนแกน loglikelihood เทียบกับค่า mu

Introduction to Linear Modeling in Python

มาฝึกกันเถอะ!

Introduction to Linear Modeling in Python

Preparing Video For Download...