-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdirichlet.py
More file actions
34 lines (24 loc) · 1.14 KB
/
Copy pathdirichlet.py
File metadata and controls
34 lines (24 loc) · 1.14 KB
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
import numpy as np
import math
class Dirichlet():
def __init__(self, shape=None, params=None):
if params is None:
self.params = np.ones(shape) # for simplicity we start with all parameters set to 1 (uniform)
else:
self.params = params
self.values = self.params.copy()
def infer(self, observations):
self.observations = observations
if self.values.shape != observations.shape: # in case of multiple observations
self.values += observations.sum(axis=0)
else:
self.values += observations # this is the very basic update function to get the posterior
def forget(self, fr):
# Only forget what is active
values_temp = self.values - self.params
values_temp *= (1 - fr)
self.values = values_temp + self.params
def get_MAP_cpd(self):
return self.values / self.values.sum(axis=0) # it's also very simple to get a point estimate for the prior over A
def get_full_cpd(self): # the MAP (point estimate) is a simplification. When considering the full distribution it gets more complicated...
pass