Skip to content

Commit

Permalink
Improve implementation of _compute_normalization (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
herilalaina authored Jul 7, 2023
1 parent bebc36e commit 93abc5b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ConfigSpace/hyperparameters/beta_integer.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ cdef class BetaIntegerHyperparameter(UniformIntegerHyperparameter):
cdef public alpha
cdef public beta
cdef public bfhp
cdef normalization_constant
cdef public normalization_constant
15 changes: 14 additions & 1 deletion ConfigSpace/hyperparameters/beta_integer.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io
from typing import Any, Dict, Optional, Union

from scipy.stats import beta as spbeta

import numpy as np
cimport numpy as np
Expand Down Expand Up @@ -175,7 +176,19 @@ cdef class BetaIntegerHyperparameter(UniformIntegerHyperparameter):
return value

def _compute_normalization(self):
chunks = arange_chunked(self.lower, self.upper + 1, chunk_size=ARANGE_CHUNKSIZE)
if self.upper - self.lower > ARANGE_CHUNKSIZE:
a = self.bfhp._inverse_transform(self.lower)
b = self.bfhp._inverse_transform(self.upper)
confidence = 0.999999
rv = spbeta(self.alpha, self.beta, loc=a, scale=b-a)
u, v = rv.ppf((1 - confidence) / 2), rv.ppf((1 + confidence) / 2)
lb = max(self.bfhp._transform(u), self.lower)
ub = min(self.bfhp._transform(v), self.upper + 1)
else:
lb = self.lower
ub = self.upper + 1

chunks = arange_chunked(lb, ub, chunk_size=ARANGE_CHUNKSIZE)
return sum(self.bfhp.pdf(chunk).sum() for chunk in chunks)

def _pdf(self, vector: np.ndarray) -> np.ndarray:
Expand Down
2 changes: 1 addition & 1 deletion ConfigSpace/hyperparameters/normal_integer.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ cdef class NormalIntegerHyperparameter(IntegerHyperparameter):
cdef public mu
cdef public sigma
cdef public nfhp
cdef normalization_constant
cdef public normalization_constant
14 changes: 13 additions & 1 deletion ConfigSpace/hyperparameters/normal_integer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,19 @@ cdef class NormalIntegerHyperparameter(IntegerHyperparameter):
return 1

else:
chunks = arange_chunked(self.lower, self.upper + 1, chunk_size=ARANGE_CHUNKSIZE)
if self.upper - self.lower > ARANGE_CHUNKSIZE:
a = (self.lower - self.mu) / self.sigma
b = (self.upper - self.mu) / self.sigma
confidence = 0.999999
rv = truncnorm(a=a, b=b, loc=self.mu, scale=self.sigma)
u, v = rv.ppf((1 - confidence) / 2), rv.ppf((1 + confidence) / 2)
lb = max(u, self.lower)
ub = min(v, self.upper + 1)
else:
lb = self.lower
ub = self.upper + 1

chunks = arange_chunked(lb, ub, chunk_size=ARANGE_CHUNKSIZE)
return sum(self.nfhp.pdf(chunk).sum() for chunk in chunks)

def _pdf(self, vector: np.ndarray) -> np.ndarray:
Expand Down
21 changes: 21 additions & 0 deletions test/test_hyperparameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import numpy as np
import pytest

from ConfigSpace.functional import arange_chunked
from ConfigSpace.hyperparameters import (
BetaFloatHyperparameter,
BetaIntegerHyperparameter,
Expand Down Expand Up @@ -1708,6 +1709,16 @@ def test_normalint_get_max_density(self):
self.assertAlmostEqual(c2.get_max_density(), 0.002790371598208875)
self.assertAlmostEqual(c3.get_max_density(), 0.9988874412972069)

def test_normalint_compute_normalization(self):
ARANGE_CHUNKSIZE = 10_000_000
lower, upper = 1, ARANGE_CHUNKSIZE * 2

c = NormalIntegerHyperparameter("c", mu=10, sigma=500, lower=lower, upper=upper)
chunks = arange_chunked(lower, upper, chunk_size=ARANGE_CHUNKSIZE)
# exact computation over the complete range
N = sum(c.nfhp.pdf(chunk).sum() for chunk in chunks)
self.assertAlmostEqual(c.normalization_constant, N, places=5)

############################################################
def test_betaint(self):
# TODO test non-equality
Expand Down Expand Up @@ -2096,6 +2107,16 @@ def test_betaint_get_max_density(self):
self.assertAlmostEqual(c2.get_max_density(), 0.0018733953504422762)
self.assertAlmostEqual(c3.get_max_density(), 0.9979110652388783)

def test_betaint_compute_normalization(self):
ARANGE_CHUNKSIZE = 10_000_000
lower, upper = 0, ARANGE_CHUNKSIZE * 2

c = BetaIntegerHyperparameter("c", alpha=3, beta=2, lower=lower, upper=upper)
chunks = arange_chunked(lower, upper, chunk_size=ARANGE_CHUNKSIZE)
# exact computation over the complete range
N = sum(c.bfhp.pdf(chunk).sum() for chunk in chunks)
self.assertAlmostEqual(c.normalization_constant, N, places=5)

def test_categorical(self):
# TODO test for inequality
f1 = CategoricalHyperparameter("param", [0, 1])
Expand Down

0 comments on commit 93abc5b

Please sign in to comment.