Comparison of MLE and MAP Estimation using Sample Data

 

Experiment

Comparison of MLE and MAP Estimation using Sample Data

๐ŸŽฏ Aim

To estimate parameters using MLE and MAP and compare  their behavior.


Objective

  • Generate sample data
  • Estimate mean using MLE
  • Estimate mean using MAP (with prior)
  • Compare results visually

๐Ÿ“– Theory 

What are MLE and MAP?


๐Ÿ”น 1. Maximum Likelihood Estimation (MLE)

๐Ÿ“Œ Idea:

Find parameters that maximize the likelihood of observed data

ฮธMLE=arg⁡max⁡P(D∣ฮธ)\theta_{MLE} = \arg\max P(D|\theta)

๐Ÿ‘‰ “Choose parameters that make the data most likely”



๐Ÿ”น 2. Maximum A Posteriori (MAP)

๐Ÿ“Œ Idea:

Includes prior knowledge about parameters

ฮธMAP=arg⁡max⁡P(D∣ฮธ)⋅P(ฮธ)\theta_{MAP} = \arg\max P(D|\theta) \cdot P(\theta)

๐Ÿ‘‰ “Choose parameters that fit data + prior belief”


๐Ÿ”‘ Key Difference

Method        Uses Data    Uses Prior
MLE✅❌
MAP✅✅

๐Ÿง  Intuition

  • MLE → trusts only data
  • MAP → balances data + prior belief

Assume:

  • Data follows Normal Distribution
  • Known variance

๐Ÿ”น MLE Estimate of Mean


MLE only uses the observed data.

Formula:

ฮผMLE=1n∑xi\mu_{MLE} = \frac{1}{n}\sum x_i
This is simply:

๐Ÿ‘‰ sample mean

​​

๐Ÿ”น MAP ( Maximum Aposteriori ) Estimate of Mean


๐Ÿ’ป Program

import numpy as np
import matplotlib.pyplot as plt

# -----------------------------
# Generate Sample Data
# -----------------------------
np.random.seed(42)

true_mean = 5
data = np.random.normal(loc=true_mean, scale=2, size=50)

# -----------------------------
# MLE Estimation
# -----------------------------
mle_mean = np.mean(data)

# -----------------------------
# MAP Estimation
# -----------------------------
# Prior (assume we believe mean is around 0)
prior_mean = 5
prior_variance = 1

# Data variance
data_variance = 4  # since std=2 → variance=4
n = len(data)

# MAP formula
map_mean = (n * mle_mean + (data_variance / prior_variance) * prior_mean) / \
           (n + (data_variance / prior_variance))

# -----------------------------
# Print Results
# -----------------------------
print("True Mean:", true_mean)
print("MLE Estimate:", round(mle_mean, 4))
print("MAP Estimate:", round(map_mean, 4))

# -----------------------------
# Visualization
# -----------------------------
plt.hist(data, bins=15, alpha=0.5, label="Data")

plt.axvline(true_mean, color='black', linestyle='--', label="True Mean")
plt.axvline(mle_mean, color='blue', label="MLE Mean")
plt.axvline(map_mean, color='red', label="MAP Mean")

plt.title("MLE vs MAP Estimation")
plt.legend()

plt.show()

๐Ÿ“Š  Output

True Mean: 5 MLE Estimate: 4.5491 MAP Estimate: 4.5825



vary the sample size and observe the output

๐Ÿ“ˆ Interpretation

๐Ÿ”น MLE

  • Close to sample data
  • Ignores prior

๐Ÿ”น MAP

  • Pulled toward prior (5)
  • Balances data + belief

Key Observation

ScenarioBehavior
Large data            MLE ≈ MAP
Small data            MAP influenced by prior

๐Ÿ“‰ Graph Explanation

  • Histogram → data distribution
  • Black line → true mean
  • Blue line → MLE
  • Red line → MAP

๐Ÿ‘‰ MAP shifts toward prior


Result

MLE and MAP estimation were implemented and compared. MAP incorporates prior knowledge, whereas MLE relies solely on observed data.

  • MLE is purely data-driven
  • MAP incorporates prior belief
  • MAP is useful when:
    • Data is limited
    • Prior knowledge exists

Comments

Popular posts from this blog

Machine Learning Lab PCCSL508 Semester 5 KTU CS 2024 Scheme manual - Dr Binu V P

Lab Assignment-2

Lab Assignment-1