-
Notifications
You must be signed in to change notification settings - Fork 0
/
expectation_maximisation.py
86 lines (74 loc) · 2.79 KB
/
expectation_maximisation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from __future__ import annotations
from typing import List, Tuple
import numpy as np
from src.models.binary_latent_factor_approximations.abstract_binary_latent_factor_approximation import (
AbstractBinaryLatentFactorApproximation,
)
from src.models.binary_latent_factor_models.binary_latent_factor_model import (
AbstractBinaryLatentFactorModel,
)
def is_converge(
free_energies: List[float],
current_lambda_matrix: np.ndarray,
previous_lambda_matrix: np.ndarray,
) -> bool:
"""
Check for convergence of free energy and lambda matrix
:param free_energies: list of free energies
:param current_lambda_matrix: current lambda matrix
:param previous_lambda_matrix: previous lambda matrix
:return: boolean indicating convergence
"""
return (abs(free_energies[-1] - free_energies[-2]) == 0) and np.linalg.norm(
current_lambda_matrix - previous_lambda_matrix
) == 0
def learn_binary_factors(
x: np.ndarray,
em_iterations: int,
binary_latent_factor_model: AbstractBinaryLatentFactorModel,
binary_latent_factor_approximation: AbstractBinaryLatentFactorApproximation,
) -> Tuple[
AbstractBinaryLatentFactorApproximation,
AbstractBinaryLatentFactorModel,
List[float],
]:
"""
Expectation maximisation algorithm to learn binary factors.
:param x: data matrix (number_of_points, number_of_dimensions)
:param em_iterations: number of iterations to run EM
:param binary_latent_factor_model: a binary_latent_factor_model
:param binary_latent_factor_approximation: a binary_latent_factor_approximation
:return: a Tuple containing the updated binary_latent_factor_model, updated binary_latent_factor_approximation,
and free energies during each step of EM
"""
free_energies: List[float] = [
binary_latent_factor_approximation.compute_free_energy(
x, binary_latent_factor_model
)
]
for _ in range(em_iterations):
previous_lambda_matrix = np.copy(
binary_latent_factor_approximation.lambda_matrix
)
# E step
binary_latent_factor_approximation.variational_expectation_step(
x=x,
binary_latent_factor_model=binary_latent_factor_model,
)
# M step
binary_latent_factor_model.maximisation_step(
x,
binary_latent_factor_approximation,
)
free_energies.append(
binary_latent_factor_approximation.compute_free_energy(
x, binary_latent_factor_model
)
)
if is_converge(
free_energies,
binary_latent_factor_approximation.lambda_matrix,
previous_lambda_matrix,
):
break
return binary_latent_factor_approximation, binary_latent_factor_model, free_energies