Skip to content

Commit dbe1acc

Browse files
authored
Merge pull request #306 from nabenabe0928/add-unit-tests-to-var
Add unit tests to value at risk
2 parents 462c534 + a201698 commit dbe1acc

File tree

2 files changed

+853
-0
lines changed

2 files changed

+853
-0
lines changed

package/samplers/value_at_risk/sampler.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,36 @@ def _get_scaled_input_noise_params(
291291
else:
292292
assert False, "Should not reach here."
293293

294+
def _verify_search_space(self, search_space: dict[str, BaseDistribution]) -> None:
295+
noisy_param_cands = [
296+
k
297+
for k, v in search_space.items()
298+
if isinstance(v, optuna.distributions.FloatDistribution)
299+
and v.step is None
300+
and not v.log
301+
]
302+
noise_param_names: list[str]
303+
if self._uniform_input_noise_ranges is not None:
304+
noise_param_names = list(self._uniform_input_noise_ranges.keys())
305+
elif self._normal_input_noise_stdevs is not None:
306+
noise_param_names = list(self._normal_input_noise_stdevs.keys())
307+
else:
308+
assert "Should not reach here."
309+
310+
if len(set(noisy_param_cands) & set(noise_param_names)) == 0:
311+
raise ValueError(
312+
"RobustGPSampler needs at least one noisy parameter defined in the search space. "
313+
f"However, no noisy parameters are found. Noisy parameter candidates are "
314+
f"{noisy_param_cands}, but input noise is specified for {noise_param_names}."
315+
)
316+
294317
def sample_relative(
295318
self, study: Study, trial: FrozenTrial, search_space: dict[str, BaseDistribution]
296319
) -> dict[str, Any]:
297320
if search_space == {}:
298321
return {}
299322

323+
self._verify_search_space(search_space)
300324
states = (TrialState.COMPLETE,)
301325
trials = study._get_trials(deepcopy=False, states=states, use_cache=True)
302326

0 commit comments

Comments
 (0)