Skip to content

Commit

Permalink
Revert "Add functionality to Region class (#444)"
Browse files Browse the repository at this point in the history
This reverts commit 0dcbc52.
  • Loading branch information
timmens authored Mar 13, 2023
1 parent 0dcbc52 commit 418149e
Show file tree
Hide file tree
Showing 21 changed files with 195 additions and 369 deletions.
27 changes: 0 additions & 27 deletions src/estimagic/optimization/tranquilo/bounds.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/estimagic/optimization/tranquilo/estimate_variance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from estimagic.optimization.tranquilo.get_component import get_component
from estimagic.optimization.tranquilo.new_history import History
from estimagic.optimization.tranquilo.region import Region
from estimagic.optimization.tranquilo.options import Region


def get_variance_estimator(fitter, user_options):
Expand Down
28 changes: 17 additions & 11 deletions src/estimagic/optimization/tranquilo/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import numpy as np

from estimagic.optimization.tranquilo.region import Region
from estimagic.optimization.tranquilo.sample_points import get_sampler
from estimagic.optimization.tranquilo.options import Region
from estimagic.optimization.tranquilo.sample_points import (
_get_effective_bounds,
_map_from_feasible_trustregion,
get_sampler,
)


def get_geometry_checker_pair(
Expand All @@ -22,7 +26,7 @@ def get_geometry_checker_pair(
samples drawn inside a box or a ball, respectively.
n_params (int): Number of parameters.
n_simulations (int): Number of simulations for the mean calculation.
bounds (Bounds): The parameter bounds. See module bounds.py.
bounds (Bounds): The parameter bounds.
Returns:
callable: The sample quality calculator.
Expand All @@ -41,7 +45,7 @@ def get_geometry_checker_pair(

_checker = built_in_checker[checker]

quality_calculator = _checker["quality_calculator"]
quality_calculator = partial(_checker["quality_calculator"], bounds=bounds)
cutoff_simulator = partial(
_checker["cutoff_simulator"],
reference_sampler=reference_sampler,
Expand All @@ -62,26 +66,26 @@ def log_d_cutoff_simulator(
rng (np.random.Generator): The random number generator.
reference_sampler (str): Either "box" or "ball", corresponding to comparison
samples drawn inside a box or a ball, respectively.
bounds (Bounds): The parameter bounds. See module bounds.py.
bounds (Bounds): The parameter bounds.
n_params (int): Dimensionality of the sample.
n_simulations (int): Number of simulations for the mean calculation.
Returns:
float: The simulated mean logarithm of the d-optimality criterion.
"""
_sampler = get_sampler(reference_sampler)
trustregion = Region(center=np.zeros(n_params), radius=1.0, bounds=bounds)
_sampler = get_sampler(reference_sampler, bounds)
trustregion = Region(center=np.zeros(n_params), radius=1, shape=None)
sampler = partial(_sampler, trustregion=trustregion)
raw = []
for _ in range(n_simulations):
x = sampler(n_points=n_samples, rng=rng)
raw.append(log_d_quality_calculator(x, trustregion))
raw.append(log_d_quality_calculator(x, trustregion, bounds))
out = np.nanmean(raw)
return out


def log_d_quality_calculator(sample, trustregion):
def log_d_quality_calculator(sample, trustregion, bounds):
"""Logarithm of the d-optimality criterion.
For a data sample x the log_d_criterion is defined as log(det(x.T @ x)). If the
Expand All @@ -90,13 +94,15 @@ def log_d_quality_calculator(sample, trustregion):
Args:
sample (np.ndarray): The data sample, shape = (n, p).
trustregion (Region): Trustregion. See module region.py.
trustregion (TrustRegion): NamedTuple with attributes center and radius.
bounds (Bounds): The parameter bounds.
Returns:
np.ndarray: The criterion values, shape = (n, ).
"""
points = trustregion.map_to_unit(sample)
effective_bounds = _get_effective_bounds(trustregion, bounds)
points = _map_from_feasible_trustregion(sample, effective_bounds)
n_samples, n_params = points.shape
xtx = points.T @ points
det = np.linalg.det(xtx / n_samples)
Expand Down
2 changes: 1 addition & 1 deletion src/estimagic/optimization/tranquilo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from numba import njit

from estimagic.optimization.tranquilo.region import Region
from estimagic.optimization.tranquilo.options import Region


@dataclass
Expand Down
27 changes: 27 additions & 0 deletions src/estimagic/optimization/tranquilo/options.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import dataclass
from typing import NamedTuple

import numpy as np
Expand All @@ -8,6 +9,17 @@
)


@dataclass
class Bounds:
"""Stopping criteria."""

lower: np.ndarray
upper: np.ndarray

def __post_init__(self):
self.has_any = _check_if_there_are_bounds(self.lower, self.upper)


class StopOptions(NamedTuple):
"""Criteria for stopping without successful convergence."""

Expand Down Expand Up @@ -42,6 +54,12 @@ class RadiusOptions(NamedTuple):
max_radius_to_step_ratio: float = np.inf


class Region(NamedTuple):
center: np.ndarray
radius: float
shape: str


class AcceptanceOptions(NamedTuple):
confidence_level: float = 0.8
power_level: float = 0.8
Expand All @@ -58,3 +76,12 @@ class StagnationOptions(NamedTuple):
sample_increment: int = 1
max_trials: int = 1
drop: bool = True


def _check_if_there_are_bounds(lb, ub):
out = False
if lb is not None and np.isfinite(lb).any():
out = True
if ub is not None and np.isfinite(ub).any():
out = True
return out
116 changes: 0 additions & 116 deletions src/estimagic/optimization/tranquilo/region.py

This file was deleted.

Loading

0 comments on commit 418149e

Please sign in to comment.