Gaussian Likelihood Practice Problem
This data science coding problem helps you practice Gaussian Naive Bayes, gaussian likelihood, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Gaussian Naive Bayes.
- Problem ID: 198
- Problem key: 198-gaussian-likelihood
- URL: https://datacrack.app/solve/198-gaussian-likelihood
- Difficulty: medium
- Topic: Gaussian Naive Bayes
- Module: Supervised Learning
Problem Statement
# 🧩 Gaussian Likelihood
---
### 🎯 Goal
Calculate the Gaussian likelihood of a numeric feature value given a class mean and variance.
---
### 📖 Introduction
Categorical Naive Bayes used counts to calculate likelihoods.
Gaussian Naive Bayes uses a curve. After training learns a mean and variance for a feature inside a class, it uses the Gaussian probability density formula:
$$
P(x\mid class)=\frac{1}{\sqrt{2\pi \cdot \text{variance}}}\times e^{-\frac{(x-\text{mean})^2}{2\cdot \text{variance}}}
$$
where:
- $x$ is the numeric value we want to score.
- `mean` is the average value learned from this class.
- `variance` measures the spread learned from this class.
- $\pi$ is the constant pi.
- $e$ is Euler's number.
This value is a **density**, not a categorical count probability. A higher density means the value is more typical for that class-feature curve.
Assume `variance > 0`. Do not use Laplace smoothing.
---
### 💻 Task
Implement `gaussian_likelihood(value, mean, variance)`.
Return the Gaussian density rounded to 6 decimals.
---
### 📥 Input / 📤 Output
**Input:** `value`, `mean`, and `variance`.
**Output:** one rounded likelihood density.
---
### 🧩 Starter Code
```python
def gaussian_likelihood(value, mean, variance):
# Your code here
pass
```
---
### 💡 Example
```python
gaussian_likelihood(5, 5, 4)
```
Expected output:
```python
0.199471
```
---
### ⚠️ Common Mistakes
- Treating the output like a count-based categorical probability.
- Forgetting the square root in the first part.
- Forgetting to square `(value - mean)`.
- Adding Laplace smoothing, which does not belong here.
Starter Code
def gaussian_likelihood(value, mean, variance):
# Your code here
pass