Warning
This page is located in archive.

Assignment: EM Algorithm

For Prior Estimation & Prior Shift Compensation

📅 Deadline: 8.1.2025 21:59

🏦 Points: 5

Task Description

In this assignment, you are tasked with implementing the EM algorithm for estimating class prior on unlabeled test data. Afterwards, you must use the estimated prior to account for prior shift in the plugin Bayes classifier. You can find the complete description of the assignment in the Assignment .

You are provided with a template containing the following files:

  • main.py: This file includes the functions e_step, m_step, compute_test_priors, bayes_classifier_with_prior_shift that you are required to implement.
  • utils.py: Contains helper functions for loading and saving data, and visualizations. You do not need to modify this file.
  • test-cases: A folder containing public test cases to help you verify your implementation before submitting to BRUTE.

Your objective is to implement the function e_step which estimates p_{test}(y|x), the function m_step which estimates p_{test}(y), the function compute_test_priors which runs the EM algorithm to estimate p_{test}(y), and the function bayes_classifier_with_prior_shift which implements the plugin-bayes classifier adapted to the prior shift. All of these functions can be found in main.py.

All python files must be stored in the root of the .zip sent for submission.
The expected runtime of the evaluation is about 7 minutes.

Go make yourself a coffee while you wait.

The Bayes classifier test might fail on your machine, but still be correct. So far, we observed it in code of 1 student. It is caused by having different versions of Scikit-Learn. Jakub

Further Reading

How to Test

After completing your implementation, you can test your solution using the following commands before submitting it to BRUTE:


Test Case 1
python main.py test-cases/public/instances/instance_1.json --plot

Expected output:

EM Algorithm - E Step:
  e_step: Test OK
  
EM Algorithm - M Step:
  m_step: Test OK

EM Algorithm - Estimated priors:
  estimated_test_prior: Test OK

Bayes Classifier:
  risks bayes_classifier: Test OK
  
Without prior compensation; Loss: 0.13
With prior compensation; Loss: 0.09
Improvement:  0.04

Test Case 2
python main.py test-cases/public/instances/instance_2.json --plot

Expected output:

EM Algorithm - E Step:
  e_step: Test OK
  
EM Algorithm - M Step:
  m_step: Test OK

EM Algorithm - Estimated priors:
  estimated_test_prior: Test OK

Bayes Classifier:
  risks bayes_classifier: Test OK
  
Without prior compensation; Loss: 0.43
With prior compensation; Loss: 0.37
Improvement:  0.06

Test Case 3
python main.py test-cases/public/instances/instance_3.json --plot

Expected output:

EM Algorithm - E Step:
  e_step: Test OK
  
EM Algorithm - M Step:
  m_step: Test OK

EM Algorithm - Estimated priors:
  estimated_test_prior: Test OK

Bayes Classifier:
  risks bayes_classifier: Test OK
  
Without prior compensation; Loss: 0.13
With prior compensation; Loss: 0.1
Improvement:  0.03

Submission Guidelines

  • Submit the completed code as a .zip via BRUTE.
  • All python files must be stored in the root of the .zip sent for submission.
  • Make sure your implementation passes the test cases provided above. Good luck! 😊
courses/be4m33ssu/homeworks/hw_em_prior_shift.txt · Last modified: 2025/01/10 10:21 by paplhjak