Example #1
0
def test_two_gaussian_nest():
    np.random.seed(0)

    # gaussians centered at (1, 1) and (-1, -1)
    mu1 = np.ones(2)
    mu2 = -np.ones(2)

    # Width of 0.1 in each dimension
    sigma = 0.1
    ivar = 1.0/(sigma*sigma)
    sigma1inv = np.diag([ivar, ivar])
    sigma2inv = np.diag([ivar, ivar])

    def logl(x):
        dx1 = x - mu1
        dx2 = x - mu2
        return np.logaddexp(-np.dot(dx1, np.dot(sigma1inv, dx1))/2.0,
                            -np.dot(dx2, np.dot(sigma2inv, dx2))/2.0)

    # Use a flat prior, over [-5, 5] in both dimensions
    def prior(x):
        return 10.0 * x - 5.0

    res = nestle.nest(logl, prior, 2, nobj=100)

    # (Approximate) analytic evidence for two identical Gaussian blobs,
    # over a uniform prior [-5:5][-5:5] with density 1/100 in this domain:
    analytic_logz = np.log(2.0 * 2.0*np.pi*sigma*sigma / 100.0)

    # Note that this is a terrible test and only works for this
    # specific random seed.
    assert abs(res.logz - analytic_logz) < res.logzerr
Example #2
0
def test_two_gaussian_nest():
    # (Approximate) analytic evidence for two identical Gaussian blobs,
    # over a uniform prior [-5:5][-5:5] with density 1/100 in this domain:
    analytic_evidence = np.log(2.0 * 2.0 * np.pi * sigma * sigma / 100.0)

    res = nestle.nest(logl, prior, 2, nobj=100)

    print "logz =", res.logz, " +/- ", res.logzerr
    print "analytic", analytic_evidence
Example #3
0
	return -0.5*(np.sum((y-model)**2/yerr**2))

def prior(theta):
    return np.array([10.0, 20.0])*theta+np.array([-5.0, -10.0])

def prior1(x):
    """

	Uniform prior, this maps x=[0, 1) to m, c  in -5, 5
    """
    return 10.0*x-5.0
	
start=time()

#perform sampling
res = nt.nest(lnlike, prior, 2,  nobj=1000, maxiter=100000)

#perform sampling for different model
res_poly=nt.nest(lnlike_poly, prior1, 3,  nobj=1000, maxiter=100000)

end=time()

#prints output, can compare evidence

print "\nEvidence is:", res.logz
print "Best fit slope value:\t ",	np.median(res.samples[:,0]), np.median(res.samples[:,1])
print "\nEvidence for degree 2 polynomial is: \t", res_poly.logz

print "\nTime it took (in seconds) \t:", end-start

#use the fabulous triangle plot machinery to show the samples 
Example #4
0
		a=z*(1-q0)/(np.sqrt(1+2*q0*z)+1+q0*z )
		dl=(1+a)*c*z/h0
		return dl
	except:
		return np.inf

def lnlikel(theta):
	"""
	Define chi_sq likelihood
	
	"""
	
	sn1=sn[2:3]
	z=sn1[:,0]; mag=sn1[:,1]; sig=sn1[:,2]
	
	#dl=lum_dist(z, theta[0], theta[1])
	
	#uses cosmocalc distance measurement
	#model=dist.mod(z)
	model= z**2
	return -0.5*np.sum((mag-model)**2/sig**2.)

def prior(u):
    """
    define prior on omega_lam and omega_m
    """
    return 2.0*u
    
    
res=nt.nest(lnlikel, prior, 2)