เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การหาค่า Likelihood สูงสุด ตอนที่ 2

ในตอนที่ 1 เราคำนวณค่า log-likelihood เพียงค่าเดียวสำหรับ mu ค่าเดียว ในตอนที่ 2 นี้ จะนำฟังก์ชัน compute_loglikelihood() ที่กำหนดไว้แล้วมาใช้คำนวณอาร์เรย์ของค่า log-likelihood โดยคำนวณหนึ่งค่าสำหรับแต่ละสมาชิกในอาร์เรย์ของค่า mu ที่เป็นไปได้

เป้าหมายคือการหาว่าค่าประมาณ mu ใดที่ให้ค่าสูงสุดในอาร์เรย์ของ loglikelihood

ให้เริ่มต้นด้วยข้อมูลที่โหลดไว้ล่วงหน้า ได้แก่ sample_distances, sample_mean, sample_stdev และฟังก์ชันช่วย compute_loglikelihood()

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Introduction to Linear Modeling in Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • สร้าง mu_guesses โดยใช้ค่าที่มีจุดศูนย์กลางอยู่ที่ sample_mean และกระจายตามช่วง sample_stdev
  • สำหรับค่าประมาณแต่ละค่า mu_guess ใน mu_guesses ให้ใช้ compute_loglikelihood() กับ sample_distances ทั้งหมด โดยคงค่า sigma ไว้ที่ sample_stdev
  • หาค่าสูงสุดในอาร์เรย์ loglikelihoods แล้วใช้ index ของค่านั้นเพื่อค้นหา best_mu จาก mu_guesses
  • แสดงผล best_mu และสร้างกราฟ loglikelihoods เพื่อแสดงภาพรวม

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Create an array of mu guesses, centered on sample_mean, spread out +/- by sample_stdev
low_guess = sample_mean - 2*sample_stdev
high_guess = sample_mean + 2*sample_stdev
mu_guesses = np.linspace(____, ____, 101)

# Compute the loglikelihood for each model created from each guess value
loglikelihoods = np.zeros(len(mu_guesses))
for n, mu_guess in enumerate(____):
    loglikelihoods[n] = compute_loglikelihood(____, mu=____, sigma=sample_stdev)

# Find the best guess by using logical indexing, the print and plot the result
best_mu = mu_guesses[loglikelihoods==np.max(____)]
print('Maximum loglikelihood found for best mu guess={}'.format(____))
fig = plot_loglikelihoods(mu_guesses, loglikelihoods)
แก้ไขและรันโค้ด