コード例 #1
0
ファイル: test_noisyopt.py プロジェクト: 42Numeric/noisyopt
def test_bisect():
    xtol = 1e-6 

    ## simple tests
    root = noisyopt.bisect(lambda x: x, -2, 2, xtol=xtol)
    npt.assert_allclose(root, 0.0, atol=xtol)

    root = noisyopt.bisect(lambda x: x-1, -2, 2, xtol=xtol)
    npt.assert_allclose(root, 1.0, atol=xtol)

    ## extrapolate if 0 outside of interval
    root = noisyopt.bisect(lambda x: x, 1, 2, xtol=xtol)
    npt.assert_allclose(root, 0.0, atol=xtol)
    npt.assert_raises(noisyopt.BisectException,
                      noisyopt.bisect, lambda x: x, 1, 2,
                      xtol=xtol, outside='raise')
    
    ## extrapolate with nonlinear function
    root = noisyopt.bisect(lambda x: x+x**2, 1.0, 2, xtol=xtol)
    assert root < 1.0

    ## test with stochastic function
    xtol = 1e-1
    func = lambda x: x - 0.25 + np.random.normal(scale=0.01)
    root = noisyopt.bisect(noisyopt.AveragedFunction(func), -2, 2, xtol=xtol,
                           errorcontrol=True)
    npt.assert_allclose(root, 0.25, atol=xtol)
コード例 #2
0
    def get_mde_size_sample(self):
        n_null_metric_samples = 2000
        n_alt_metric_samples = 400
        null_metric_samples = []

        # Metric distribution under null hypothesis
        for sample in range(0, n_null_metric_samples):
            group_a1_responses = self.get_group_a1_samples()
            group_a2_responses = self.get_group_a2_samples()
            group_b1_responses = self.get_group_b1_samples()
            group_b2_responses_null = self.get_group_b2_samples_by_effect(effect=0)

            null_metric_samples.append(
                (np.mean(group_b2_responses_null) - np.mean(group_b1_responses)) -
                (np.mean(group_a2_responses) - np.mean(group_a1_responses))
            )

        null_critical_value = np.percentile(null_metric_samples, (1 - self.alpha / 2) * 100)

        # Speeding up the search by bounding the search space more efficiently
        indicative_mde = (
                null_critical_value / norm.ppf(1 - (self.alpha / 2)) *
                (norm.ppf(1 - (self.alpha / 2)) - norm.ppf(1 - self.pi_min)))
        search_lbound = indicative_mde * 0.7
        search_ubound = indicative_mde * 1.5

        # Let the root finding tolerance to be around 0.05% of the difference between
        # the group A means (in absolute value)
        # With enough bootstrap samples this error would be further reduced
        mde_search_tol = np.abs(np.mean(self.get_group_a2_samples()) - np.mean(self.get_group_a1_samples())) * 0.0005

        # Extrapolation sometimes give extremely large (both +ve/-ve) values
        # due to small denominator, the while loop guards against that
        mde_sample = 2 * search_ubound
        while np.abs(mde_sample) > search_ubound:
            mde_sample = noisyopt.bisect(
                noisyopt.AveragedFunction(
                    lambda x: self._simulated_power_dual_control(x, null_critical_value, n_alt_metric_samples) -
                              self.pi_min,
                    N=20),
                search_lbound, search_ubound,
                xtol=mde_search_tol, errorcontrol=True,
                testkwargs={'alpha': 0.01, 'force': True, 'eps': 0.001, 'maxN': 160},
                outside='extrapolate', ascending=True, disp=False)

        return mde_sample
コード例 #3
0
def test_bisect():
    xtol = 1e-6

    ## simple tests
    # ascending
    root = noisyopt.bisect(lambda x: x, -2, 2, xtol=xtol, errorcontrol=False)
    npt.assert_allclose(root, 0.0, atol=xtol)

    root = noisyopt.bisect(lambda x: x - 1,
                           -2,
                           2,
                           xtol=xtol,
                           errorcontrol=False)
    npt.assert_allclose(root, 1.0, atol=xtol)

    # descending
    root = noisyopt.bisect(lambda x: -x, -2, 2, xtol=xtol, errorcontrol=False)
    npt.assert_allclose(root, 0.0, atol=xtol)

    ## extrapolate if 0 outside of interval
    root = noisyopt.bisect(lambda x: x, 1, 2, xtol=xtol, errorcontrol=False)
    npt.assert_allclose(root, 0.0, atol=xtol)
    npt.assert_raises(noisyopt.BisectException,
                      noisyopt.bisect,
                      lambda x: x,
                      1,
                      2,
                      xtol=xtol,
                      outside='raise',
                      errorcontrol=False)

    ## extrapolate with nonlinear function
    root = noisyopt.bisect(lambda x: x + x**2,
                           1.0,
                           2,
                           xtol=xtol,
                           errorcontrol=False)
    assert root < 1.0

    ## test with stochastic function
    xtol = 1e-1
    func = lambda x: x - 0.25 + np.random.normal(scale=0.01)
    root = noisyopt.bisect(noisyopt.AveragedFunction(func),
                           -2,
                           2,
                           xtol=xtol,
                           errorcontrol=True)
    npt.assert_allclose(root, 0.25, atol=xtol)
コード例 #4
0
    sig_fits.append(guess)
    logMmin_list.append(m)
    ng_diff = wp_ng_vals[0] - ng
    func_val.append(ng_diff)
    return ng_diff


for m in logMmin:
    print("logMmin: " + str(np.log10(m)))
    model_instance.param_dict['logMmin'] = np.log10(m)
    #res = minimizeCompass(_find_sigma,[0.3], bounds = [[0.01,1.5]],deltatol=0.0001,
    #                      niter = 100, paired=False, disp=False, errorcontrol=False)
    #res = minimizeCompass(_find_sigma,[0.3], bounds = [[0.01,1.5]],
    #                      niter = 100, paired=False, disp=False, errorcontrol=False)
    avfunc = AveragedFunction(_find_sigma)
    res = bisect(avfunc,
                 0.01,
                 2.,
                 xtol=1e-06,
                 errorcontrol=True,
                 outside='extrapolate',
                 ascending=None,
                 disp=False,
                 testkwargs={0.0001})

#res = op.minimize(_get_ng,[12.,0.30],options={'maxiter': 1e3,'disp':True},tol=1e-3)
#res = minimizeCompass(_get_ng,[12.36,0.38],bounds = [[11.0, 14.0], [0.01, 2.0]], deltatol=1e#-6, a=5.0, disp = True, paired=False)

np.save("logMmin_sigma_errctrl.npy",
        np.array([logMmin_list, sig_fits, func_val]))
コード例 #5
0
ファイル: run_phases.py プロジェクト: xuepingsun/evolimmune
 njob = int(sys.argv[1])
 data = []
 for i in progressbar(range(nbatch)):
     n = (njob - 1) * nbatch + i
     boundary, aenv = paramscomb[n]
     if disp:
         print 'boundary %s, aenv %s' % (boundary, aenv)
     bisect_kwargs = dict(xtol=xtol,
                          errorcontrol=True,
                          testkwargs=dict(alpha=alpha),
                          ascending=False,
                          disp=disp)
     fargs = lambda_, mus, cup, aenv, niter, nburnin
     pienvbnd = noisyopt.bisect(
         noisyopt.DifferenceFunction(cuts[boundary[0]],
                                     cuts[boundary[1]],
                                     fargs1=fargs,
                                     fargs2=fargs,
                                     paired=True), 0, 1, **bisect_kwargs)
     data.append([
         boundary, lambda_, mus, cup, aenv, niter, nburnin, deltainit,
         deltatol,
         np.log10(feps), boundtol, qboundtol, xtol, xtolbound, pienvbnd
     ])
 columns = [
     'boundary', 'lambda_', 'mus', 'cup', 'aenv', 'niter', 'nburnin',
     'deltainit', 'deltatol', 'logfeps', 'boundtol', 'qboundtol', 'xtol',
     'xtolbound', 'pienvbnd'
 ]
 np.savez_compressed(datadir + 'scan_phases_%g' % (njob),
                     data=data,
                     columns=columns)
コード例 #6
0
ファイル: bisect.py プロジェクト: vishalbelsare/noisyopt
# This is a minimal usage example for the root finding algorithm
import numpy as np
from noisyopt import bisect, AveragedFunction

# definition of noisy function of which root should be found
def func(x):
    return x + 0.1*np.random.randn()

avfunc = AveragedFunction(func)
root = bisect(avfunc, -2, 2)
print(root)
コード例 #7
0
ファイル: run_phases.py プロジェクト: andim/evolimmune
                               ('io', filterarray(aenvs, 0.01, 1.0)),
                               ('pm', aenvs),
                               ('pi', filterarray(aenvs, 0.0, 0.9)),
                               ('po', aenvs[[0, -1]]),
                               ('pc', aenvs),
                               ])

# define cut functions
cuts = strategies.cuts(coptkwargs=coptkwargs, moptkwargs=moptkwargs,
                       ioptkwargs=ioptkwargs, poptkwargs=poptkwargs)
# run simulations
if parametercheck(datadir, sys.argv, paramscomb, nbatch):
    njob = int(sys.argv[1])
    data = []
    for i in progressbar(range(nbatch)):
        n = (njob-1) * nbatch + i
        boundary, aenv = paramscomb[n]
        if disp:
            print 'boundary %s, aenv %s' % (boundary, aenv)
        bisect_kwargs = dict(xtol=xtol, errorcontrol=True, testkwargs=dict(alpha=alpha),
                             ascending=False, disp=disp)
        fargs = lambda_, mus, cup, aenv, niter, nburnin
        pienvbnd = noisyopt.bisect(noisyopt.DifferenceFunction(cuts[boundary[0]], cuts[boundary[1]],
                                                               fargs1=fargs, fargs2=fargs, paired=True),
                                   0, 1, **bisect_kwargs)
        data.append([boundary, lambda_, mus, cup, aenv, niter, nburnin, deltainit, deltatol,
                     np.log10(feps), boundtol, qboundtol, xtol, xtolbound, pienvbnd])
    columns = ['boundary', 'lambda_', 'mus', 'cup', 'aenv', 'niter', 'nburnin', 'deltainit',
               'deltatol', 'logfeps', 'boundtol', 'qboundtol', 'xtol', 'xtolbound', 'pienvbnd']
    np.savez_compressed(datadir + 'scan_phases_%g' % (njob), data=data, columns=columns)