-
Notifications
You must be signed in to change notification settings - Fork 2
/
LikelihoodBased.py
53 lines (40 loc) · 1.44 KB
/
LikelihoodBased.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
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 14 10:37:36 2022
@author: Cecilia
"""
import time
import numpy as np
class ImpSamp:
"""Class to perform likelihood-based importance sampling analysis
`model` a `Model` object encapsulating likelihood and importance distributions
`S` is number of samples from the importance distributions
`size` is number of samples from the the weighted importance sample
"""
def __init__(self, model, S, size):
self.model = model
self.S = S
self.size = size
def get_proposals(self):
self.model.get_parameters(self.S)
return(self.model.particles)
def compute_likelihood(self):
return(self.model.likelihood())
def get_weights(self):
"default uniform prior"
lik = self.compute_likelihood()
self.weights = lik/self.model.ImpProb
return(self.weights)
def sample(self):
start = time.time()
proposals = self.get_proposals()
w = self.get_weights()
end = time.time()
indexes = np.random.choice(list(range(self.S)), self.size, p=w/sum(w), replace=True)
self.ess = sum(self.weights)**2/sum(self.weights**2)
self.elapsed_time = end-start
print(
f"Effective sample size {self.ess}, "
f"elapsed sec {self.elapsed_time:.1f}"
)
return(proposals[indexes,:])