# Path: crowdastro/crowd/raykar.py
# EPS = 1E-8
#         X = numpy.hstack([X, numpy.ones((X.shape[0], 1))])
#         X = numpy.hstack([X, numpy.ones((X.shape[0], 1))])
# class RaykarClassifier(object):
#     def __init__(self, n_restarts=5, epsilon=1e-5, lr_init=True):
#     def fit(self, X, Y):
#     def _fit_params(self, x, y):
#     def _exp_m_step(self, a, b, w, x, y, y_mask):
#     def _hessian_inverse_multiply(self, x, H, g):
#     def _max_w_step(self, a, b, m, x, y_0, y_1, mv, init_w=None):
#     def _max_alpha_step(self, m, y, y_mask):
#     def _max_beta_step(self, m, y, y_mask):
#     def predict(self, X):
#     def predict_proba(self, X):
#     def score(self, X, Y):
#     def _log_likelihood(self, *args, **kwargs):
#     def _likelihood(self, w, a, b, X, Y_0, Y_1):
#     def get_params(self, deep=True):
#     def set_params(self, **parameters):
#     def serialise(self):
#     def unserialise(cls, array):
# 
# Path: crowdastro/crowd/util.py
# def logistic_regression(w, x):
#     """Logistic regression classifier model.
# 
#     w: Weights w. (n_features,) NumPy array
#     x: Data point x_i. (n_features,) NumPy array
#     -> float in [0, 1]
#     """
#     return scipy.special.expit(numpy.dot(x, w.T))


import unittest
import numpy
import scipy.optimize
import sklearn.linear_model
from crowdastro.crowd import raykar
from crowdastro.crowd.util import logistic_regression
        y_1 = self.y.filled(1)
        lh = self.rc._likelihood(w, a, b, x, y_0, y_1)
        y_flip = 1 - self.y
        y_0 = y_flip.filled(0)
        y_1 = y_flip.filled(1)
        lr.fit(self.x, 1 - self.z)
        w = numpy.concatenate([lr.coef_, lr.intercept_.reshape((1, 1))], axis=1)
        lh_flip = self.rc._likelihood(w, b, a, x, y_0, y_1)
        self.assertTrue(numpy.isclose(lh, lh_flip))

    def test_max_steps(self):
        """_max_beta_step and _max_alpha_step are symmetric."""
        m = numpy.random.random(size=(self.n_examples,))
        mbs = self.rc._max_beta_step(m, self.y.filled(0), self.y.mask)
        mas = self.rc._max_alpha_step(m, self.y.filled(0), self.y.mask)
        mbs_ = self.rc._max_beta_step(1 - m, 1 - self.y.filled(1), self.y.mask)
        mas_ = self.rc._max_alpha_step(1 - m, 1 - self.y.filled(1), self.y.mask)
        self.assertTrue(numpy.allclose(mbs, mas_))
        self.assertTrue(numpy.allclose(mas, mbs_))

    def test_exp_m_step(self):
        """_exp_m_step is symmetric."""
        a = numpy.random.random(size=(self.n_labellers, 1))
        b = numpy.random.random(size=(self.n_labellers, 1))
        w = numpy.random.random(size=(self.n_features + 1))
        x = numpy.hstack([self.x, numpy.ones((self.n_examples, 1))])
        y_mask = self.y.mask
        m = self.rc._exp_m_step(a, b, w, x, self.y.filled(0), y_mask)
        w_flip = scipy.optimize.fmin_bfgs(
            lambda k: ((logistic_regression(w, x) - 1 +

# Path: crowdastro/crowd/raykar.py
# EPS = 1E-8
#         X = numpy.hstack([X, numpy.ones((X.shape[0], 1))])
#         X = numpy.hstack([X, numpy.ones((X.shape[0], 1))])
# class RaykarClassifier(object):
#     def __init__(self, n_restarts=5, epsilon=1e-5, lr_init=True):
#     def fit(self, X, Y):
#     def _fit_params(self, x, y):
#     def _exp_m_step(self, a, b, w, x, y, y_mask):
#     def _hessian_inverse_multiply(self, x, H, g):
#     def _max_w_step(self, a, b, m, x, y_0, y_1, mv, init_w=None):
#     def _max_alpha_step(self, m, y, y_mask):
#     def _max_beta_step(self, m, y, y_mask):
#     def predict(self, X):
#     def predict_proba(self, X):
#     def score(self, X, Y):
#     def _log_likelihood(self, *args, **kwargs):
#     def _likelihood(self, w, a, b, X, Y_0, Y_1):
#     def get_params(self, deep=True):
#     def set_params(self, **parameters):
#     def serialise(self):
#     def unserialise(cls, array):
# 
# Path: crowdastro/crowd/util.py
# def logistic_regression(w, x):
#     """Logistic regression classifier model.
# 
#     w: Weights w. (n_features,) NumPy array
#     x: Data point x_i. (n_features,) NumPy array
#     -> float in [0, 1]
#     """
#     return scipy.special.expit(numpy.dot(x, w.T))


# Path: crowdastro/crowd/raykar_test.py
import unittest
import numpy
import scipy.optimize
import sklearn.linear_model
from crowdastro.crowd import raykar
from crowdastro.crowd.util import logistic_regression

        y_1 = self.y.filled(1)
        lh = self.rc._likelihood(w, a, b, x, y_0, y_1)
        y_flip = 1 - self.y
        y_0 = y_flip.filled(0)
        y_1 = y_flip.filled(1)
        lr.fit(self.x, 1 - self.z)
        w = numpy.concatenate([lr.coef_, lr.intercept_.reshape((1, 1))], axis=1)
        lh_flip = self.rc._likelihood(w, b, a, x, y_0, y_1)
        self.assertTrue(numpy.isclose(lh, lh_flip))

    def test_max_steps(self):
        """_max_beta_step and _max_alpha_step are symmetric."""
        m = numpy.random.random(size=(self.n_examples,))
        mbs = self.rc._max_beta_step(m, self.y.filled(0), self.y.mask)
        mas = self.rc._max_alpha_step(m, self.y.filled(0), self.y.mask)
        mbs_ = self.rc._max_beta_step(1 - m, 1 - self.y.filled(1), self.y.mask)
        mas_ = self.rc._max_alpha_step(1 - m, 1 - self.y.filled(1), self.y.mask)
        self.assertTrue(numpy.allclose(mbs, mas_))
        self.assertTrue(numpy.allclose(mas, mbs_))

    def test_exp_m_step(self):
        """_exp_m_step is symmetric."""
        a = numpy.random.random(size=(self.n_labellers, 1))
        b = numpy.random.random(size=(self.n_labellers, 1))
        w = numpy.random.random(size=(self.n_features + 1))
        x = numpy.hstack([self.x, numpy.ones((self.n_examples, 1))])
        y_mask = self.y.mask
        m = self.rc._exp_m_step(a, b, w, x, self.y.filled(0), y_mask)
        w_flip = scipy.optimize.fmin_bfgs(
            lambda k: ((logistic_regression(w, x) - 1 +

                        logistic_regression(k, x)) ** 2 +