-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBOCD_Algorithms.py
More file actions
167 lines (133 loc) · 6.78 KB
/
BOCD_Algorithms.py
File metadata and controls
167 lines (133 loc) · 6.78 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from __future__ import division
import numpy as np
from BOCD_modules import *
import matplotlib.pyplot as plt
"""
--------------------------------------------------------------------------------------------------------------------------------------
Bayesian Online Change-point Detection original version
Inputs:
-- environment: numpy array of piece-wise Bernoulli distributions
Outputs:
-- ChangePointEstimation: numpy array of change-point estimations
--------------------------------------------------------------------------------------------------------------------------------------
"""
def BOCD(environment):
#--------------- Initialization ---------------------
Horizon = environment.size
gamma = 1/Horizon # Switching Rate
alphas = np.array([1])
betas = np.array([1])
ForecasterDistribution = np.array([1])
ChangePointEstimation = np.array([])
#-----------------------------------------------------
#Interation with the environment ...
print('Launching BOCD ...')
for t in range(Horizon):
EstimatedBestExpert = np.argmax(ForecasterDistribution) #Change-point estimation
ChangePointEstimation = np.append(ChangePointEstimation,EstimatedBestExpert+1)
reward = int ((np.random.uniform() < environment[t]) == True) #Get the observation from the environment
ForecasterDistribution = updateForecasterDistribution(ForecasterDistribution, alphas, betas, reward, gamma)
(alphas, betas) = updateLaplacePrediction(alphas, betas, reward) #Update the laplace predictor
return ChangePointEstimation
"""
--------------------------------------------------------------------------------------------------------------------------------------
Bayesian Online Change-point Detection with original prior and restart
Inputs:
-- environment: numpy array of piece-wise Bernoulli distributions
Outputs:
-- ChangePointEstimation: numpy array of change-point estimations
--------------------------------------------------------------------------------------------------------------------------------------
"""
def BOCD_restart(environment):
#--------------- Initialization ---------------------
Horizon = environment.size
gamma = 1/Horizon # Switching Rate
alphas = np.array([1])
betas = np.array([1])
ForecasterDistribution = np.array([1])
ChangePointEstimation = np.array([])
Restart = 1 # Position of last restart
#-----------------------------------------------------
#Interation with the environment ...
print('Launching BOCD with restart ... ')
for t in range(Horizon):
EstimatedBestExpert = np.argmax(ForecasterDistribution)
# Restart precedure
if not(EstimatedBestExpert == 0):
# Reinitialization
alphas = np.array([1])
betas = np.array([1])
ForecasterDistribution = np.array([1])
Restart = t+1
ChangePointEstimation = np.append(ChangePointEstimation,Restart+1)#Change-point estimation
reward = int ((np.random.uniform() < environment[t]) == True) #Get the observation from the environment
ForecasterDistribution = updateForecasterDistribution(ForecasterDistribution, alphas, betas, reward, gamma)
(alphas, betas) = updateLaplacePrediction(alphas, betas, reward) #Update the laplace predictor
return ChangePointEstimation
"""
--------------------------------------------------------------------------------------------------------------------------------------
Bayesian Online Change-point Detection modified without restart and simple prior
Inputs:
-- environment: numpy array of piece-wise Bernoulli distributions
Outputs:
-- ChangePointEstimation: numpy array of change-point estimations
--------------------------------------------------------------------------------------------------------------------------------------
"""
def BOCDm(environment):
#--------------- Initialization ---------------------
Horizon = environment.size
gamma = 1/Horizon # Switching Rate
alphas = np.array([1])
betas = np.array([1])
ForecasterDistribution = np.array([1])
PseudoDist = np.array([1])
ChangePointEstimation = np.array([])
like1 = 1
#-----------------------------------------------------
#Interation with the environment ...
print('Launching BOCD modified ... ')
for t in range(Horizon):
EstimatedBestExpert = np.argmax(ForecasterDistribution)
ChangePointEstimation = np.append(ChangePointEstimation,EstimatedBestExpert+1) #Change-point estimation
reward = int ((np.random.uniform() < environment[t]) == True) #Get the observation from the environment
(ForecasterDistribution, PseudoDist, like1) = updateForecasterDistribution_m(ForecasterDistribution,PseudoDist, alphas, betas, reward, gamma, like1)
(alphas, betas) = updateLaplacePrediction(alphas, betas, reward) #Update the laplace predictor
return ChangePointEstimation
"""
--------------------------------------------------------------------------------------------------------------------------------------
Bayesian Online Change-point Detection modified without restart and simple prior
Inputs:
-- environment: numpy array of piece-wise Bernoulli distributions
Outputs:
-- ChangePointEstimation: numpy array of change-point estimations
--------------------------------------------------------------------------------------------------------------------------------------
"""
def BOCDm_restart(environment):
#--------------- Initialization ---------------------
Horizon = environment.size
gamma = 1/Horizon # Switching Rate
alphas = np.array([1])
betas = np.array([1])
ForecasterDistribution = np.array([1])
PseudoDist = np.array([1])
ChangePointEstimation = np.array([])
like1 = 1
Restart = 1 # Position of last restart
#------------------------------------------------------
#Interation with the environment ...
print('Launching BOCD modified with restart ...')
for t in range(Horizon):
EstimatedBestExpert = np.argmax(ForecasterDistribution)
# Restart precedure
if not(EstimatedBestExpert == 0):
# Reinitialization
alphas = np.array([1])
betas = np.array([1])
ForecasterDistribution = np.array([1])
Restart = t+1
like1 = 1
ChangePointEstimation = np.append(ChangePointEstimation,Restart+1) #Change-point estimation
reward = int ((np.random.uniform() < environment[t]) == True) #Get the observation from the environment
(ForecasterDistribution, PseudoDist, like1) = updateForecasterDistribution_m(ForecasterDistribution,PseudoDist, alphas, betas, reward, gamma, like1)
(alphas, betas) = updateLaplacePrediction(alphas, betas, reward) #Update the laplace predictor
return ChangePointEstimation