Skip to content

Commit

Permalink
Fixed circular dependency, broken test.
Browse files Browse the repository at this point in the history
  • Loading branch information
cgranade committed Sep 19, 2016
1 parent f92c54e commit 1023981
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
33 changes: 6 additions & 27 deletions src/qinfer/ale.py
Expand Up @@ -45,35 +45,11 @@

from scipy.stats.distributions import binom

from qinfer.utils import binom_est_error, binom_est_p
from qinfer.derived_models import DerivedModel
from qinfer.abstract_model import Model, Simulatable, FiniteOutcomeModel
from qinfer._exceptions import ApproximationWarning

## FUNCTIONS ##################################################################

def binom_est_p(n, N, hedge=float(0)):
r"""
Given a number of successes :math:`n` and a number of trials :math:`N`,
estimates the binomial distribution parameter :math:`p` using the
hedged maximum likelihood estimator of [FB12]_.
:param n: Number of successes.
:type n: `numpy.ndarray` or `int`
:param int N: Number of trials.
:param float hedge: Hedging parameter :math:`\beta`.
:rtype: `float` or `numpy.ndarray`.
:return: The estimated binomial distribution parameter :math:`p` for each
value of :math:`n`.
"""
return (n + hedge) / (N + 2 * hedge)

def binom_est_error(p, N, hedge = float(0)):
r"""
"""

# asymptotic np.sqrt(p * (1 - p) / N)
return np.sqrt(p*(1-p)/(N+2*hedge+1))

## CLASSES ####################################################################

class ALEApproximateModel(DerivedModel):
Expand Down Expand Up @@ -135,10 +111,13 @@ def __init__(self, simulator,
self._adapt_hedge = float(adapt_hedge)

## WRAPPED METHODS AND PROPERTIES ##
# We only need to wrap sim_count, since the rest are handled
# by DerivedModel.
# We only need to wrap sim_count and simulate_experiment,
# since the rest are handled by DerivedModel.
@property
def sim_count(self): return self.underlying_model.sim_count

def simulate_experiment(self, modelparams, expparams, repeat=1):
return self.underlying_model.simulate_experiment(modelparams, expparams, repeat=repeat)

## IMPLEMENTATIONS OF MODEL METHODS ##

Expand Down
2 changes: 1 addition & 1 deletion src/qinfer/derived_models.py
Expand Up @@ -52,7 +52,7 @@
from qinfer.utils import binomial_pdf, multinomial_pdf, sample_multinomial
from qinfer.abstract_model import Model, DifferentiableModel
from qinfer._lib import enum # <- TODO: replace with flufl.enum!
from qinfer.ale import binom_est_error
from qinfer.utils import binom_est_error
from qinfer.domains import IntegerDomain, MultinomialDomain

## FUNCTIONS ###################################################################
Expand Down
22 changes: 22 additions & 0 deletions src/qinfer/utils.py
Expand Up @@ -448,3 +448,25 @@ def safe_shape(arr, idx=0, default=1):
ax.plot_surface(x, y, z, cstride = 1, rstride = 1, alpha = 0.1)
plt.show()

def binom_est_p(n, N, hedge=float(0)):
r"""
Given a number of successes :math:`n` and a number of trials :math:`N`,
estimates the binomial distribution parameter :math:`p` using the
hedged maximum likelihood estimator of [FB12]_.
:param n: Number of successes.
:type n: `numpy.ndarray` or `int`
:param int N: Number of trials.
:param float hedge: Hedging parameter :math:`\beta`.
:rtype: `float` or `numpy.ndarray`.
:return: The estimated binomial distribution parameter :math:`p` for each
value of :math:`n`.
"""
return (n + hedge) / (N + 2 * hedge)

def binom_est_error(p, N, hedge = float(0)):
r"""
"""

# asymptotic np.sqrt(p * (1 - p) / N)
return np.sqrt(p*(1-p)/(N+2*hedge+1))

0 comments on commit 1023981

Please sign in to comment.