Exemplo n.º 1
0
def likelihood_wrapper(theta, timeBins, N, freqs, z_bins, z_logcdf, z_logsf,
                       ancGLs, ancHapGLs, gens, noCoals, currFreq, h, sMax,
                       changePts):
    S = theta
    Sprime = np.concatenate((S, [0.0]))
    if np.any(np.abs(Sprime) > sMax):
        return np.inf

    sel = Sprime[np.digitize(epochs, timeBins, right=False) - 1]

    tShape = times.shape
    if tShape[2] == 0:
        t = np.zeros((2, 0))
        importanceSampling = False
    elif tShape[2] == 1:
        t = times[:, :, 0]
        importanceSampling = False
    else:
        importanceSampling = True

    if importanceSampling:
        M = tShape[2]
        loglrs = np.zeros(M)
        for i in range(M):
            betaMat = backward_algorithm(sel,
                                         times[:, :, i],
                                         epochs,
                                         N,
                                         freqs,
                                         z_bins,
                                         z_logcdf,
                                         z_logsf,
                                         ancGLs,
                                         ancHapGLs,
                                         changePts,
                                         noCoals=noCoals,
                                         currFreq=currFreq,
                                         h=h)
            logl = logsumexp(betaMat[-2, :])
            logl0 = proposal_density(times[:, :, i], epochs, N)
            loglrs[i] = logl - logl0
        logl = -1 * (-np.log(M) + logsumexp(loglrs))
    else:
        betaMat = backward_algorithm(sel,
                                     t,
                                     epochs,
                                     N,
                                     freqs,
                                     z_bins,
                                     z_logcdf,
                                     z_logsf,
                                     ancGLs,
                                     ancHapGLs,
                                     changePts,
                                     noCoals=noCoals,
                                     currFreq=currFreq,
                                     h=h)
        logl = -logsumexp(betaMat[-2, :])
    #print(logl,S)
    return logl
Exemplo n.º 2
0
def traj_wrapper(theta,timeBins,N,freqs,z_bins,z_logcdf,z_logsf,ancGLs,ancHapGLs,gens,noCoals,currFreq,h,sMax,changePts):
    S = theta
    Sprime = np.concatenate((S,[0.0]))
    if np.any(np.abs(Sprime) > sMax):
        print('WARNING: selection coefficient exceeds bounds. Maybe change --sMax?')
        return np.inf

    sel = Sprime[np.digitize(epochs,timeBins,right=False)-1]
    T = len(epochs)
    F = len(freqs)
    tShape = times.shape
    if tShape[2] == 0:
    	t = np.zeros((2,0))
    	importanceSampling = False
    elif tShape[2] == 1:
    	t = times[:,:,0]
    	importanceSampling = False
    else:
    	importanceSampling = True

    if importanceSampling:
    	M = tShape[2]
    	loglrs = np.zeros(M)
    	postBySamples = np.zeros((F,T-1,M))
    	for i in range(M):
    		betaMat = backward_algorithm(sel,times[:,:,i],epochs,N,freqs,z_bins,z_logcdf,z_logsf,ancGLs,ancHapGLs,changePts,noCoals=noCoals,currFreq=currFreq,h=h)
    		alphaMat = forward_algorithm(sel,times[:,:,i],epochs,N,freqs,z_bins,z_logcdf,z_logsf,ancGLs,ancHapGLs,changePts,noCoals=noCoals,h=h)
    		logl = logsumexp(betaMat[-2,:])
    		logl0 = proposal_density(times[:,:,i],epochs,N)
    		loglrs[i] = logl-logl0
    		postBySamples[:,:,i] = (alphaMat[1:,:] + betaMat[:-1,:]).transpose()
    	post = logsumexp(loglrs + postBySamples,axis=2)
    	post -= logsumexp(post,axis=0)

    else:
    	post = np.zeros((F,T))
    	betaMat = backward_algorithm(sel,t,epochs,N,freqs,z_bins,z_logcdf,z_logsf,ancGLs,ancHapGLs,changePts,noCoals=noCoals,currFreq=currFreq,h=h)
    	alphaMat = forward_algorithm(sel,t,epochs,N,freqs,z_bins,z_logcdf,z_logsf,ancGLs,ancHapGLs,changePts,noCoals=noCoals,h=h)
    	post = (alphaMat[1:,:] + betaMat[:-1,:]).transpose()
    	post -= logsumexp(post,axis=0)
    return post