def gradWB(new,objVOI,BN,keep):
    points=objVOI._points
    kern=objVOI._k
    alpha1=0.5*((kern.alpha[0:n1])**2)/scaleAlpha**2
    alpha2=0.5*((kern.alpha[n1:n1+n2])**2)/scaleAlpha**2
    variance0=kern.variance
    wNew=new[0,n1:n1+n2].reshape((1,n2))
    gradWBarray=np.zeros([len(keep),n2])
    M=len(keep)
    parameterLamb=parameterSetsPoisson
   # quantil=int(poisson.ppf(.99999999,max(parameterLamb)))
   # expec=np.array([i for i in xrange(quantil)])
   # logproductExpectations=0.0
  #  a=range(n2)
    X=new[0,0:n1]
    W=new[0,n1:n1+n2]
   
    for i in xrange(n2):
        logproductExpectations=0.0
        a=range(n2)
        del a[i]
        for r in a:
            G=poisson(parameterLamb[r])
            temp=G.dist.expect(lambda z: np.exp(-alpha2[r]*((z-W[r])**2)),G.args)
            logproductExpectations+=np.log(temp)
        G=poisson(parameterLamb[i])
        temp=G.dist.expect(lambda z: -2.0*alpha2[i]*(-z+W[i])*np.exp(-alpha2[i]*((z-W[i])**2)),G.args)
        productExpectations=np.exp(logproductExpectations)*temp
        for j in xrange(M):
            gradWBarray[j,i]=np.log(variance0)-np.sum(alpha1*((points[keep[j],:]-X)**2))
            gradWBarray[j,i]=np.exp(gradWBarray[j,i])*productExpectations
    return gradWBarray
    def fitted_plots(self):
        n = self.count_data.number_of_observations()

        m = 16
        count_range = np.array(range(m))
        nbs = np.repeat(np.array(count_range), self.num_segments).reshape([self.num_segments, m], order='F')  # S-by-m
        segment_probs = self.segment_probs.reshape(self.num_segments, 1)  # make sure shape is S-by-1

        figs = list()
        for cat in range(self.count_data.num_categories):
            counts_observed = [(self.count_data.arr[:, cat] == count).sum() for count in count_range]

            poisson_means = self.lambdas[:, cat].reshape(self.num_segments, 1)  # S-by-1
            poisson_pmf = poisson(poisson_means).pmf(nbs)  # S-by-m
            if self.inflated_zeros:
                deflate_probs = self.deflate_probs[:, cat].reshape(self.num_segments, 1)  # S-by-1
                poisson_pmf *= deflate_probs
                poisson_pmf += (1-deflate_probs) * poisson(1e-2).pmf(nbs)
            counts_predicted = (poisson_pmf * segment_probs).sum(axis=0) * n

            fig = plt.figure()
            plt.plot(count_range, counts_predicted, '--D', label="Fitted", zorder=4)
            plt.bar(count_range-0.45, counts_observed, width=0.9, color='lightgray', label="Observed", linewidth=1, edgecolor="gray",zorder=3)
            plt.grid(axis='y', which='major', color=(0.1, 0.1, 0.1), linestyle=':',zorder=0)
            plt.xlabel("Count of %s" % self.count_data.category_names[cat], fontsize=16)
            plt.ylabel("Number of observations", fontsize=16)
            plt.xticks(range(16), fontsize=13)
            plt.tick_params('both', length=0, width=1, which='major')
            plt.title("Aggregate Comparison at NumSegments=%d" % self.num_segments,fontsize=18)
            plt.legend(fontsize=16)
            plt.gca().set_axis_bgcolor((0.98, 0.98, 0.99))
            plt.xlim(-1,15.9)
            figs.append(fig)
Exemple #3
0
def test_intensity():
    from .. import toymodel

    np.random.seed(0)

    geom = CameraGeometry.from_name('LSTCam')

    width = 0.05
    length = 0.15
    intensity = 50

    # make a toymodel shower model
    model = toymodel.generate_2d_shower_model(
        centroid=(0.2, 0.3),
        width=width, length=length,
        psi='30d',
    )

    image, signal, noise = toymodel.make_toymodel_shower_image(
        geom, model.pdf, intensity=intensity, nsb_level_pe=5,
    )

    # test if signal reproduces given cog values
    assert np.average(geom.pix_x.value, weights=signal) == approx(0.2, rel=0.15)
    assert np.average(geom.pix_y.value, weights=signal) == approx(0.3, rel=0.15)

    # test if signal reproduces given width/length values
    cov = np.cov(geom.pix_x.value, geom.pix_y.value, aweights=signal)
    eigvals, eigvecs = np.linalg.eigh(cov)

    assert np.sqrt(eigvals[0]) == approx(width, rel=0.15)
    assert np.sqrt(eigvals[1]) == approx(length, rel=0.15)

    # test if total intensity is inside in 99 percent confidence interval
    assert poisson(intensity).ppf(0.05) <= signal.sum() <= poisson(intensity).ppf(0.95)
def poisson_best_fit(dataset, board_id=0):
    """ Returns the poisson fit for a sample set
    :param board_id: Board identifier.
    :param dataset: Data values.
    """
    # poisson_model = smf.poisson(AVG_TESTPROD_COLUMN + " ~ 1", data=dataset)
    poisson_model = sm.Poisson(dataset[AVG_TESTPROD_COLUMN], np.ones_like(dataset[AVG_TESTPROD_COLUMN]))

    result = poisson_model.fit()
    lmbda = np.exp(result.params)

    poisson_dist = stats.poisson(lmbda.values)
    lower_poisson_dist = stats.poisson(np.exp(result.conf_int().values)[0, 0])
    higher_poisson_dist = stats.poisson(np.exp(result.conf_int().values)[0, 1])

    print 'result.params ', result.params
    print 'lmbda ', lmbda
    print result.summary()

    testprod_samples = dataset[AVG_TESTPROD_COLUMN]
    print 'testprod_samples.mean ', testprod_samples.mean()

    plot_discrete_distributions(testprod_samples, [{"dist": poisson_dist,
                                                    "color": "red",
                                                    "name": "Poisson Fit"},
                                                   {"dist": lower_poisson_dist,
                                                    "color": "green",
                                                    "name": "Poisson Fit Lower"},
                                                   {"dist": higher_poisson_dist,
                                                    "color": "steelblue",
                                                    "name": "Poisson Fit Higher"}], board_id)

    return poisson_dist
def pmm_to_cluster(patientToGenes, classes, lam, p_k):
    clusterToPatient = {}

    for k in classes:
        clusterToPatient[k] = set()

    clusterToPatient[-1] = set()


    for patient in patientToGenes:
        d = len(patientToGenes[patient])

        max_class = -1
        max_ll = -np.inf
        for k in classes:
            if (np.log(p_k[k]) + np.log(poisson(lam[k]).pmf(d))) > -np.inf:
                if (np.log(p_k[k]) + np.log(poisson(lam[k]).pmf(d))) > max_ll:
                    max_class = k
                    max_ll = (np.log(poisson(lam[k]).pmf(d)))


        clusterToPatient[max_class].add(patient)

    for cluster in clusterToPatient:
        if not clusterToPatient[cluster]:
            clusterToPatient[cluster].add('EMPTY PATIENTS')

    return clusterToPatient
Exemple #6
0
def metro_exp_poison(chute=[1], N=1000):
    valores = chute
    taxa = []

    priori = expon(1)

    for i in range(N):
        expo_aux = expon(1)
        valor = expo_aux.rvs()

        U = random.rand()

        x_dado_y = poisson(valores[-1])
        y_dado_x = poisson(valor)

        teste = ( priori.pdf(valor) * x_dado_y.pmf(int(valores[-1])) ) / ( priori.pdf(valores[-1]) * y_dado_x.pmf(int(valor)) )

        if min([teste,1]) > U:
            valores.append(valor)
            taxa.append(1)
        else:
            valores.append(valores[-1])
            taxa.append(0)

    return {"valores":valores , "taxa":sum(taxa)/len(taxa)}
Exemple #7
0
def test_intensity():
    from ctapipe.image.toymodel import Gaussian

    np.random.seed(0)
    geom = CameraGeometry.from_name('LSTCam')

    x, y = u.Quantity([0.2, 0.3], u.m)
    width = 0.05 * u.m
    length = 0.15 * u.m
    intensity = 50
    psi = '30d'

    # make a toymodel shower model
    model = Gaussian(x=x, y=y, width=width, length=length, psi=psi)

    image, signal, noise = model.generate_image(
        geom, intensity=intensity, nsb_level_pe=5,
    )

    # test if signal reproduces given cog values
    assert np.average(geom.pix_x.to_value(u.m), weights=signal) == approx(0.2, rel=0.15)
    assert np.average(geom.pix_y.to_value(u.m), weights=signal) == approx(0.3, rel=0.15)

    # test if signal reproduces given width/length values
    cov = np.cov(geom.pix_x.value, geom.pix_y.value, aweights=signal)
    eigvals, eigvecs = np.linalg.eigh(cov)

    assert np.sqrt(eigvals[0]) == approx(width.to_value(u.m), rel=0.15)
    assert np.sqrt(eigvals[1]) == approx(length.to_value(u.m), rel=0.15)

    # test if total intensity is inside in 99 percent confidence interval
    assert poisson(intensity).ppf(0.05) <= signal.sum() <= poisson(intensity).ppf(0.95)
Exemple #8
0
    def test_timer(self):
        #Test a basic timer
        print()
        print("=====Testing RandomTimer with immediate = False=======")
        dp.Session.new()
        sim = dp.Simulation()            
        model1 = dp.model.Component("Timer_Test_Model_A")
        sim.model = model1   

        sim.config.seed = 731                  
        dist1 = stats.poisson(10)
        cb_class = TimerCallback()
        model1.add_component(dp.model.RandomTimer("timer",
                                                  dist1,
                                                  cb_class.call))
        self.assertEqual(len(model1.components), 1)
        
        results = sim.irunf(100)
         
        trace1 = results.trace
        self.assertEqual(trace1[0]['time'], 7)
        self.assertEqual(trace1[1]['time'], 15)
        self.assertEqual(trace1[2]['time'], 23)
         
        #Test timer with immediate = True
        print()
        print("=====Testing RandomTimer with immediate = True======")
        model2 = dp.model.Component("Timer_Test_Model_B")
        dist2 = stats.poisson(150)
        model2.add_component(
                   dp.model.RandomTimer("Timer", dist2, timerCB_function))
        sim2 = dp.Simulation(model2)
        sim2.config.seed = 704
        results = sim2.irunf(1000)
        trace2 = results.trace
        self.assertEqual(trace2[0]['time'], 142)
        self.assertEqual(trace2[1]['time'], 285)
        self.assertEqual(len(trace2), 3)
         
        #Test timer with Priority.LATE
        print()
        print("=====Testing Priority Attribute =======================")
        session = dp.Session.new()
        model3 = dp.model.Component("Timer_Test_Model_C")
 
        dist3 = drand.get_empirical_pmf([5, 10], [0.3, 0.7])
        model3.add_component(
                   dp.model.RandomTimer("timer", dist3, timerCB_function,
                                priority = dp.LATE))
        session.sim = sim3 = dp.Simulation(model3)
        session.config.seed = 731        
        results = sim3.irunf(100)
        trace3 = results.trace
        self.assertEqual(trace3[0]['priority'], 1)
        self.assertEqual(trace3[1]['priority'], 1)
        self.assertEqual(trace3[2]['interval'], 10)
        self.assertEqual(trace3[4]['interval'], 10)
        self.assertEqual(trace3[10]['interval'], 10)
    def test_multivariate_gaussian(self):
        from scipy.stats import poisson, norm

        po_normal = poisson(10)
        po_anomaly = poisson(25)

        po_normal2 = poisson(2)
        po_anomaly2 = poisson(3)

        gs_normal = norm(1, 12)
        gs_anomaly = norm(2, 30)

        normal_len = 10000
        anomaly_len = 15

        data = np.column_stack(
            [
                [1] * (normal_len + anomaly_len),
                list(po_normal.rvs(normal_len)) + list(po_anomaly.rvs(anomaly_len)),
                list(po_normal2.rvs(normal_len)) + list(po_anomaly2.rvs(anomaly_len)),
                list(gs_normal.rvs(normal_len)) + list(gs_anomaly.rvs(anomaly_len)),
            ]
        )
        anomaly_detector = pyisc.AnomalyDetector(
            component_models=[
                pyisc.P_PoissonOnesided(1, 0),  # columns 1 and 0
                pyisc.P_Poisson(2, 0),  # columns 2 and 0
                pyisc.P_Gaussian(3)  # column 3
            ],
            output_combination_rule=pyisc.cr_max
        )

        anomaly_detector.fit(data);
        # This above should fail this test if the problem still occurs:
        '''
        ---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-5-ecd0c0a2a8d4> in <module>()
----> 1 anomaly_detector.fit(data);

C:\ProgramData\Anaconda3\envs\pyISC_py27\lib\site-packages\_pyisc_modules\BaseISC.pyc in fit(self, X, y)
    313         
    314
--> 315         return self._fit(X,y)
    316
    317     def _fit(self,X,y=None):

C:\ProgramData\Anaconda3\envs\pyISC_py27\lib\site-packages\_pyisc_modules\BaseISC.pyc in _fit(self, X, y)
    352
    353             if data_object is not None:
--> 354                 assert self._max_index < data_object.length()  # ensure that data distribution has not to large index into the data
    355
    356                 return self._fit(data_object)

AssertionError:
        '''

        assert True;
def SalehValenzuela(**kwargs):
    """ generic Saleh and Valenzuela Model

    Parameters
    ----------

    Lam : clusters Poisson Process parameter (ns)
    lam : rays Poisson Process parameter (ns)
    Gam : clusters exponential decay factor
    gam : rays exponential decay factor
    tauM : maximum delay


    """
    defaults = { 'Lam' : 10.,
                 'lam' : 5.,
                 'Gam' : 30.,
                 'gam' : 5. ,
                 'tauM': 1000.}

    for k in defaults:
        if k not in kwargs:
            kwargs[k]=defaults[k]

    Lam = kwargs['Lam']
    lam = kwargs['lam']
    Gam = kwargs['Gam']
    gam = kwargs['gam']
    tauM = kwargs['tauM']
    Nc = tauM/Lam
    Nr = tauM/lam

    p1 = st.poisson(Lam)
    p2 = st.poisson(lam)
    # cluster time of arrival
    tc   = np.cumsum(e1.rvs(Nr))
    tc   = tc[np.where(tc<T)]
    Nc   = len(tc)
    tauc = np.kron(tc,np.ones((1,Nr)))[0,:]
    # rays time of arrival
    taur = np.cumsum(e2.rvs((Nr,Nc)),axis=0).ravel()
    # exponential decays of cluster and rays
    etc = np.exp(-tauc/(1.0*Gam))
    etr = np.exp(-taur/(1.0*gam))
    et = etc*etr
    tau = tauc+taur
    # filtering < T and reordering in delay domain
    tau = tau[np.where(tau<T)]
    et = et[np.where(tau<T)]
    u = np.argsort(tau)
    taus = tau[u]
    ets = et[u]
    # limiting in delay domain
    v = np.where(taus<tauM)[0]
    taus = taus[v]
    ets = ets[v]
    SVir = bs.Bsignal(taus,ets)
    return(SVir)
def test_single_bin():
    conf = test_conf(mc=True, analysis_space=[['x', [-40,40 ]]])

    lf = BinnedLogLikelihood(conf)
    lf.add_rate_parameter('s0')
    lf.prepare()

    # Make a single event at x=0
    lf.set_data(np.zeros(1,
                         dtype=[('x', np.float), ('source', np.int)]))

    assert lf() == stats.poisson(1000).logpmf(1)
    assert lf(s0_rate_multiplier=5.4) == stats.poisson(5400).logpmf(1)
def test_twobin_mc():
    conf = test_conf(mc=True, analysis_space=[['x', [-40, 0, 40]]])

    lf = BinnedLogLikelihood(conf)
    lf.add_rate_parameter('s0')
    lf.prepare()

    # Make 100 events at x=1
    lf.set_data(np.ones(100,
                        dtype=[('x', np.float), ('source', np.int)]))

    assert almost_equal(lf(),
                        stats.poisson(500).logpmf(100) + stats.poisson(500).logpmf(0),
                        1e-2)
def B(x,XW,n1,n2):
    x=np.array(x).reshape((x.shape[0],n1))
    results=np.zeros(x.shape[0])
    parameterLamb=parameterSetsPoisson
    X=XW[0:n1]
    W=XW[n1:n1+n2]
    alpha2=0.5*((kernel.alpha[n1:n1+n2])**2)/scaleAlpha**2
    alpha1=0.5*((kernel.alpha[0:n1])**2)/scaleAlpha**2
    variance0=kernel.variance
    
    logproductExpectations=0.0
    print "veamos B"
    print "x is"
    print x
    print "parameters"
    print parameterLamb
    print "X"
    print X
    print "W"
    print W
    print "alphas,variance"
    print alpha1,alpha2,variance0

    for j in xrange(n2):
        G=poisson(parameterLamb[j])
        temp=G.dist.expect(lambda z: np.exp(-alpha2[j]*((z-W[j])**2)),G.args)
        logproductExpectations+=np.log(temp)
    for i in xrange(x.shape[0]):
        results[i]=logproductExpectations+np.log(variance0)-np.sum(alpha1*((x[i,:]-X)**2))
    return np.exp(results)
Exemple #14
0
def lower_bound(mean, alpha):
    fun = poisson(mu=mean).cdf
    i = 0
    while True:
        if fun(i) > alpha/2.0:
            return max(i - 1, 0)
        i += 1
def plot_poisson():
    fig, ax = plt.subplots(1, 1)

    # This is prediction for Wawrinka in 2014
    mu = 7.869325

    x = np.arange(poisson.ppf(0.01, mu), poisson.ppf(0.999, mu))
    ax.plot(x, poisson.pmf(x, mu), 'wo', ms=8, label='poisson pmf')
    ax.vlines(x, 0, poisson.pmf(x, mu),
              colors=['b', 'b', 'b', 'b', 'b', 'r', 'r', 'r', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g'], lw=5, alpha=0.5)

    rv = poisson(mu)
    ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1, label='frozen pmf')

    plt.title("Stanislas Wawrinka")
    plt.xlabel('# QF+ Finishes in 2014')
    plt.ylabel('Probability')

    prob0 = poisson.cdf(6, mu)
    prob123 = poisson.cdf(9, mu) - poisson.cdf(6, mu)
    probAbove3 = poisson.cdf(10000, mu) - poisson.cdf(9, mu)
    print prob0
    print prob123
    print probAbove3

    plt.show()
def prob_noise_above_th(rate, T, m):
    """Returns the probability that noise is above the burst search threshold.

    Basically is the probability that a poisson process with rate "rate" had
    "m" or more events in a time window "T".
    """
    return poisson(rate*T).sf(m-1)
def test_BeestonBarlowSingleBin():

    instructions_mc = [dict(n_events=32, x=0.5)]
    data, n_mc = make_data(instructions_mc)

    conf = test_conf(default_source_class=FixedSampleSource,
                     events_per_day=32/5,
                     analysis_space=[['x', [0, 1]]],
                     data=data)

    likelihood_config = {'model_statistical_uncertainty_handling': 'bb_single',
                         'bb_single_source': 0}
    lf = BinnedLogLikelihood(conf, likelihood_config=likelihood_config)
    lf.prepare()
    assert lf.n_model_events is not None

    # Make a single event at x=0
    lf.set_data(np.zeros(2, dtype=[('x', np.float), ('source', np.int)]))

    assert lf.n_model_events is not None
    assert almost_equal(28.0814209, beeston_barlow_root2(np.array([32]), 0.2, np.array([1]), np.array([2])))

    # A = beeston_barlow_root2(np.array([32]), 0.2, np.array([0]), np.array([2]))
    A = (2+32)/(1+0.2)

    assert almost_equal(lf(), stats.poisson(0.2*A).logpmf(2))
def fig2():
    '''
        Plot histogram and solve characteristics of probability_cut_nooverlapsability, that we cut fibers.
        Compare with binomial distribution.
        Set n_sim >> 1
    '''
    figure( 2 )
    delta = 0.
    p = probability_cut_nooverlaps( spec.l_x, fib.lf, delta )

    rvb = binom( fib.n, p )
    rvp = poisson( fib.n * p )
    rvn = norm( fib.n * p, sqrt( fib.n * p * ( 1 - p ) ) )

    graph_from = floor( bin_mean - 4 * bin_stdv )
    graph_to = floor( bin_mean + 4 * bin_stdv ) + 1


    x = arange( graph_from , graph_to )
    plot( x, n_sim * rvb.pmf( x ), color = 'red', linewidth = 2, label = 'Binomial' )
    plot( x, n_sim * rvp.pmf( x ), color = 'green', linewidth = 2, label = 'Poisson' )
    plot( x, n_sim * rvn.pdf( x ), color = 'blue', linewidth = 2, label = 'Normal' )
    #plot( x, 20 * rv.pmf( x ) )

    pdf, bins, patches = hist( v, n_sim, normed = 0 ) #, facecolor='green', alpha=1
    #set_xlim( bin_mean - 2 * bin_stdv, bin_mean + 2 * bin_stdv )
    #plot( sx, sy, 'rx' )   # centroids
    #print sum( pdf * diff( bins ) )
    legend()
    draw()
Exemple #19
0
def upper_bound(mean, alpha):
    fun = poisson(mu=mean).cdf
    i = 0
    while True:
        if fun(i) > 1 - alpha/2.0:
            return i
        i += 1
def show_poisson():
    """Show different views of a Poisson distribution"""
    
    fig, ax = plt.subplots(3,1)
    
    k = np.arange(25)
    pd = stats.poisson(10)
    mystyle.set(12)
    
    ax[0].plot(k, pd.pmf(k),'x-')
    ax[0].set_title('Poisson distribition')
    ax[0].set_xticklabels([])
    ax[0].set_ylabel('PMF (X)')
    
    ax[1].plot(k, pd.cdf(k))
    ax[1].set_xlabel('X')
    ax[1].set_ylabel('CDF (X)')
    
    y = np.linspace(0,1,100)
    ax[2].plot(y, pd.ppf(y))
    ax[2].set_xlabel('X')
    ax[2].set_ylabel('PPF (X)')
    
    plt.tight_layout()
    plt.show()
Exemple #21
0
 def __init__(self, stack):
     super(ImpressionViewer, self).__init__()
     self.stack = stack
     self.mean_requests_per_batch = 300
     self.batch_index = 0
     self.batch_size_dist = poisson(self.mean_requests_per_batch)
     self.batch_target = self.batch_size_dist.rvs(1)[0]
def test_multi_bin_single_dim():
    instructions_mc = [dict(n_events=24, x=0.5),
                       dict(n_events=56, x=1.5)]
    data, n_mc = make_data(instructions_mc)

    conf = test_conf(events_per_day=42,
                     analysis_space=[['x', [0, 1, 5]]], default_source_class=FixedSampleSource, data=data)

    lf = BinnedLogLikelihood(conf)
    lf.add_rate_parameter('s0')

    instructions_data = [dict(n_events=18, x=0.5),
                         dict(n_events=70, x=1.5)]
    data, _ = make_data(instructions_data)
    lf.set_data(data)

    mus = [42 / n_mc * instructions_mc[i]['n_events']
           for i in range(len(instructions_mc))]
    seen = [instructions_data[i]['n_events']
            for i in range(len(instructions_data))]

    assert almost_equal(lf(),
                        np.sum([stats.poisson(mu).logpmf(seen_in_bin)
                                for mu, seen_in_bin in zip(mus, seen)]),
                        1e-6)
Exemple #23
0
def generate_hits(n, nstrain, ntarget, ngene, prDecoy, lambdaTrue,
                  lambdaError=0., pFail=0., residual=1e-06,
                  targetProbs=None):
    '''generate lists of hit counts in real targets vs. nontargets in
    multihit screen, using probability of hitting target
    gene(s) conditioned on requiring at least one such hit.
    decoyCounts vectors are offset by poissonRange.nmin'''
    prTarget = PoissonRange(ntarget * lambdaTrue, residual / nstrain, 1)
    prTarget.renormalize() # condition on at least one hit in target region
    targetN = prTarget.sample_totals(nstrain, n)
    if targetProbs: # user-supplied target size vector
        targetProbs = [(p * (1. - pFail)) for p in targetProbs] + [pFail]
    else: # uniform target sizes
        targetProbs = ((1. - pFail) / ntarget,) * ntarget + (pFail,)
    targetProbs = numpy.array(targetProbs)
    if lambdaError:
        poisErr = stats.poisson(nstrain * lambdaError)
        targetNoise = poisErr.rvs((n, ntarget))
    decoyCounts = numpy.random.multinomial(ngene - ntarget,
                                           prDecoy.pmf, size=n)
    for i in xrange(n): # analyze replicates
        targetHits = numpy.random.multinomial(targetN[i], targetProbs)
        if lambdaError:
            hits = targetHits[:ntarget] + targetNoise[i]
        else:
            hits = targetHits[:ntarget]
        nmax = hits.max()
        if nmax < ntarget: # list all counts from 0 to nmax
            targetCounts = [(hits == j).sum() for j in xrange(nmax + 1)]
        else: # use efficient container for sparse list
            targetCounts = HitCountList(hits)
        yield targetCounts, decoyCounts[i]
def mean_variance_demo(reruns=35, maxn=100):
    means = []
    stds = []
    ns = range(2, maxn + 1)
    # use a poisson dist
    mu = 5

    for r in range(reruns):

        if r % 10 == 0:
            print('On rerun {} of {}'.format(r, reruns))

        means.append([])
        stds.append([])

        # show convergence of mean and std
        for n in ns:
            rvs = stats.poisson(mu).rvs(size=n)
            means[r].append( np.mean(rvs) )
            stds[r].append( np.std(rvs) )

    np_means = np.asarray(means)
    np_stds = np.asarray(stds)

    return np_means, np_stds
    def __init__(self):
        """
        Inizialise of the model by default

        """
        super(MLE, self).__init__()

        # Metadata
        self.appliance = None
        self.stats = []
        self.units = None
        self.resistive = False
        self.thDelta = 0
        self.thLikelihood = 0
        self.sample_period = None
        self.sampling_method = None
        # FEATURES:
        self.onpower = {'name': 'gmm', 'model': mixture.GMM(n_components=1)}
        self.offpower = {'name': 'gmm', 'model': mixture.GMM(n_components=1)}
        self.duration = {'name': 'poisson', 'model': poisson(0)}

        # Trainings:
        self.onpower_train = pd.DataFrame(columns=['onpower'])
        self.offpower_train = pd.DataFrame(columns=['offpower'])
        self.duration_train = pd.DataFrame(columns=['duration'])

        # Constrains
        self.powerNoise = 0    # Background noise in the main
        self.powerPair = 0  # Max diff between onpower and offpower
        self.timeWindow = 0        # To avoid high computation
Exemple #26
0
    def AverageUpperLimit(self, bkg):
        """
        For a number of events b, compute the average upper limit. That is:
        UL = Sum Po(n;b) * Upper (n,b)
        """
        ### The Poisson distribution, Po(n;b), is defined only for b>0.
        ### Therefore, this method returns 0 if bkg is negative, and uses
        ### a number close to 0 for the computation if bkg=0.
        if bkg<0.:
            return 0.
        elif bkg==0.:
            bkg=1.E-5

        ### We'll compute the sum in the range [-5sigma, +5sigma] around
        ### the mean, where sigma is the standard deviation of the Poisson
        ### distribution.
        sigma = math.sqrt(bkg)
        nmin = max(0,  int(bkg-5.*sigma))   # Use 0 if nmin<0
        nmax = max(20, int(bkg+5.*sigma)+1) # Use at least 20 for low means
        #print "nmin=%f, nmax=%f" % (nmin,nmax)

        po = poisson(bkg)
        UL = 0.

        for i in range(nmin, nmax):
            pmf = po.pmf(i)
            ul = self.FC.CalculateUpperLimit(i, bkg)
            #print "i=%i, Po(i)=%f, U(i,b)=%f" % (i, pmf, ul)
            UL += po.pmf(i) * self.FC.CalculateUpperLimit(i,bkg)

        return UL
Exemple #27
0
def B(x,XW,n1,n2,kernel,logproductExpectations=None):
    """Computes B(x)=\int\Sigma_{0}(x,w,XW[0:n1],XW[n1:n1+n2])dp(w).
      
       Args:
          x: Vector of points where B is evaluated
          XW: Point (x,w)
          n1: Dimension of x
          n2: Dimension of w
          kernel
          logproductExpectations: Vector with the logarithm
                                  of the product of the
                                  expectations of
                                  np.exp(-alpha2[j]*((z-W[i,j])**2))
                                  where W[i,:] is a point in the history.
          
    """
    x=np.array(x).reshape((x.shape[0],n1))
    results=np.zeros(x.shape[0])
    parameterLamb=parameterSetsPoisson
    X=XW[0:n1]
    inda=n1+n2
    W=XW[n1:inda]
    alpha2=0.5*((kernel.alpha[n1:n1+n2])**2)/scaleAlpha**2
    alpha1=0.5*((kernel.alpha[0:n1])**2)/scaleAlpha**2
    variance0=kernel.variance
    if logproductExpectations is None:
        logproductExpectations=0.0
        for j in xrange(n2):
            G=poisson(parameterLamb[j])
            temp=G.dist.expect(lambda z: np.exp(-alpha2[j]*((z-W[j])**2)),G.args)
            logproductExpectations+=np.log(temp)
    for i in xrange(x.shape[0]):
        results[i]=logproductExpectations+np.log(variance0)-np.sum(alpha1*((x[i,:]-X)**2))
    return np.exp(results)
def show_poisson_views():
    """Show different views of a Poisson distribution"""
    
    sns.set_palette(sns.color_palette('muted'))
    
    fig, ax = plt.subplots(3,1)
    
    k = np.arange(25)
    pd = stats.poisson(10)
    setFonts(12)
    
    ax[0].plot(k, pd.pmf(k),'x-')
    ax[0].set_title('Poisson distribution', fontsize=24)
    ax[0].set_xticklabels([])
    ax[0].set_ylabel('PMF (X)')
    
    ax[1].plot(k, pd.cdf(k))
    ax[1].set_xlabel('X')
    ax[1].set_ylabel('CDF (X)')
    
    y = np.linspace(0,1,100)
    ax[2].plot(y, pd.ppf(y))
    ax[2].set_xlabel('X')
    ax[2].set_ylabel('PPF (X)')
    
    plt.tight_layout()
    plt.show()
	def gen_data(self,erange,step,L=-1):
		if (L < 0):
			L = self.L
		higgs = self.higgs
		beam = self.beam
		seff = self.seff
		isrcorr = self.isrcorr
		b = self.bkg
		x = []
		y = []
		yerr = []
		nstep = int(erange/step)
		self.nstep=nstep
		lstep = L/(2.0*nstep)
		self.lstep=lstep
		s = seff*isrcorr*higgs_smear(higgs,beam,higgs[0])
		for i in range(-nstep,nstep+1):
			ecm = higgs[0] + step*i
			s = seff*isrcorr*higgs_smear(higgs,beam,ecm)
			mu = lstep*(s+b)
			N = stats.poisson(mu).rvs()
			x.append(ecm)
			y.append(N)
			if(N>0):
				yerr.append(math.sqrt(N))
			else:
				yerr.append(1)
		out = [ x, y, yerr ]
		self.x = np.array(x)
		self.y = np.array(y)
		self.yerr = np.array(yerr)
		return out
Exemple #30
0
def _poisson_inputs(data):
    observed = np.asanyarray(data)
    
    #All possible frequencies from [min(observed) to max(observed)]
    #Those who are not in observed, have frequency = 0. The frequency
    #of a value is is accessed by all_freq[value].
    all_freqs = np.bincount(observed)
    all_values = np.arange(len(all_freqs))
    
    #Estimating the mean of the Poisson
    aux = (all_freqs * all_values).sum()
    total = all_freqs.sum()
    estimated_mean = aux / total

    #Computes expected frequencies in ascending order of the values
    #First for all values till the one before the last
    dist = stats.poisson(estimated_mean)
    probabilites = np.apply_along_axis(dist.pmf, 0, all_values[:-1])
    last_value = all_values[-1]
    
    #Add greater or equal last one
    geq_last = dist.sf(last_value) + dist.pmf(last_value)
    probabilites = np.append(probabilites, geq_last)
    sum_probs = probabilites.sum()

    #Now the arrays are matched (each index is the frequency of the same value)
    expected_freqs = total * probabilites
    
    return all_freqs, expected_freqs