3.3 How would you improve your classifier? Is the decision rule given above optimal? Write a new code test3_3.py modifying test3_2.py and report new test accuracy. (hint: recall MAP rule.)
Answer:
The classifier provided in the original decision rule compares the likelihoods of the data under each class:
[
\delta(x) =
\begin{cases}
+1 & \text{if } N(x|\mu_{\text{pos}}, \sigma_{\text{pos}}^2) \geq N(x|\mu_{\text{neg}}, \sigma_{\text{neg}}^2) \
-1 & \text{otherwise}
\end{cases}
]
This decision rule does not take into account the prior probabilities of each class. The likelihoods ( N(x|\mu,\sigma^2) ) tell us how probable the data is under each class, but they don't tell us how probable each class is overall in the data. Ignoring class priors can be problematic, especially if the classes are imbalanced (i.e., one class occurs more frequently than the other).
Is the decision rule given above optimal?
No, the given decision rule is not optimal because it does not consider the prior probabilities of the classes. The optimal decision rule under the Bayesian framework is the Maximum A Posteriori (MAP) rule, which incorporates both the likelihood and the prior probability of each class.
How to improve the classifier:
To improve the classifier, we should use the MAP decision rule, which assigns each data point to the class with the highest posterior probability:
[
\delta_{\text{MAP}}(x) = \arg\max_{y \in {+1, -1}} P(y|x)
]
By Bayes' theorem, the posterior probability ( P(y|x) ) is proportional to the product of the likelihood ( P(x|y) ) and the prior probability ( P(y) ):
[
P(y|x) \propto P(x|y) P(y)
]
Therefore, the MAP decision rule becomes:
[
\delta_{\text{MAP}}(x) =
\begin{cases}
+1 & \text{if } P(y=+1) \cdot N(x|\mu_{\text{pos}}, \sigma_{\text{pos}}^2) \geq P(y=-1) \cdot N(x|\mu_{\text{neg}}, \sigma_{\text{neg}}^2) \
-1 & \text{otherwise}
\end{cases}
]
Modifying the code (test3_3.py):
To incorporate the prior probabilities into the decision rule, modify test3_2.py as follows:
-
Estimate Prior Probabilities:
Calculate the prior probabilities from the training data in train3_1.py:
[
P(y=+1) = \frac{\text{Number of positive examples}}{\text{Total number of examples}}
]
[
P(y=-1) = \frac{\text{Number of negative examples}}{\text{Total number of examples}}
]
-
Modify the Decision Rule:
In test3_3.py, update the classifier to use the MAP decision rule:
import numpy as np
# Load test data
test_data = np.load('./data_1/test.npz')
X_test = test_data['X']
y_test = test_data['y']
# Load parameters estimated from training data
params = np.load('params.npz')
mu_pos = params['mu_pos']
mu_neg = params['mu_neg']
sigma2_pos = params['sigma2_pos']
sigma2_neg = params['sigma2_neg']
P_pos = params['P_pos']
P_neg = params['P_neg']
# Calculate the likelihoods
def gaussian_pdf(x, mu, sigma2):
return (1 / np.sqrt(2 * np.pi * sigma2)) * np.exp(-0.5 * ((x - mu) ** 2) / sigma2)
# Predict labels using MAP rule
likelihood_pos = gaussian_pdf(X_test, mu_pos, sigma2_pos)
likelihood_neg = gaussian_pdf(X_test, mu_neg, sigma2_neg)
posterior_pos = P_pos * likelihood_pos
posterior_neg = P_neg * likelihood_neg
y_pred = np.where(posterior_pos >= posterior_neg, +1, -1)
# Calculate accuracy
accuracy = np.mean(y_pred == y_test)
print(f'Test Accuracy using MAP decision rule: {accuracy * 100:.2f}%')
In the above code:
P_pos and P_neg are the prior probabilities of the positive and negative classes, respectively.
- We compute the posterior probabilities by multiplying the prior probabilities with the likelihoods.
- The decision is made by comparing the posterior probabilities rather than just the likelihoods.
-
Update train3_1.py:
Ensure that the prior probabilities are saved when estimating parameters:
import numpy as np
# Load training data
train_data = np.load('./data_1/train.npz')
X_train = train_data['X']
y_train = train_data['y']
# Separate data by class
X_pos = X_train[y_train == +1]
X_neg = X_train[y_train == -1]
# Estimate parameters
mu_pos = np.mean(X_pos)
mu_neg = np.mean(X_neg)
sigma2_pos = np.var(X_pos, ddof=1) # Unbiased estimator
sigma2_neg = np.var(X_neg, ddof=1)
# Estimate prior probabilities
P_pos = len(X_pos) / len(X_train)
P_neg = len(X_neg) / len(X_train)
# Save parameters
np.savez('params.npz', mu_pos=mu_pos, mu_neg=mu_neg,
sigma2_pos=sigma2_pos, sigma2_neg=sigma2_neg,
P_pos=P_pos, P_neg=P_neg)
Report New Test Accuracy:
After modifying the code and running test3_3.py, suppose we obtain a new test accuracy of X%. (Replace X% with the actual accuracy obtained from running the code.)
For example:
Test Accuracy using MAP decision rule: 95.00%
Conclusion:
By incorporating the prior probabilities into the decision rule (using the MAP criterion), we improve the classifier's performance. This is because the MAP rule accounts for both how well the data fits each class (likelihood) and how common each class is overall (prior). The original decision rule was not optimal since it ignored class priors, potentially leading to suboptimal classification, especially in cases of class imbalance.
Note: Since I don't have access to the actual datasets (train.npz and test.npz), I cannot provide the exact test accuracy. Please run the provided code with your data to obtain the actual results.