Skip to content

Commit d3c561a

Browse files
0.24.7
theoretical variance
1 parent 2052d3d commit d3c561a

6 files changed

Lines changed: 257 additions & 62 deletions

File tree

notebooks/00_spotPython_tests.ipynb

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8454,6 +8454,162 @@
84548454
"print(f\"X0: {X0}\")"
84558455
]
84568456
},
8457+
{
8458+
"cell_type": "markdown",
8459+
"metadata": {},
8460+
"source": [
8461+
"## aggregate_mean_var()"
8462+
]
8463+
},
8464+
{
8465+
"cell_type": "code",
8466+
"execution_count": 1,
8467+
"metadata": {},
8468+
"outputs": [
8469+
{
8470+
"name": "stdout",
8471+
"output_type": "stream",
8472+
"text": [
8473+
"[[1 2]\n",
8474+
" [3 4]]\n",
8475+
"[2. 2.]\n",
8476+
"[1. 0.]\n"
8477+
]
8478+
}
8479+
],
8480+
"source": [
8481+
"from spotpython.utils.aggregate import aggregate_mean_var\n",
8482+
"import numpy as np\n",
8483+
"X = np.array([[1, 2], [3, 4], [1, 2]])\n",
8484+
"y = np.array([1, 2, 3])\n",
8485+
"X_agg, y_mean, y_var = aggregate_mean_var(X, y)\n",
8486+
"print(X_agg)\n",
8487+
"print(y_mean)\n",
8488+
"print(y_var)"
8489+
]
8490+
},
8491+
{
8492+
"cell_type": "code",
8493+
"execution_count": 2,
8494+
"metadata": {},
8495+
"outputs": [
8496+
{
8497+
"name": "stdout",
8498+
"output_type": "stream",
8499+
"text": [
8500+
"[[1 2]\n",
8501+
" [3 4]]\n",
8502+
"[3. 3.]\n",
8503+
"[2.66666667 1. ]\n"
8504+
]
8505+
}
8506+
],
8507+
"source": [
8508+
"X = np.array([[1, 2], [3, 4], [1, 2], [3, 4], [1,2]])\n",
8509+
"y = np.array([1, 2, 3, 4, 5])\n",
8510+
"X_agg, y_mean, y_var = aggregate_mean_var(X, y)\n",
8511+
"print(X_agg)\n",
8512+
"print(y_mean)\n",
8513+
"print(y_var)"
8514+
]
8515+
},
8516+
{
8517+
"cell_type": "code",
8518+
"execution_count": 3,
8519+
"metadata": {},
8520+
"outputs": [
8521+
{
8522+
"name": "stdout",
8523+
"output_type": "stream",
8524+
"text": [
8525+
"[[1. 1. 1.]\n",
8526+
" [1. 1. 1.]\n",
8527+
" [2. 2. 2.]\n",
8528+
" [2. 2. 2.]\n",
8529+
" [1. 1. 1.]\n",
8530+
" [1. 1. 1.]]\n",
8531+
"[3. 3. 6. 6. 6. 6.]\n",
8532+
"(array([[1., 1., 1.],\n",
8533+
" [2., 2., 2.]]), array([4.5, 6. ]), array([3., 0.]))\n"
8534+
]
8535+
}
8536+
],
8537+
"source": [
8538+
"X_1 = np.ones((2, 3))\n",
8539+
"y_1 = np.sum(X_1, axis=1)\n",
8540+
"y_2 = 2 * y_1\n",
8541+
"X_2 = np.append(X_1, 2 * X_1, axis=0)\n",
8542+
"X = np.append(X_2, X_1, axis=0)\n",
8543+
"y = np.append(y_1, y_2, axis=0)\n",
8544+
"y = np.append(y, y_2, axis=0)\n",
8545+
"print(X)\n",
8546+
"print(y)\n",
8547+
"Z = aggregate_mean_var(X, y, var_empirical=True)\n",
8548+
"print(Z)"
8549+
]
8550+
},
8551+
{
8552+
"cell_type": "code",
8553+
"execution_count": 4,
8554+
"metadata": {},
8555+
"outputs": [
8556+
{
8557+
"name": "stdout",
8558+
"output_type": "stream",
8559+
"text": [
8560+
"[[1. 1. 1.]\n",
8561+
" [1. 1. 1.]\n",
8562+
" [2. 2. 2.]\n",
8563+
" [2. 2. 2.]\n",
8564+
" [1. 1. 1.]\n",
8565+
" [1. 1. 1.]]\n",
8566+
"[3. 3. 6. 6. 6. 6.]\n",
8567+
"(array([[1., 1., 1.],\n",
8568+
" [2., 2., 2.]]), array([4.5, 6. ]), array([2.25, 0. ]))\n"
8569+
]
8570+
}
8571+
],
8572+
"source": [
8573+
"X_1 = np.ones((2, 3))\n",
8574+
"y_1 = np.sum(X_1, axis=1)\n",
8575+
"y_2 = 2 * y_1\n",
8576+
"X_2 = np.append(X_1, 2 * X_1, axis=0)\n",
8577+
"X = np.append(X_2, X_1, axis=0)\n",
8578+
"y = np.append(y_1, y_2, axis=0)\n",
8579+
"y = np.append(y, y_2, axis=0)\n",
8580+
"print(X)\n",
8581+
"print(y)\n",
8582+
"Z = aggregate_mean_var(X, y, var_empirical=False)\n",
8583+
"print(Z)"
8584+
]
8585+
},
8586+
{
8587+
"cell_type": "code",
8588+
"execution_count": 5,
8589+
"metadata": {},
8590+
"outputs": [
8591+
{
8592+
"name": "stdout",
8593+
"output_type": "stream",
8594+
"text": [
8595+
"[[1 2]\n",
8596+
" [3 4]]\n",
8597+
"[2. 2.]\n",
8598+
"[ 2. nan]\n"
8599+
]
8600+
}
8601+
],
8602+
"source": [
8603+
"from spotpython.utils.aggregate import aggregate_mean_var\n",
8604+
"import numpy as np\n",
8605+
"X = np.array([[1, 2], [3, 4], [1, 2]])\n",
8606+
"y = np.array([1, 2, 3])\n",
8607+
"X_agg, y_mean, y_var = aggregate_mean_var(X, y, var_empirical=True)\n",
8608+
"print(X_agg)\n",
8609+
"print(y_mean)\n",
8610+
"print(y_var)"
8611+
]
8612+
},
84578613
{
84588614
"cell_type": "code",
84598615
"execution_count": null,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotpython"
10-
version = "0.24.6"
10+
version = "0.24.7"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotpython/spot/spot.py

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def __init__(
235235
# because the writer is passed to the surrogate model:
236236
self.init_spot_writer()
237237

238-
self._surrogate_update(surrogate)
238+
self._surrogate_setup(surrogate)
239239

240240
if self.fun_control.get("save_experiment"):
241241
self.save_experiment(verbosity=self.verbosity)
@@ -392,7 +392,7 @@ def _surrogate_control_setup(self) -> None:
392392
if self.surrogate_control["n_theta"] > 1:
393393
self.surrogate_control.update({"n_theta": self.k})
394394

395-
def _surrogate_update(self, surrogate) -> None:
395+
def _surrogate_setup(self, surrogate) -> None:
396396
# Surrogate related information:
397397
self.surrogate = surrogate
398398
# If no surrogate model is specified, use the internal
@@ -950,7 +950,7 @@ def initialize_design(self, X_start=None) -> None:
950950

951951
self.evaluate_initial_design()
952952

953-
self.write_tensorboard_log()
953+
self.write_initial_tensorboard_log()
954954

955955
def initialize_design_matrix(self, X_start=None) -> None:
956956
"""
@@ -1098,7 +1098,7 @@ def evaluate_initial_design(self) -> None:
10981098
logger.debug("In Spot() evaluate_initial_design(), final X val, after remove nan: self.X: %s", self.X)
10991099
logger.debug("In Spot() evaluate_initial_design(), final y val, after remove nan: self.y: %s", self.y)
11001100

1101-
def write_tensorboard_log(self) -> None:
1101+
def write_initial_tensorboard_log(self) -> None:
11021102
"""Writes initial design data using the spot_writer. The spot_writer
11031103
is a tensorboard writer that writes the data to a tensorboard file.
11041104
@@ -1116,7 +1116,7 @@ def write_tensorboard_log(self) -> None:
11161116
fun_control=fun_control,
11171117
)
11181118
S.initialize_design()
1119-
S.write_tensorboard_log()
1119+
S.write_initial_tensorboard_log()
11201120
Moving TENSORBOARD_PATH: runs/ to TENSORBOARD_PATH_OLD: runs_OLD/runs_2025_01_12_09_24_15
11211121
Created spot_tensorboard_path: runs/spot_logs/00_p040025_2025-01-12_09-24-15 for SummaryWriter()
11221122
"""
@@ -1135,6 +1135,46 @@ def write_tensorboard_log(self) -> None:
11351135
self.spot_writer.add_hparams(config, {"hp_metric": y_j})
11361136
self.spot_writer.flush()
11371137

1138+
def update_stats(self) -> None:
1139+
"""
1140+
Update the following stats: 1. `min_y` 2. `min_X` 3. `counter`
1141+
If `noise` is `True`, additionally the following stats are computed: 1. `mean_X`
1142+
2. `mean_y` 3. `min_mean_y` 4. `min_mean_X`.
1143+
1144+
Args:
1145+
self (object): Spot object
1146+
1147+
Returns:
1148+
(NoneType): None
1149+
1150+
Attributes:
1151+
self.min_y (float): minimum y value
1152+
self.min_X (numpy.ndarray): X value of the minimum y value
1153+
self.counter (int): number of function evaluations
1154+
self.mean_X (numpy.ndarray): mean X values
1155+
self.mean_y (numpy.ndarray): mean y values
1156+
self.var_y (numpy.ndarray): variance of y values
1157+
self.min_mean_y (float): minimum mean y value
1158+
self.min_mean_X (numpy.ndarray): X value of the minimum mean y value
1159+
1160+
"""
1161+
self.min_y = min(self.y)
1162+
self.min_X = self.X[argmin(self.y)]
1163+
self.counter = self.y.size
1164+
self.fun_control.update({"counter": self.counter})
1165+
# Update aggregated x and y values (if noise):
1166+
if self.noise:
1167+
Z = aggregate_mean_var(X=self.X, y=self.y)
1168+
self.mean_X = Z[0]
1169+
self.mean_y = Z[1]
1170+
self.var_y = Z[2]
1171+
# X value of the best mean y value so far:
1172+
self.min_mean_X = self.mean_X[argmin(self.mean_y)]
1173+
# variance of the best mean y value so far:
1174+
self.min_var_y = self.var_y[argmin(self.mean_y)]
1175+
# best mean y value so far:
1176+
self.min_mean_y = self.mean_y[argmin(self.mean_y)]
1177+
11381178
def save_result(self, filename=None, path=None, overwrite=True, verbosity=0) -> None:
11391179
"""
11401180
Save the results to a file.
@@ -1614,46 +1654,6 @@ def generate_design(self, size, repeats, lower, upper) -> np.array:
16141654
"""
16151655
return self.design.scipy_lhd(n=size, repeats=repeats, lower=lower, upper=upper)
16161656

1617-
def update_stats(self) -> None:
1618-
"""
1619-
Update the following stats: 1. `min_y` 2. `min_X` 3. `counter`
1620-
If `noise` is `True`, additionally the following stats are computed: 1. `mean_X`
1621-
2. `mean_y` 3. `min_mean_y` 4. `min_mean_X`.
1622-
1623-
Args:
1624-
self (object): Spot object
1625-
1626-
Returns:
1627-
(NoneType): None
1628-
1629-
Attributes:
1630-
self.min_y (float): minimum y value
1631-
self.min_X (numpy.ndarray): X value of the minimum y value
1632-
self.counter (int): number of function evaluations
1633-
self.mean_X (numpy.ndarray): mean X values
1634-
self.mean_y (numpy.ndarray): mean y values
1635-
self.var_y (numpy.ndarray): variance of y values
1636-
self.min_mean_y (float): minimum mean y value
1637-
self.min_mean_X (numpy.ndarray): X value of the minimum mean y value
1638-
1639-
"""
1640-
self.min_y = min(self.y)
1641-
self.min_X = self.X[argmin(self.y)]
1642-
self.counter = self.y.size
1643-
self.fun_control.update({"counter": self.counter})
1644-
# Update aggregated x and y values (if noise):
1645-
if self.noise:
1646-
Z = aggregate_mean_var(X=self.X, y=self.y)
1647-
self.mean_X = Z[0]
1648-
self.mean_y = Z[1]
1649-
self.var_y = Z[2]
1650-
# X value of the best mean y value so far:
1651-
self.min_mean_X = self.mean_X[argmin(self.mean_y)]
1652-
# variance of the best mean y value so far:
1653-
self.min_var_y = self.var_y[argmin(self.mean_y)]
1654-
# best mean y value so far:
1655-
self.min_mean_y = self.mean_y[argmin(self.mean_y)]
1656-
16571657
def update_writer(self) -> None:
16581658
if hasattr(self, "spot_writer") and self.spot_writer is not None:
16591659
# get the last y value:

0 commit comments

Comments
 (0)