physbo 2.0.0__cp310-cp310-macosx_12_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- physbo/__init__.py +17 -0
- physbo/blm/__init__.py +17 -0
- physbo/blm/basis/__init__.py +8 -0
- physbo/blm/basis/fourier.py +148 -0
- physbo/blm/core/__init__.py +8 -0
- physbo/blm/core/model.py +257 -0
- physbo/blm/inf/__init__.py +8 -0
- physbo/blm/inf/exact.py +192 -0
- physbo/blm/lik/__init__.py +10 -0
- physbo/blm/lik/_src/__init__.py +8 -0
- physbo/blm/lik/_src/cov.py +113 -0
- physbo/blm/lik/gauss.py +136 -0
- physbo/blm/lik/linear.py +117 -0
- physbo/blm/predictor.py +238 -0
- physbo/blm/prior/__init__.py +8 -0
- physbo/blm/prior/gauss.py +215 -0
- physbo/gp/__init__.py +15 -0
- physbo/gp/core/__init__.py +11 -0
- physbo/gp/core/learning.py +364 -0
- physbo/gp/core/model.py +420 -0
- physbo/gp/core/prior.py +207 -0
- physbo/gp/cov/__init__.py +8 -0
- physbo/gp/cov/_src/__init__.py +1 -0
- physbo/gp/cov/_src/enhance_gauss.cpython-310-darwin.so +0 -0
- physbo/gp/cov/gauss.py +393 -0
- physbo/gp/inf/__init__.py +8 -0
- physbo/gp/inf/exact.py +231 -0
- physbo/gp/lik/__init__.py +8 -0
- physbo/gp/lik/gauss.py +179 -0
- physbo/gp/mean/__init__.py +9 -0
- physbo/gp/mean/const.py +150 -0
- physbo/gp/mean/zero.py +66 -0
- physbo/gp/predictor.py +170 -0
- physbo/misc/__init__.py +15 -0
- physbo/misc/_src/__init__.py +1 -0
- physbo/misc/_src/cholupdate.cpython-310-darwin.so +0 -0
- physbo/misc/_src/diagAB.cpython-310-darwin.so +0 -0
- physbo/misc/_src/logsumexp.cpython-310-darwin.so +0 -0
- physbo/misc/_src/traceAB.cpython-310-darwin.so +0 -0
- physbo/misc/centering.py +28 -0
- physbo/misc/gauss_elim.py +35 -0
- physbo/misc/set_config.py +299 -0
- physbo/opt/__init__.py +8 -0
- physbo/opt/adam.py +107 -0
- physbo/predictor.py +261 -0
- physbo/search/__init__.py +11 -0
- physbo/search/discrete/__init__.py +11 -0
- physbo/search/discrete/policy.py +804 -0
- physbo/search/discrete/results.py +192 -0
- physbo/search/discrete_multi/__init__.py +11 -0
- physbo/search/discrete_multi/policy.py +552 -0
- physbo/search/discrete_multi/results.py +128 -0
- physbo/search/pareto.py +206 -0
- physbo/search/score.py +155 -0
- physbo/search/score_multi.py +197 -0
- physbo/search/utility.py +101 -0
- physbo/variable.py +222 -0
- physbo-2.0.0.dist-info/METADATA +110 -0
- physbo-2.0.0.dist-info/RECORD +61 -0
- physbo-2.0.0.dist-info/WHEEL +5 -0
- physbo-2.0.0.dist-info/top_level.txt +1 -0
physbo/__init__.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
# Copyright (C) 2020- The University of Tokyo
|
|
3
|
+
#
|
|
4
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
5
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
|
+
|
|
8
|
+
from . import gp
|
|
9
|
+
from . import opt
|
|
10
|
+
from . import blm
|
|
11
|
+
from . import misc
|
|
12
|
+
from . import search
|
|
13
|
+
from . import predictor
|
|
14
|
+
from .predictor import base_predictor
|
|
15
|
+
from .variable import variable
|
|
16
|
+
|
|
17
|
+
__version__ = "2.0.0"
|
physbo/blm/__init__.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
# Copyright (C) 2020- The University of Tokyo
|
|
3
|
+
#
|
|
4
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
5
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
|
+
|
|
8
|
+
""" Bayesian Linear Model
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from . import basis
|
|
12
|
+
from . import prior
|
|
13
|
+
from . import lik
|
|
14
|
+
from . import inf
|
|
15
|
+
|
|
16
|
+
from .core import model
|
|
17
|
+
from .predictor import predictor
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
# Copyright (C) 2020- The University of Tokyo
|
|
3
|
+
#
|
|
4
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
5
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
|
+
|
|
8
|
+
from .fourier import fourier
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
# Copyright (C) 2020- The University of Tokyo
|
|
3
|
+
#
|
|
4
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
5
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
|
+
|
|
8
|
+
# -*- coding:utf-8 -*-
|
|
9
|
+
import numpy as np
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class fourier:
|
|
13
|
+
"""
|
|
14
|
+
random feature maps
|
|
15
|
+
``Psi(X; W,b) = cos[X * Wt + b] * alpha``
|
|
16
|
+
where
|
|
17
|
+
|
|
18
|
+
- X: input, N-by-d matrix
|
|
19
|
+
- W: weight, l-by-d matrix
|
|
20
|
+
- Wt: transpose of W
|
|
21
|
+
- b: bias, 1-by-l matrix
|
|
22
|
+
- alpha: coefficient
|
|
23
|
+
|
|
24
|
+
and
|
|
25
|
+
|
|
26
|
+
- N: number of data
|
|
27
|
+
- d: dimension of input
|
|
28
|
+
- l: number of basis
|
|
29
|
+
|
|
30
|
+
Attributes
|
|
31
|
+
==========
|
|
32
|
+
params: Tuple
|
|
33
|
+
W, b, alpha
|
|
34
|
+
nbasis: int
|
|
35
|
+
number of basis
|
|
36
|
+
|
|
37
|
+
References
|
|
38
|
+
==========
|
|
39
|
+
A. Rahimi and B. Recht, "Random features for large-scale kernel machines,"
|
|
40
|
+
in "Advances in neural information processing systems," 2007, pp. 1177-1184.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def __init__(self, params):
|
|
44
|
+
"""
|
|
45
|
+
Parameters
|
|
46
|
+
----------
|
|
47
|
+
params: Tuple
|
|
48
|
+
W, b, alpha
|
|
49
|
+
"""
|
|
50
|
+
self._check_params(params)
|
|
51
|
+
self._check_len_params(params)
|
|
52
|
+
self.params = params
|
|
53
|
+
self.nbasis = self.params[1].shape[0]
|
|
54
|
+
|
|
55
|
+
def get_basis(self, X, params=None):
|
|
56
|
+
"""
|
|
57
|
+
compute the value of basis
|
|
58
|
+
|
|
59
|
+
Parameters
|
|
60
|
+
==========
|
|
61
|
+
X: numpy.ndarray
|
|
62
|
+
input
|
|
63
|
+
params: Tuple
|
|
64
|
+
W, b, alpha
|
|
65
|
+
(default: self.params)
|
|
66
|
+
|
|
67
|
+
Returns
|
|
68
|
+
=======
|
|
69
|
+
Psi(X; W,b): numpy.ndarray
|
|
70
|
+
N-by-l matrix
|
|
71
|
+
|
|
72
|
+
``cos[X * Wt + b] * alpha``
|
|
73
|
+
|
|
74
|
+
where ``Wt`` is the transpose of ``W``.
|
|
75
|
+
"""
|
|
76
|
+
if params is None:
|
|
77
|
+
params = self.params
|
|
78
|
+
|
|
79
|
+
self._check_params(params)
|
|
80
|
+
self._check_len_params(params)
|
|
81
|
+
|
|
82
|
+
return np.cos(np.dot(X, params[0].transpose()) + params[1]) * params[2]
|
|
83
|
+
|
|
84
|
+
def set_params(self, params):
|
|
85
|
+
"""
|
|
86
|
+
update basis parameters
|
|
87
|
+
|
|
88
|
+
Parameters
|
|
89
|
+
==========
|
|
90
|
+
params: tuple
|
|
91
|
+
W, b, alpha
|
|
92
|
+
|
|
93
|
+
"""
|
|
94
|
+
self._check_params(params)
|
|
95
|
+
self._check_len_params(params)
|
|
96
|
+
self.params = params
|
|
97
|
+
|
|
98
|
+
def show(self):
|
|
99
|
+
"""
|
|
100
|
+
print parameters
|
|
101
|
+
"""
|
|
102
|
+
print("W = ", self.params[0])
|
|
103
|
+
print("b = ", self.params[1])
|
|
104
|
+
print("alpha = ", self.params[2])
|
|
105
|
+
|
|
106
|
+
def _check_params(self, params):
|
|
107
|
+
"""
|
|
108
|
+
Parameters
|
|
109
|
+
==========
|
|
110
|
+
params: tuple
|
|
111
|
+
W, b, alpha
|
|
112
|
+
|
|
113
|
+
Raises
|
|
114
|
+
======
|
|
115
|
+
ValueError
|
|
116
|
+
if ``params`` is not a 3-dimensional tuple
|
|
117
|
+
"""
|
|
118
|
+
if not isinstance(params, tuple):
|
|
119
|
+
raise ValueError("The variable < params > must be a tuple.")
|
|
120
|
+
|
|
121
|
+
if len(params) != 3:
|
|
122
|
+
raise ValueError("The variable < params > must be 3-dimensional tuple.")
|
|
123
|
+
|
|
124
|
+
def _check_len_params(self, params):
|
|
125
|
+
"""
|
|
126
|
+
Parameters
|
|
127
|
+
==========
|
|
128
|
+
params: tuple
|
|
129
|
+
W, b, alpha
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
Raises
|
|
133
|
+
======
|
|
134
|
+
ValueError
|
|
135
|
+
when dim of W and b are mismatch
|
|
136
|
+
or alpha is not a scalar
|
|
137
|
+
"""
|
|
138
|
+
if params[0].shape[0] != params[1].shape[0]:
|
|
139
|
+
raise ValueError(
|
|
140
|
+
"The length of 0-axis of W must be same as the length of b."
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
if hasattr(params[2], "__len__"):
|
|
144
|
+
if len(params[2]) != 1:
|
|
145
|
+
raise ValueError("The third entry of <params> must be a scalar.")
|
|
146
|
+
else:
|
|
147
|
+
if isinstance(params[2], str):
|
|
148
|
+
raise ValueError("The third entry of <params> must be a scalar.")
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
# Copyright (C) 2020- The University of Tokyo
|
|
3
|
+
#
|
|
4
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
5
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
|
+
|
|
8
|
+
from .model import model
|
physbo/blm/core/model.py
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
# Copyright (C) 2020- The University of Tokyo
|
|
3
|
+
#
|
|
4
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
5
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
from .. import inf
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class model:
|
|
13
|
+
"""
|
|
14
|
+
Baysean Linear Model
|
|
15
|
+
|
|
16
|
+
Attributes
|
|
17
|
+
==========
|
|
18
|
+
prior: physbo.blm.prior.gauss
|
|
19
|
+
prior distribution of weights
|
|
20
|
+
lik: physbo.blm.lik.gauss
|
|
21
|
+
kernel
|
|
22
|
+
nbasis: int
|
|
23
|
+
number of features in random feature map
|
|
24
|
+
stats: Tuple
|
|
25
|
+
auxially parameters for sampling
|
|
26
|
+
method: str
|
|
27
|
+
sampling method
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(self, lik, prior, options={}):
|
|
31
|
+
self.prior = prior
|
|
32
|
+
self.lik = lik
|
|
33
|
+
self.nbasis = self.lik.linear.basis.nbasis
|
|
34
|
+
self._init_prior(prior)
|
|
35
|
+
self._set_options(options)
|
|
36
|
+
self.stats = ()
|
|
37
|
+
|
|
38
|
+
def prepare(self, X, t, Psi=None):
|
|
39
|
+
"""
|
|
40
|
+
initializes model by using the first training dataset
|
|
41
|
+
|
|
42
|
+
Parameters
|
|
43
|
+
==========
|
|
44
|
+
X: numpy.ndarray
|
|
45
|
+
inputs
|
|
46
|
+
t: numpy.ndarray
|
|
47
|
+
target (label)
|
|
48
|
+
Psi: numpy.ndarray
|
|
49
|
+
feature maps
|
|
50
|
+
|
|
51
|
+
See also
|
|
52
|
+
========
|
|
53
|
+
physbo.blm.inf.exact.prepare
|
|
54
|
+
"""
|
|
55
|
+
if self.method == "exact":
|
|
56
|
+
inf.exact.prepare(blm=self, X=X, t=t, Psi=Psi)
|
|
57
|
+
else:
|
|
58
|
+
pass
|
|
59
|
+
|
|
60
|
+
def update_stats(self, x, t, psi=None):
|
|
61
|
+
"""
|
|
62
|
+
updates model by using another training data
|
|
63
|
+
|
|
64
|
+
Parameters
|
|
65
|
+
==========
|
|
66
|
+
x: numpy.ndarray
|
|
67
|
+
input
|
|
68
|
+
t: float
|
|
69
|
+
target (label)
|
|
70
|
+
psi: numpy.ndarray
|
|
71
|
+
feature map
|
|
72
|
+
|
|
73
|
+
See also
|
|
74
|
+
========
|
|
75
|
+
physbo.blm.inf.exact.update_stats
|
|
76
|
+
"""
|
|
77
|
+
if self.method == "exact":
|
|
78
|
+
self.stats = inf.exact.update_stats(self, x, t, psi)
|
|
79
|
+
else:
|
|
80
|
+
pass
|
|
81
|
+
|
|
82
|
+
def get_post_params_mean(self):
|
|
83
|
+
"""
|
|
84
|
+
calculates posterior mean of weights
|
|
85
|
+
|
|
86
|
+
Returns
|
|
87
|
+
=======
|
|
88
|
+
numpy.ndarray
|
|
89
|
+
|
|
90
|
+
See also
|
|
91
|
+
========
|
|
92
|
+
physbo.blm.inf.exact.get_post_params_mean
|
|
93
|
+
"""
|
|
94
|
+
if self.method == "exact":
|
|
95
|
+
self.lik.linear.params = inf.exact.get_post_params_mean(blm=self)
|
|
96
|
+
|
|
97
|
+
def get_post_fmean(self, X, Psi=None, w=None):
|
|
98
|
+
"""
|
|
99
|
+
calculates posterior mean of model (function)
|
|
100
|
+
|
|
101
|
+
Parameters
|
|
102
|
+
==========
|
|
103
|
+
X: numpy.ndarray
|
|
104
|
+
inputs
|
|
105
|
+
Psi: numpy.ndarray
|
|
106
|
+
feature maps
|
|
107
|
+
w: numpy.ndarray
|
|
108
|
+
weight
|
|
109
|
+
|
|
110
|
+
See also
|
|
111
|
+
========
|
|
112
|
+
physbo.blm.inf.exact.get_post_fmean
|
|
113
|
+
"""
|
|
114
|
+
if self.method == "exact":
|
|
115
|
+
fmu = inf.exact.get_post_fmean(self, X, Psi, w)
|
|
116
|
+
else:
|
|
117
|
+
pass
|
|
118
|
+
return fmu
|
|
119
|
+
|
|
120
|
+
def sampling(self, w_mu=None, N=1, alpha=1.0):
|
|
121
|
+
"""
|
|
122
|
+
draws samples of weights
|
|
123
|
+
|
|
124
|
+
Parameters
|
|
125
|
+
==========
|
|
126
|
+
blm: physbo.blm.core.model
|
|
127
|
+
model
|
|
128
|
+
w_mu: numpy.ndarray
|
|
129
|
+
mean of weight
|
|
130
|
+
N: int
|
|
131
|
+
the number of samples
|
|
132
|
+
(default: 1)
|
|
133
|
+
alpha: float
|
|
134
|
+
noise for sampling source
|
|
135
|
+
(default: 1.0)
|
|
136
|
+
|
|
137
|
+
Returns
|
|
138
|
+
=======
|
|
139
|
+
numpy.ndarray
|
|
140
|
+
samples of weights
|
|
141
|
+
|
|
142
|
+
See also
|
|
143
|
+
========
|
|
144
|
+
physbo.blm.inf.exact.sampling
|
|
145
|
+
"""
|
|
146
|
+
if self.method == "exact":
|
|
147
|
+
w_hat = inf.exact.sampling(self, w_mu, N, alpha=alpha)
|
|
148
|
+
else:
|
|
149
|
+
pass
|
|
150
|
+
return w_hat
|
|
151
|
+
|
|
152
|
+
def post_sampling(self, Xtest, Psi=None, N=1, alpha=1.0):
|
|
153
|
+
"""
|
|
154
|
+
draws samples of mean value of model
|
|
155
|
+
|
|
156
|
+
Parameters
|
|
157
|
+
==========
|
|
158
|
+
Xtest: numpy.ndarray
|
|
159
|
+
inputs
|
|
160
|
+
Psi: numpy.ndarray
|
|
161
|
+
feature maps
|
|
162
|
+
(default: ``blm.lik.get_basis(Xtest)``)
|
|
163
|
+
N: int
|
|
164
|
+
number of samples
|
|
165
|
+
(default: 1)
|
|
166
|
+
alpha: float
|
|
167
|
+
noise for sampling source
|
|
168
|
+
|
|
169
|
+
Returns
|
|
170
|
+
=======
|
|
171
|
+
numpy.ndarray
|
|
172
|
+
"""
|
|
173
|
+
if Psi is None:
|
|
174
|
+
Psi = self.lik.get_basis(Xtest)
|
|
175
|
+
w_hat = self.sampling(N=N, alpha=alpha)
|
|
176
|
+
return Psi.dot(w_hat) + self.lik.linear.bias
|
|
177
|
+
|
|
178
|
+
def predict_sampling(self, Xtest, Psi=None, N=1):
|
|
179
|
+
"""
|
|
180
|
+
draws samples from model
|
|
181
|
+
|
|
182
|
+
Parameters
|
|
183
|
+
==========
|
|
184
|
+
Xtest: numpy.ndarray
|
|
185
|
+
inputs
|
|
186
|
+
Psi: numpy.ndarray
|
|
187
|
+
feature map
|
|
188
|
+
(default: ``blm.lik.get_basis(Xtest)``)
|
|
189
|
+
N: int
|
|
190
|
+
number of samples
|
|
191
|
+
(default: 1)
|
|
192
|
+
|
|
193
|
+
Returns
|
|
194
|
+
=======
|
|
195
|
+
numpy.ndarray
|
|
196
|
+
"""
|
|
197
|
+
if Xtest.shape[0] == 0:
|
|
198
|
+
return np.zeros((0, N))
|
|
199
|
+
fmean = self.post_sampling(Xtest, Psi, N=N)
|
|
200
|
+
A = np.random.randn(Xtest.shape[0], N)
|
|
201
|
+
return fmean + np.sqrt(self.lik.cov.sigma2) * A
|
|
202
|
+
|
|
203
|
+
def get_post_fcov(self, X, Psi=None, diag=True):
|
|
204
|
+
"""
|
|
205
|
+
calculates posterior covariance of model
|
|
206
|
+
|
|
207
|
+
Parameters
|
|
208
|
+
==========
|
|
209
|
+
X: numpy.ndarray
|
|
210
|
+
inputs
|
|
211
|
+
Psi: numpy.ndarray
|
|
212
|
+
feature maps
|
|
213
|
+
(default: blm.lik.linear.basis.get_basis(X))
|
|
214
|
+
diag: bool
|
|
215
|
+
if True, returns only variances as a diagonal matrix
|
|
216
|
+
(default: True)
|
|
217
|
+
|
|
218
|
+
Returns
|
|
219
|
+
=======
|
|
220
|
+
numpy.ndarray
|
|
221
|
+
|
|
222
|
+
See also
|
|
223
|
+
========
|
|
224
|
+
physbo.blm.inf.exact.get_post_fcov
|
|
225
|
+
"""
|
|
226
|
+
if self.method == "exact":
|
|
227
|
+
fcov = inf.exact.get_post_fcov(self, X, Psi, diag=True)
|
|
228
|
+
else:
|
|
229
|
+
pass
|
|
230
|
+
return fcov
|
|
231
|
+
|
|
232
|
+
def _set_options(self, options):
|
|
233
|
+
"""
|
|
234
|
+
read options
|
|
235
|
+
|
|
236
|
+
Parameters
|
|
237
|
+
==========
|
|
238
|
+
options: dict
|
|
239
|
+
|
|
240
|
+
- 'method' : sampling method
|
|
241
|
+
|
|
242
|
+
- 'exact' (default)
|
|
243
|
+
"""
|
|
244
|
+
self.method = options.get("method", "exact")
|
|
245
|
+
|
|
246
|
+
def _init_prior(self, prior):
|
|
247
|
+
"""
|
|
248
|
+
sets the prior distribution
|
|
249
|
+
|
|
250
|
+
Parameters
|
|
251
|
+
==========
|
|
252
|
+
prior: physbo.blm.prior.gauss
|
|
253
|
+
if None, prior.gauss(self.nbasis)
|
|
254
|
+
"""
|
|
255
|
+
if prior is None:
|
|
256
|
+
prior = prior.gauss(self.nbasis)
|
|
257
|
+
self.prior = prior
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
# Copyright (C) 2020- The University of Tokyo
|
|
3
|
+
#
|
|
4
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
5
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
|
+
|
|
8
|
+
from . import exact
|
physbo/blm/inf/exact.py
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
# Copyright (C) 2020- The University of Tokyo
|
|
3
|
+
#
|
|
4
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
5
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
import scipy
|
|
10
|
+
|
|
11
|
+
import physbo.misc as misc
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def prepare(blm, X, t, Psi=None):
|
|
15
|
+
"""
|
|
16
|
+
initializes auxiaialy parameters for quick sampling
|
|
17
|
+
|
|
18
|
+
``blm.stats`` will be updated.
|
|
19
|
+
|
|
20
|
+
Parameters
|
|
21
|
+
==========
|
|
22
|
+
blm: physbo.blm.core.model
|
|
23
|
+
model
|
|
24
|
+
X: numpy.ndarray
|
|
25
|
+
inputs
|
|
26
|
+
t: numpy.ndarray
|
|
27
|
+
target (label)
|
|
28
|
+
Psi:
|
|
29
|
+
feature maps (default: blm.lik.get_basis(X))
|
|
30
|
+
"""
|
|
31
|
+
if Psi is None:
|
|
32
|
+
Psi = blm.lik.get_basis(X)
|
|
33
|
+
PsiT = Psi.transpose()
|
|
34
|
+
G = np.dot(PsiT, Psi) * blm.lik.cov.prec
|
|
35
|
+
A = G + blm.prior.get_prec()
|
|
36
|
+
U = scipy.linalg.cholesky(A, check_finite=False)
|
|
37
|
+
b = PsiT.dot(t - blm.lik.linear.bias)
|
|
38
|
+
alpha = misc.gauss_elim(U, b)
|
|
39
|
+
blm.stats = (U, b, alpha)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def update_stats(blm, x, t, psi=None):
|
|
43
|
+
"""
|
|
44
|
+
calculates new auxiaialy parameters for quick sampling by fast-update
|
|
45
|
+
|
|
46
|
+
Parameters
|
|
47
|
+
==========
|
|
48
|
+
blm: physbo.blm.core.model
|
|
49
|
+
model
|
|
50
|
+
x: numpy.ndarray
|
|
51
|
+
input
|
|
52
|
+
t: numpy.ndarray
|
|
53
|
+
target (label)
|
|
54
|
+
psi:
|
|
55
|
+
feature map (default: blm.lik.get_basis(X))
|
|
56
|
+
|
|
57
|
+
Returns
|
|
58
|
+
=======
|
|
59
|
+
(U, b, alpha): Tuple
|
|
60
|
+
new auxially parameters
|
|
61
|
+
|
|
62
|
+
Notes
|
|
63
|
+
=====
|
|
64
|
+
``blm.stats[0]`` (U) will be mutated while the others not.
|
|
65
|
+
"""
|
|
66
|
+
if psi is None:
|
|
67
|
+
psi = blm.lik.get_basis(x)
|
|
68
|
+
U = blm.stats[0]
|
|
69
|
+
b = blm.stats[1] + (t - blm.lik.linear.bias) * psi
|
|
70
|
+
misc.cholupdate(U, psi * np.sqrt(blm.lik.cov.prec))
|
|
71
|
+
alpha = misc.gauss_elim(U, b)
|
|
72
|
+
return (U, b, alpha)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def sampling(blm, w_mu=None, N=1, alpha=1.0):
|
|
76
|
+
"""
|
|
77
|
+
draws samples of weights
|
|
78
|
+
|
|
79
|
+
Parameters
|
|
80
|
+
==========
|
|
81
|
+
blm: physbo.blm.core.model
|
|
82
|
+
model
|
|
83
|
+
w_mu: numpy.ndarray
|
|
84
|
+
mean of weight
|
|
85
|
+
N: int
|
|
86
|
+
the number of samples
|
|
87
|
+
(default: 1)
|
|
88
|
+
alpha: float
|
|
89
|
+
noise for sampling source
|
|
90
|
+
(default: 1.0)
|
|
91
|
+
|
|
92
|
+
Returns
|
|
93
|
+
=======
|
|
94
|
+
numpy.ndarray
|
|
95
|
+
samples of weights
|
|
96
|
+
"""
|
|
97
|
+
if w_mu is None:
|
|
98
|
+
w_mu = get_post_params_mean(blm)
|
|
99
|
+
if N == 1:
|
|
100
|
+
z = np.random.randn(blm.nbasis) * alpha
|
|
101
|
+
else:
|
|
102
|
+
z = np.random.randn(blm.nbasis, N) * alpha
|
|
103
|
+
|
|
104
|
+
U = blm.stats[0]
|
|
105
|
+
invUz = scipy.linalg.solve_triangular(
|
|
106
|
+
U, z, lower=False, overwrite_b=False, check_finite=False
|
|
107
|
+
)
|
|
108
|
+
return (invUz.transpose() + w_mu).transpose()
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def get_post_params_mean(blm):
|
|
112
|
+
"""
|
|
113
|
+
calculates mean of weight
|
|
114
|
+
|
|
115
|
+
Parameters
|
|
116
|
+
==========
|
|
117
|
+
blm: physbo.blm.core.model
|
|
118
|
+
|
|
119
|
+
Returns
|
|
120
|
+
=======
|
|
121
|
+
numpy.ndarray
|
|
122
|
+
"""
|
|
123
|
+
return blm.stats[2] * blm.lik.cov.prec
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def get_post_fmean(blm, X, Psi=None, w=None):
|
|
127
|
+
"""
|
|
128
|
+
calculates posterior mean of model
|
|
129
|
+
|
|
130
|
+
Parameters
|
|
131
|
+
==========
|
|
132
|
+
blm: physbo.blm.core.model
|
|
133
|
+
X: numpy.ndarray
|
|
134
|
+
inputs
|
|
135
|
+
Psi: numpy.ndarray
|
|
136
|
+
feature maps
|
|
137
|
+
(default: blm.lik.linear.basis.get_basis(X))
|
|
138
|
+
w: numpy.ndarray
|
|
139
|
+
weights
|
|
140
|
+
(default: get_post_params_mean(blm))
|
|
141
|
+
|
|
142
|
+
Returns
|
|
143
|
+
=======
|
|
144
|
+
numpy.ndarray
|
|
145
|
+
"""
|
|
146
|
+
if Psi is None:
|
|
147
|
+
Psi = blm.lik.linear.basis.get_basis(X)
|
|
148
|
+
|
|
149
|
+
if w is None:
|
|
150
|
+
w = get_post_params_mean(blm)
|
|
151
|
+
return Psi.dot(w) + blm.lik.linear.bias
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def get_post_fcov(blm, X, Psi=None, diag=True):
|
|
155
|
+
"""
|
|
156
|
+
calculates posterior covariance of model
|
|
157
|
+
|
|
158
|
+
Parameters
|
|
159
|
+
==========
|
|
160
|
+
blm: physbo.blm.core.model
|
|
161
|
+
X: numpy.ndarray
|
|
162
|
+
inputs
|
|
163
|
+
Psi: numpy.ndarray
|
|
164
|
+
feature maps
|
|
165
|
+
(default: blm.lik.linear.basis.get_basis(X))
|
|
166
|
+
diag: bool
|
|
167
|
+
if True, returns only variances as a diagonal matrix
|
|
168
|
+
(default: True)
|
|
169
|
+
|
|
170
|
+
Returns
|
|
171
|
+
=======
|
|
172
|
+
numpy.ndarray
|
|
173
|
+
"""
|
|
174
|
+
if Psi is None:
|
|
175
|
+
Psi = blm.lik.linear.basis.get_basis(X)
|
|
176
|
+
|
|
177
|
+
U = blm.stats[0]
|
|
178
|
+
R = scipy.linalg.solve_triangular(
|
|
179
|
+
U.transpose(),
|
|
180
|
+
Psi.transpose(),
|
|
181
|
+
lower=True,
|
|
182
|
+
overwrite_b=False,
|
|
183
|
+
check_finite=False,
|
|
184
|
+
)
|
|
185
|
+
RT = R.transpose()
|
|
186
|
+
|
|
187
|
+
if diag is True:
|
|
188
|
+
fcov = misc.diagAB(RT, R)
|
|
189
|
+
else:
|
|
190
|
+
fcov = np.dot(RT, R)
|
|
191
|
+
|
|
192
|
+
return fcov
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
# Copyright (C) 2020- The University of Tokyo
|
|
3
|
+
#
|
|
4
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
5
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
|
+
|
|
8
|
+
from ._src import cov
|
|
9
|
+
from .linear import linear
|
|
10
|
+
from .gauss import gauss
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
# Copyright (C) 2020- The University of Tokyo
|
|
3
|
+
#
|
|
4
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
5
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
|
+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
|
+
|
|
8
|
+
from .cov import cov
|