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.
Files changed (61) hide show
  1. physbo/__init__.py +17 -0
  2. physbo/blm/__init__.py +17 -0
  3. physbo/blm/basis/__init__.py +8 -0
  4. physbo/blm/basis/fourier.py +148 -0
  5. physbo/blm/core/__init__.py +8 -0
  6. physbo/blm/core/model.py +257 -0
  7. physbo/blm/inf/__init__.py +8 -0
  8. physbo/blm/inf/exact.py +192 -0
  9. physbo/blm/lik/__init__.py +10 -0
  10. physbo/blm/lik/_src/__init__.py +8 -0
  11. physbo/blm/lik/_src/cov.py +113 -0
  12. physbo/blm/lik/gauss.py +136 -0
  13. physbo/blm/lik/linear.py +117 -0
  14. physbo/blm/predictor.py +238 -0
  15. physbo/blm/prior/__init__.py +8 -0
  16. physbo/blm/prior/gauss.py +215 -0
  17. physbo/gp/__init__.py +15 -0
  18. physbo/gp/core/__init__.py +11 -0
  19. physbo/gp/core/learning.py +364 -0
  20. physbo/gp/core/model.py +420 -0
  21. physbo/gp/core/prior.py +207 -0
  22. physbo/gp/cov/__init__.py +8 -0
  23. physbo/gp/cov/_src/__init__.py +1 -0
  24. physbo/gp/cov/_src/enhance_gauss.cpython-310-darwin.so +0 -0
  25. physbo/gp/cov/gauss.py +393 -0
  26. physbo/gp/inf/__init__.py +8 -0
  27. physbo/gp/inf/exact.py +231 -0
  28. physbo/gp/lik/__init__.py +8 -0
  29. physbo/gp/lik/gauss.py +179 -0
  30. physbo/gp/mean/__init__.py +9 -0
  31. physbo/gp/mean/const.py +150 -0
  32. physbo/gp/mean/zero.py +66 -0
  33. physbo/gp/predictor.py +170 -0
  34. physbo/misc/__init__.py +15 -0
  35. physbo/misc/_src/__init__.py +1 -0
  36. physbo/misc/_src/cholupdate.cpython-310-darwin.so +0 -0
  37. physbo/misc/_src/diagAB.cpython-310-darwin.so +0 -0
  38. physbo/misc/_src/logsumexp.cpython-310-darwin.so +0 -0
  39. physbo/misc/_src/traceAB.cpython-310-darwin.so +0 -0
  40. physbo/misc/centering.py +28 -0
  41. physbo/misc/gauss_elim.py +35 -0
  42. physbo/misc/set_config.py +299 -0
  43. physbo/opt/__init__.py +8 -0
  44. physbo/opt/adam.py +107 -0
  45. physbo/predictor.py +261 -0
  46. physbo/search/__init__.py +11 -0
  47. physbo/search/discrete/__init__.py +11 -0
  48. physbo/search/discrete/policy.py +804 -0
  49. physbo/search/discrete/results.py +192 -0
  50. physbo/search/discrete_multi/__init__.py +11 -0
  51. physbo/search/discrete_multi/policy.py +552 -0
  52. physbo/search/discrete_multi/results.py +128 -0
  53. physbo/search/pareto.py +206 -0
  54. physbo/search/score.py +155 -0
  55. physbo/search/score_multi.py +197 -0
  56. physbo/search/utility.py +101 -0
  57. physbo/variable.py +222 -0
  58. physbo-2.0.0.dist-info/METADATA +110 -0
  59. physbo-2.0.0.dist-info/RECORD +61 -0
  60. physbo-2.0.0.dist-info/WHEEL +5 -0
  61. physbo-2.0.0.dist-info/top_level.txt +1 -0
physbo/gp/lik/gauss.py ADDED
@@ -0,0 +1,179 @@
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
+
10
+
11
+ class gauss:
12
+ """Gaussian likelihood function"""
13
+
14
+ def __init__(self, std=1, max_params=1e6, min_params=1e-6):
15
+ """
16
+
17
+ Parameters
18
+ ----------
19
+ std: numpy.ndarray or float
20
+ standard deviation.
21
+ max_params: float
22
+ The maximum value of the parameter.
23
+ If the parameter is greater than this value, it will be replaced by this value.
24
+ min_params: float
25
+ The minimum value of the parameter.
26
+ If the parameter is less than this value, it will be replaced by this value.
27
+ """
28
+ self.min_params = np.log(min_params)
29
+ self.max_params = np.log(max_params)
30
+ self.num_params = 1
31
+ self.std = std
32
+ self.params = np.log(std)
33
+ self.set_params(self.params)
34
+
35
+ def supp_params(self, params=None):
36
+ """
37
+ Set maximum (minimum) values for parameters when the parameter is greater(less) than this value.
38
+
39
+ Parameters
40
+ ----------
41
+ params: numpy.ndarray
42
+ Parameters for optimization.
43
+ Array of real elements of size (n,), where ‘n’ is the number of independent variables.
44
+ Returns
45
+ -------
46
+
47
+ """
48
+ if params is None:
49
+ params = np.copy(params)
50
+
51
+ if params > self.max_params:
52
+ params = self.max_params
53
+
54
+ if params < self.min_params:
55
+ params = self.min_params
56
+
57
+ return params
58
+
59
+ def trans_params(self, params=None):
60
+ """
61
+ Get exp[params].
62
+
63
+ Parameters
64
+ ----------
65
+ params: numpy.ndarray
66
+ Parameters for optimization.
67
+ Array of real elements of size (n,), where ‘n’ is the number of independent variables.
68
+
69
+ Returns
70
+ -------
71
+ std: numpy.ndarray
72
+ """
73
+ if params is None:
74
+ params = np.copy(self.params)
75
+
76
+ std = np.exp(params)
77
+ return std
78
+
79
+ def get_params_bound(self):
80
+ """
81
+ Get boundary array.
82
+
83
+ Returns
84
+ -------
85
+ bound: list
86
+ A num_params-dimensional array with the tuple (min_params, max_params).
87
+ """
88
+ bound = [(self.min_params, self.max_params) for i in range(0, self.num_params)]
89
+ return bound
90
+
91
+ def get_cov(self, num_data, params=None):
92
+ """
93
+ Get a covariance matrix
94
+
95
+ Parameters
96
+ ----------
97
+ num_data: int
98
+ params: numpy.ndarray
99
+ Parameters for optimization.
100
+ Array of real elements of size (n,), where ‘n’ is the number of independent variables.
101
+
102
+ Returns
103
+ -------
104
+ numpy.ndarray
105
+ Diagonal element matrix of exp(2.0*params)
106
+ """
107
+ std = self.trans_params(params)
108
+ var = std**2
109
+ return var * np.identity(num_data)
110
+
111
+ def get_grad(self, num_data, params=None):
112
+ """
113
+ Get a gradient matrix
114
+
115
+ Parameters
116
+ ----------
117
+ num_data: int
118
+ params: numpy.ndarray
119
+ Parameters for optimization.
120
+ Array of real elements of size (n,), where ‘n’ is the number of independent variables.
121
+
122
+ Returns
123
+ -------
124
+ numpy.ndarray
125
+ Diagonal element matrix of 2.0 * exp(2.0*params)
126
+ """
127
+ std = self.trans_params(params)
128
+ var = std**2
129
+ return var * np.identity(num_data) * 2
130
+
131
+ def set_params(self, params):
132
+ """
133
+ Set parameters.
134
+
135
+ Parameters
136
+ ----------
137
+ params: numpy.ndarray
138
+ Parameters for optimization.
139
+ Array of real elements of size (n,), where ‘n’ is the number of independent variables.
140
+
141
+ Returns
142
+ -------
143
+
144
+ """
145
+ self.params = self.supp_params(params)
146
+ self.std = self.trans_params(params)
147
+
148
+ def get_cand_params(self, t):
149
+ """
150
+ Getting candidate parameters.
151
+
152
+ Parameters
153
+ ----------
154
+ t: numpy.ndarray
155
+ N dimensional array. The negative energy of each search candidate (value of the objective function to be optimized).
156
+
157
+ Returns
158
+ -------
159
+ numpy.ndarray
160
+ log[ standard deviation of t] - log 10.0
161
+ """
162
+ return np.log(np.std(t) / 10)
163
+
164
+ # [TODO] Check: This function seems not to be used.
165
+ def sampling(self, fmean):
166
+ """
167
+ Sampling by adding noise
168
+
169
+ Parameters
170
+ ----------
171
+ fmean: numpy.ndarray
172
+
173
+ Returns
174
+ -------
175
+
176
+ """
177
+ num_data = fmean.shape[0]
178
+ eps = self.std * np.random.randn(num_data)
179
+ return fmean + eps
@@ -0,0 +1,9 @@
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 .zero import zero
9
+ from .const import const
@@ -0,0 +1,150 @@
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
+
10
+
11
+ class const:
12
+ """constant"""
13
+
14
+ def __init__(self, params=None, max_params=1e12, min_params=-1e12):
15
+ """
16
+
17
+ Parameters
18
+ ----------
19
+ params: numpy.ndarray
20
+ Parameters
21
+ max_params: float
22
+ Threshold value for specifying the maximum value of the parameter
23
+ min_params: float
24
+ Threshold value for specifying the minimum value of the parameter
25
+
26
+ """
27
+ self.max_params = max_params
28
+ self.min_params = min_params
29
+ self.init_params(params)
30
+ self.num_params = 1
31
+
32
+ def supp_params(self, params):
33
+ """
34
+ Setting maximum and minimum value of parameters.
35
+
36
+ Parameters
37
+ ----------
38
+ params: numpy.ndarray
39
+ parameters
40
+
41
+ Returns
42
+ -------
43
+ numpy.ndarray
44
+ """
45
+ if params > self.max_params:
46
+ params = self.max_params
47
+
48
+ if params < self.min_params:
49
+ params = self.min_params
50
+
51
+ return params
52
+
53
+ def get_params_bound(self):
54
+ """
55
+ Getting the boundary list for parameters
56
+
57
+ Returns
58
+ -------
59
+ bound: list
60
+ num_params array with the tupple (min_param, max_params)
61
+
62
+ """
63
+ bound = [(self.min_params, self.max_params) for i in range(0, self.num_params)]
64
+ return bound
65
+
66
+ def get_mean(self, num_data, params=None):
67
+ """
68
+
69
+ Parameters
70
+ ----------
71
+ num_data: int
72
+ total number of data
73
+ params: numpy.ndarray
74
+ parameters
75
+
76
+ Returns
77
+ -------
78
+ numpy.ndarray
79
+ """
80
+ if params is None:
81
+ params = np.copy(self.params)
82
+ return params * np.ones(num_data)
83
+
84
+ def get_grad(self, num_data, params=None):
85
+ """
86
+ Returning a new array of (num_data), filled with ones.
87
+
88
+ Parameters
89
+ ----------
90
+ num_data: int
91
+ total number of data
92
+ params: object
93
+ not used
94
+
95
+ Returns
96
+ -------
97
+ numpy.ndarray
98
+ """
99
+ return np.ones(num_data)
100
+
101
+ def set_params(self, params):
102
+ """
103
+ Setting parameters defined in const class.
104
+
105
+ Parameters
106
+ ----------
107
+ params: numpy.ndarray
108
+ parameters
109
+
110
+ Returns
111
+ -------
112
+ numpy.ndarray
113
+ """
114
+ self.params = params
115
+
116
+ def init_params(self, params):
117
+ """
118
+ Initializing parameters
119
+
120
+ Parameters
121
+ ----------
122
+ params: numpy.ndarray
123
+ parameters
124
+
125
+ Returns
126
+ -------
127
+ params: numpy.ndarray
128
+ initialized parameters
129
+ """
130
+ if params is None:
131
+ self.params = 0
132
+ else:
133
+ self.params = self.supp_params(params)
134
+
135
+ def get_cand_params(self, t):
136
+ """
137
+ Getting the median array of candidates.
138
+
139
+ Parameters
140
+ ----------
141
+ t: array_like
142
+ Input array or object that can be converted to an array
143
+
144
+ Returns
145
+ -------
146
+ median: numpy.ndarray
147
+ A new array holding the result.
148
+
149
+ """
150
+ return np.median(t)
physbo/gp/mean/zero.py ADDED
@@ -0,0 +1,66 @@
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
+
10
+
11
+ class zero:
12
+ """zero"""
13
+
14
+ def __init__(self):
15
+ self.num_params = 0
16
+ self.params = np.array([])
17
+
18
+ def get_mean(self, num_data, params=None):
19
+ """
20
+ Returning numpy.zeros(num_data)
21
+
22
+ Parameters
23
+ ----------
24
+ num_data: int
25
+ dimension of numpy.zeros
26
+ params: object
27
+ not used
28
+
29
+ Returns
30
+ -------
31
+ numpy.ndarray
32
+
33
+ """
34
+ return np.zeros(num_data)
35
+
36
+ def get_grad(self, num_data, params=None):
37
+ """
38
+ Returning empty numpy.ndarray
39
+
40
+ Parameters
41
+ ----------
42
+ num_data: int
43
+ not used
44
+ params: object
45
+ not used
46
+
47
+ Returns
48
+ -------
49
+ numpy.ndarray
50
+
51
+ """
52
+ return np.array([])
53
+
54
+ def set_params(self, params):
55
+ """
56
+ Not defined
57
+
58
+ Parameters
59
+ ----------
60
+ params
61
+
62
+ Returns
63
+ -------
64
+
65
+ """
66
+ pass
physbo/gp/predictor.py ADDED
@@ -0,0 +1,170 @@
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 physbo.predictor
9
+
10
+
11
+ class predictor(physbo.predictor.base_predictor):
12
+ """predictor"""
13
+
14
+ def __init__(self, config, model=None):
15
+ """
16
+
17
+ Parameters
18
+ ----------
19
+ config: physbo.misc.set_config
20
+ configuration
21
+ model: physbo.gp.core.model
22
+ """
23
+ super(predictor, self).__init__(config, model)
24
+
25
+ def fit(self, training, num_basis=None):
26
+ """
27
+ Fitting model to training dataset
28
+
29
+ Parameters
30
+ ----------
31
+ training: physbo.variable
32
+ dataset for training
33
+ num_basis: int
34
+ the number of basis (default: self.config.predict.num_basis)
35
+ """
36
+ if self.model.prior.cov.num_dim is None:
37
+ self.model.prior.cov.num_dim = training.X.shape[1]
38
+ self.model.fit(training.X, training.t, self.config)
39
+ self.delete_stats()
40
+
41
+ def get_basis(self, *args, **kwds):
42
+ """
43
+
44
+ Parameters
45
+ ----------
46
+ args
47
+ kwds
48
+
49
+ Returns
50
+ -------
51
+
52
+ """
53
+ pass
54
+
55
+ def get_post_params(self, *args, **kwds):
56
+ """
57
+
58
+ Parameters
59
+ ----------
60
+ args
61
+ kwds
62
+
63
+ Returns
64
+ -------
65
+
66
+ """
67
+ pass
68
+
69
+ def update(self, training, test):
70
+ self.prepare(training)
71
+
72
+ def prepare(self, training):
73
+ """
74
+ Initializing model by using training data set
75
+
76
+ Parameters
77
+ ----------
78
+ training: physbo.variable
79
+ dataset for training
80
+
81
+ """
82
+ self.model.prepare(training.X, training.t)
83
+
84
+ def delete_stats(self):
85
+ self.model.stats = None
86
+
87
+ def get_post_fmean(self, training, test):
88
+ """
89
+ Calculating posterior mean value of model
90
+
91
+ Parameters
92
+ ----------
93
+ training: physbo.variable
94
+ training dataset. If already trained, the model does not use this.
95
+ test: physbo.variable
96
+ inputs
97
+
98
+ Returns
99
+ -------
100
+ numpy.ndarray
101
+
102
+ """
103
+ if self.model.stats is None:
104
+ self.prepare(training)
105
+ return self.model.get_post_fmean(training.X, test.X)
106
+
107
+ def get_post_fcov(self, training, test, diag=True):
108
+ """
109
+ Calculating posterior variance-covariance matrix of model
110
+
111
+ Parameters
112
+ ----------
113
+ training: physbo.variable
114
+ training dataset. If already trained, the model does not use this.
115
+ test: physbo.variable
116
+ inputs
117
+ diag: bool
118
+ Diagonlization flag in physbo.exact.get_post_fcov function.
119
+ Returns
120
+ -------
121
+ numpy.ndarray
122
+
123
+ """
124
+ if self.model.stats is None:
125
+ self.prepare(training)
126
+ return self.model.get_post_fcov(training.X, test.X, diag=diag)
127
+
128
+ def get_post_samples(self, training, test, alpha=1):
129
+ """
130
+ Drawing samples of mean values of model
131
+
132
+ Parameters
133
+ ----------
134
+ training: physbo.variable
135
+ training dataset. If already trained, the model does not use this.
136
+ test: physbo.variable
137
+ inputs (not used)
138
+ alpha: float
139
+ tuning parameter of the covariance by multiplying alpha**2 for np.random.multivariate_normal.
140
+ Returns
141
+ -------
142
+ numpy.ndarray
143
+
144
+ """
145
+ if self.model.stats is None:
146
+ self.prepare(training)
147
+ return self.model.post_sampling(training.X, test.X, alpha=alpha)
148
+
149
+ def get_predict_samples(self, training, test, N=1):
150
+ """
151
+ Drawing samples of values of model
152
+
153
+ Parameters
154
+ ----------
155
+ training: physbo.variable
156
+ training dataset. If already trained, the model does not use this.
157
+ test: physbo.variable
158
+ inputs
159
+ N: int
160
+ number of samples
161
+ (default: 1)
162
+
163
+ Returns
164
+ -------
165
+ numpy.ndarray (N x len(test))
166
+
167
+ """
168
+ if self.model.stats is None:
169
+ self.prepare(training)
170
+ return self.model.predict_sampling(training.X, test.X, N=N)
@@ -0,0 +1,15 @@
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 .centering import centering
9
+ from .gauss_elim import gauss_elim
10
+ from .set_config import set_config
11
+ from ._src.diagAB import diagAB_64 as diagAB
12
+ from ._src.traceAB import traceAB2_64 as traceAB2
13
+ from ._src.traceAB import traceAB3_64 as traceAB3
14
+ from ._src.cholupdate import cholupdate64 as cholupdate
15
+ from ._src.logsumexp import logsumexp64
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,28 @@
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
+
10
+
11
+ def centering(X):
12
+ """
13
+ Normalize the mean and standard deviation along the each column of X to 0 and 1, respectively
14
+
15
+ Parameters
16
+ ----------
17
+ X: numpy array
18
+ N x d dimensional matrix. Each row of X denotes the d-dimensional feature vector of search candidate.
19
+
20
+ Returns
21
+ -------
22
+ X_normalized: numpy array
23
+ normalized N x d dimensional matrix.
24
+ """
25
+ stdX = np.std(X, 0)
26
+ index = np.where(stdX != 0)
27
+ X_normalized = (X[:, index[0]] - np.mean(X[:, index[0]], 0)) / stdX[index[0]]
28
+ return X_normalized
@@ -0,0 +1,35 @@
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
+
12
+ def gauss_elim(U, t):
13
+ """
14
+ Calculate alpha using scipy.linalg.solve_triangular.
15
+ alpha = (U^T U)^-1 t = U^-1 [(U^T)-1 t]
16
+
17
+ Parameters
18
+ ----------
19
+ U: (M, M) array_like
20
+ A triangular matrix
21
+ t: (M,) or (M, N) array_like
22
+
23
+ Returns
24
+ -------
25
+ alpha: numpy.ndarray
26
+ Solution to the system L^T alpha = t. Shape of return matches t.
27
+ """
28
+ alpha = scipy.linalg.solve_triangular(
29
+ U.transpose(), t, lower=True, overwrite_b=False, check_finite=False
30
+ )
31
+
32
+ alpha = scipy.linalg.solve_triangular(
33
+ U, alpha, lower=False, overwrite_b=False, check_finite=False
34
+ )
35
+ return alpha