Skip to content

Commit

Permalink
Rename chi_sq_conf_lvl -> gaussian_conf_lvl
Browse files Browse the repository at this point in the history
  • Loading branch information
shermanjasonaf committed Nov 25, 2024
1 parent b9576c0 commit 6b36ebb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
42 changes: 21 additions & 21 deletions pyomo/contrib/pyros/tests/test_uncertainty_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1868,8 +1868,8 @@ def test_normal_construction_and_update(self):
# evaluate chisquare CDF for 2 degrees of freedom
# using simplified formula
1 - np.exp(-scale / 2),
eset.chi_sq_conf_lvl,
err_msg="EllipsoidalSet chi-squared confidence level not as expected",
eset.gaussian_conf_lvl,
err_msg="EllipsoidalSet Gaussian confidence level not as expected",
)

# check attributes update
Expand Down Expand Up @@ -1898,11 +1898,11 @@ def test_normal_construction_and_update(self):
# evaluate chisquare CDF for 2 degrees of freedom
# using simplified formula
1 - np.exp(-new_scale / 2),
eset.chi_sq_conf_lvl,
err_msg="EllipsoidalSet chi-square confidence level update not as expected",
eset.gaussian_conf_lvl,
err_msg="EllipsoidalSet Gaussian confidence level update not as expected",
)

def test_normal_construction_and_update_chi_sq_conf_lvl(self):
def test_normal_construction_and_update_gaussian_conf_lvl(self):
"""
Test EllipsoidalSet constructor and setter
work normally when arguments are appropriate.
Expand All @@ -1912,19 +1912,19 @@ def test_normal_construction_and_update_chi_sq_conf_lvl(self):
center=[0, 0, 0],
shape_matrix=np.eye(3),
scale=None,
chi_sq_conf_lvl=init_conf_lvl,
gaussian_conf_lvl=init_conf_lvl,
)

self.assertEqual(eset.chi_sq_conf_lvl, init_conf_lvl)
self.assertEqual(eset.gaussian_conf_lvl, init_conf_lvl)
np.testing.assert_allclose(
sp.stats.chi2.isf(q=1 - init_conf_lvl, df=eset.dim),
eset.scale,
err_msg="EllipsoidalSet scale not as expected",
)

new_conf_lvl = 0.99
eset.chi_sq_conf_lvl = new_conf_lvl
self.assertEqual(eset.chi_sq_conf_lvl, new_conf_lvl)
eset.gaussian_conf_lvl = new_conf_lvl
self.assertEqual(eset.gaussian_conf_lvl, new_conf_lvl)
np.testing.assert_allclose(
sp.stats.chi2.isf(q=1 - new_conf_lvl, df=eset.dim),
eset.scale,
Expand Down Expand Up @@ -1970,9 +1970,9 @@ def test_error_on_neg_scale(self):
with self.assertRaisesRegex(ValueError, exc_str):
eset.scale = neg_scale

def test_error_invalid_chi_sq_conf_lvl(self):
def test_error_invalid_gaussian_conf_lvl(self):
"""
Test error when attempting to initialize with chi-squared
Test error when attempting to initialize with Gaussian
confidence level outside range.
"""
center = [0, 0]
Expand All @@ -1987,30 +1987,30 @@ def test_error_invalid_chi_sq_conf_lvl(self):
center=center,
shape_matrix=shape_matrix,
scale=None,
chi_sq_conf_lvl=invalid_conf_lvl,
gaussian_conf_lvl=invalid_conf_lvl,
)

# error on updating valid ellipsoid
eset = EllipsoidalSet(center, shape_matrix, scale=None, chi_sq_conf_lvl=0.95)
eset = EllipsoidalSet(center, shape_matrix, scale=None, gaussian_conf_lvl=0.95)
with self.assertRaisesRegex(ValueError, exc_str):
eset.chi_sq_conf_lvl = invalid_conf_lvl
eset.gaussian_conf_lvl = invalid_conf_lvl

# negative confidence level
eset = EllipsoidalSet(center, shape_matrix, scale=None, chi_sq_conf_lvl=0.95)
eset = EllipsoidalSet(center, shape_matrix, scale=None, gaussian_conf_lvl=0.95)
with self.assertRaisesRegex(ValueError, exc_str):
eset.chi_sq_conf_lvl = -0.1
eset.gaussian_conf_lvl = -0.1

def test_error_scale_chi_sq_conf_lvl_construction(self):
def test_error_scale_gaussian_conf_lvl_construction(self):
"""
Test exception raised if neither or both of
`scale` and `chi_sq_conf_lvl` are None.
`scale` and `gaussian_conf_lvl` are None.
"""
exc_str = r"Exactly one of `scale` and `chi_sq_conf_lvl` should be None"
exc_str = r"Exactly one of `scale` and `gaussian_conf_lvl` should be None"
with self.assertRaisesRegex(ValueError, exc_str):
EllipsoidalSet([0], [[1]], scale=None, chi_sq_conf_lvl=None)
EllipsoidalSet([0], [[1]], scale=None, gaussian_conf_lvl=None)

with self.assertRaisesRegex(ValueError, exc_str):
EllipsoidalSet([0], [[1]], scale=1, chi_sq_conf_lvl=0.95)
EllipsoidalSet([0], [[1]], scale=1, gaussian_conf_lvl=0.95)

def test_error_on_shape_matrix_with_wrong_size(self):
"""
Expand Down
40 changes: 20 additions & 20 deletions pyomo/contrib/pyros/uncertainty_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2380,11 +2380,11 @@ class EllipsoidalSet(UncertaintySet):
Square of the factor by which to scale the semi-axes
of the ellipsoid (i.e. the eigenvectors of the shape
matrix). The default is `1`.
chi_sq_conf_lvl : numeric type or None, optional
(Fractional) chi-squared confidence level of the multivariate
gaussian_conf_lvl : numeric type or None, optional
(Fractional) confidence level of the multivariate
normal distribution with mean `center` and covariance
matrix `shape_matrix`.
Exactly one of `scale` and `chi_sq_conf_lvl` should be
Exactly one of `scale` and `gaussian_conf_lvl` should be
None; otherwise, an exception is raised.
Examples
Expand Down Expand Up @@ -2428,7 +2428,7 @@ class EllipsoidalSet(UncertaintySet):
... center=np.zeros(4),
... shape_matrix=np.diag(range(1, 5)),
... scale=None,
... chi_sq_conf_lvl=0.95,
... gaussian_conf_lvl=0.95,
... )
>>> conf_ellipsoid.center
array([0, 0, 0, 0])
Expand All @@ -2439,24 +2439,24 @@ class EllipsoidalSet(UncertaintySet):
[0, 0, 0. 4]])
>>> conf_ellipsoid.scale
...9.4877...
>>> conf_ellipsoid.chi_sq_conf_lvl
>>> conf_ellipsoid.gaussian_conf_lvl
0.95
"""

def __init__(self, center, shape_matrix, scale=1, chi_sq_conf_lvl=None):
def __init__(self, center, shape_matrix, scale=1, gaussian_conf_lvl=None):
"""Initialize self (see class docstring)."""
self.center = center
self.shape_matrix = shape_matrix

if scale is not None and chi_sq_conf_lvl is None:
if scale is not None and gaussian_conf_lvl is None:
self.scale = scale
elif scale is None and chi_sq_conf_lvl is not None:
self.chi_sq_conf_lvl = chi_sq_conf_lvl
elif scale is None and gaussian_conf_lvl is not None:
self.gaussian_conf_lvl = gaussian_conf_lvl
else:
raise ValueError(
"Exactly one of `scale` and `chi_sq_conf_lvl` should be "
f"None (got {scale=}, {chi_sq_conf_lvl=})"
"Exactly one of `scale` and `gaussian_conf_lvl` should be "
f"None (got {scale=}, {gaussian_conf_lvl=})"
)

@property
Expand Down Expand Up @@ -2599,24 +2599,24 @@ def scale(self, val):
)

self._scale = val
self._chi_sq_conf_lvl = sp.stats.chi2.cdf(x=val, df=self.dim)
self._gaussian_conf_lvl = sp.stats.chi2.cdf(x=val, df=self.dim)

@property
def chi_sq_conf_lvl(self):
def gaussian_conf_lvl(self):
"""
numeric type : (Fractional) chi-squared confidence level of the
multivariate normal distribution with mean ``self.origin``
numeric type : (Fractional) confidence level of the
multivariate Gaussian distribution with mean ``self.origin``
and covariance ``self.shape_matrix`` for ellipsoidal region
with square magnification factor ``self.scale``.
"""
return self._chi_sq_conf_lvl
return self._gaussian_conf_lvl

@chi_sq_conf_lvl.setter
def chi_sq_conf_lvl(self, val):
@gaussian_conf_lvl.setter
def gaussian_conf_lvl(self, val):
validate_arg_type(
"chi_sq_conf_lvl", val, valid_num_types, "a valid numeric type", False
"gaussian_conf_lvl", val, valid_num_types, "a valid numeric type", False
)
self._chi_sq_conf_lvl = val
self._gaussian_conf_lvl = val
self._scale = sp.stats.chi2.isf(q=1 - val, df=self.dim)
if np.isnan(self._scale) or np.isinf(self._scale):
raise ValueError(
Expand Down

0 comments on commit 6b36ebb

Please sign in to comment.