Ejemplo n.º 1
0
def upperLimitFunc(A, optstat_ref, nreal):
    """
    Compute the value of the Optimal Statistic for different signal realizations
    
    @param A: value of GWB amplitude
    @param optstat_ref: value of optimal statistic with no injection 
    @param nreal: number of realizations

    """
    count = 0
    for ii in range(nreal):
        
        # create residuals
        inducedRes = PALutils.createGWB(psr, A, 4.3333)

        Pinvr = []
        Pinv = []
        for ct, p in enumerate(psr):

            # replace residuals in pulsar object
            p.res = res[ct] + np.dot(R[ct], inducedRes[ct])

            # determine injected amplitude by minimizing Likelihood function
            c = np.dot(Dmatrix[ct], p.res)
            f = lambda x: -PALutils.twoComponentNoiseLike(x, D[ct], c)
            fbounded = minimize_scalar(f, bounds=(0, 1e-14, 3.0e-13), method='Brent')
            Amp = np.abs(fbounded.x)
            #print Amp
            #Amp = A

            # construct P^-1 r
            Pinvr.append(c/(Amp**2 * D[ct] + 1))
            Pinv.append(1/(Amp**2 * D[ct] + 1))

        # construct optimal statstic
        k = 0
        top = 0
        bot = 0
        for ll in range(npsr):
            for kk in range(ll+1, npsr):

                # compute numerator of optimal statisic
                top += ORF[k]/2 * np.dot(Pinvr[ll], np.dot(SIJ[k], Pinvr[kk]))

                # compute trace term
                bot += (ORF[k]/2)**2 * np.trace(np.dot((Pinv[ll]*SIJ[k].T).T, (Pinv[kk]*SIJ[k]).T))
                # iterate counter 
                k += 1

        # get optimal statistic and SNR
        optStat = top/bot
        snr = top/np.sqrt(bot)
        
        # check to see if larger than in real data
        if optStat > optstat_ref:
            count += 1


    # now get detection probability
    detProb = count/nreal

    print A, detProb
    injAmp.append(A)
    injDetProb.append(detProb)

    return detProb - 0.95