コード例 #1
0
def test_continuous_mh_exp():
    """
    Test on a distribution with support on the positive reals
    """

    def dens(x): 
        #mixture of gaussian
        lamb = 2.47
        if x < 0:
            return -np.inf
        else:
            return -x * lamb
        
        # return util.log_norm_dens(x, 0, 1.0)

    rng = irm.RNG()
    ITERS = 1000000
    
    x = 0
    results = np.zeros(ITERS)
    
    for i in range(ITERS):
        x = irm.continuous_mh_sample(x, dens, rng, 10, -4, 4)
        results[i] = x
    MIN = -1
    MAX = 4
    BINS = 101
    x = np.linspace(MIN, MAX, BINS)
    bin_width = x[1] - x[0]

    y = [dens(a + bin_width/2) for a in x[:-1]]
    p = np.exp(y)
    p = p/np.sum(p)/(x[1]-x[0])


    hist, bin_edges = np.histogram(results, x, normed=True)

    kl=  util.kl(hist, p)
    assert kl < 0.1
コード例 #2
0
def test_cont_mh_normal():
    def dens(x): 
        #mixture of gaussian
        mus = [-1.5, 2]
        vars = [1, 1]
        pis = [0.25, 0.75]
        return np.logaddexp.accumulate([(np.log(pi)  +  util.log_norm_dens(x, mu, var)) for (pi, mu, var) in zip(pis, mus, vars)])[-1]
        # return util.log_norm_dens(x, 0, 1.0)

    rng = irm.RNG()
    ITERS = 100000
    
    x = 0
    results = np.zeros(ITERS)
    
    for i in range(ITERS):
        x = irm.continuous_mh_sample(x, dens, rng, 10, -4, 4)
        results[i] = x
    MIN = -5
    MAX = 5
    BINS = 100
    x = np.linspace(MIN, MAX, BINS)
    bin_width = x[1] - x[0]

    y = [dens(a + bin_width/2) for a in x[:-1]]
    p = np.exp(y)
    p = p/np.sum(p)/(x[1]-x[0])


    hist, bin_edges = np.histogram(results, x, normed=True)
    print hist
    print p
    pylab.plot(p)
    pylab.plot(hist)
    pylab.show()
    kl=  util.kl(hist, p)
    assert kl < 0.1