예제 #1
0
def test2():
    mu = 10.0
    sigma = 2.0

    x = Variable("x", float)
    loggauss = -0.5 * math.log(2.0 * math.pi * sigma * sigma) - 0.5 * (
        (x - mu)**2) / (sigma * sigma)

    f = Function(name="foo", params=(x, ), rettype=float, expr=loggauss)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))

    samples = metropolis_hastings(func_ptr, sigma, 0.0, 1000, 2)
    #plt.plot(np.arange(len(samples)), samples)

    n, bins, patches = plt.hist(samples[500:],
                                25,
                                normed=1,
                                histtype='stepfilled')
    plt.setp(patches, 'facecolor', 'g', 'alpha', 0.75)

    # add a line showing the expected distribution
    y = plt.normpdf(bins, mu, sigma)
    l = plt.plot(bins, y, 'k--', linewidth=1.5)
    plt.show()
예제 #2
0
파일: barebones.py 프로젝트: stephentu/xpr
def test2():
    mu = 10.0
    sigma = 2.0

    x = Variable("x", float)
    loggauss = -0.5 * math.log( 2.0 * math.pi * sigma * sigma ) - 0.5 * ((x - mu) ** 2) / (sigma * sigma)

    f = Function(
            name="foo",
            params=(x,),
            rettype=float,
            expr=loggauss)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))

    samples = metropolis_hastings(func_ptr, sigma, 0.0, 1000, 2)
    #plt.plot(np.arange(len(samples)), samples)

    n, bins, patches = plt.hist(samples[500:], 25, normed=1, histtype='stepfilled')
    plt.setp(patches, 'facecolor', 'g', 'alpha', 0.75)

    # add a line showing the expected distribution
    y = plt.normpdf(bins, mu, sigma)
    l = plt.plot(bins, y, 'k--', linewidth=1.5)
    plt.show()
예제 #3
0
def test1():

    x = Variable("x", float)
    f = Function(name="foo", params=(x, ), rettype=float, expr=x**2)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    call_and_print_result(func_ptr)
예제 #4
0
def test4():
    x = Variable("x", float)
    f = Function(name="foo", params=(x, ), rettype=float, expr=x**2)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double)(func_ptr)

    assert almost_eq(cfunc(23.45), 23.45 * 23.45)
예제 #5
0
def test2():
    x = Variable("x", int)
    y = Variable("y", int)
    f = Function(name="foo", params=(x, y), rettype=int, expr=10 * x - y)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_longlong, c_longlong, c_longlong)(func_ptr)
    assert cfunc(5, 3) == 10 * 5 - 3
예제 #6
0
def test1():
    x = Variable("x", float)
    y = Variable("y", float)
    f = Function(name="foo", params=(x, y), rettype=float, expr=10 * x + y)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double, c_double)(func_ptr)
    assert almost_eq(cfunc(1.0, 3.5), 10 * 1.0 + 3.5)
예제 #7
0
파일: barebones.py 프로젝트: stephentu/xpr
def test1():

    x = Variable("x", float)
    f = Function(
            name="foo",
            params=(x,),
            rettype=float,
            expr=x ** 2)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    call_and_print_result(func_ptr)
예제 #8
0
def test3():
    x = Variable("x", float)
    f = Function(name="foo",
                 params=(x, ),
                 rettype=float,
                 expr=10 * Log(x) + Exp(x) + 1)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double)(func_ptr)

    assert almost_eq(cfunc(3.5), 10.0 * math.log(3.5) + math.exp(3.5) + 1.0)
예제 #9
0
파일: prog.py 프로젝트: stephentu/xpr
def test4():
    x = Variable("x", float)
    f = Function(
            name="foo",
            params=(x,),
            rettype=float,
            expr=x ** 2)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double)(func_ptr)

    assert almost_eq(cfunc(23.45), 23.45 * 23.45)
예제 #10
0
파일: prog.py 프로젝트: stephentu/xpr
def test3():
    x = Variable("x", float)
    f = Function(
            name="foo",
            params=(x,),
            rettype=float,
            expr=10 * Log(x) + Exp(x) + 1)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double)(func_ptr)

    assert almost_eq(cfunc(3.5), 10.0 * math.log(3.5) + math.exp(3.5) + 1.0)
예제 #11
0
파일: prog.py 프로젝트: stephentu/xpr
def test2():
    x = Variable("x", int)
    y = Variable("y", int)
    f = Function(
            name="foo",
            params=(x, y),
            rettype=int,
            expr=10 * x - y)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_longlong, c_longlong, c_longlong)(func_ptr)
    assert cfunc(5, 3) == 10 * 5 - 3
예제 #12
0
파일: prog.py 프로젝트: stephentu/xpr
def test1():
    x = Variable("x", float)
    y = Variable("y", float)
    f = Function(
            name="foo",
            params=(x, y),
            rettype=float,
            expr=10 * x + y)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double, c_double)(func_ptr)
    assert almost_eq(cfunc(1.0, 3.5), 10 * 1.0 + 3.5)
예제 #13
0
def test5():
    # logpdf of Gaussian
    mu = 10.0
    sigma = 2.0

    x = Variable("x", float)
    loggauss = -0.5 * math.log(2.0 * math.pi * sigma * sigma) - 0.5 * (
        (x - mu)**2) / (sigma * sigma)
    f = Function(name="foo", params=(x, ), rettype=float, expr=loggauss)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double)(func_ptr)

    from scipy.stats import norm
    assert almost_eq(cfunc(5.0), norm.logpdf(5.0, loc=mu, scale=sigma))
예제 #14
0
파일: prog.py 프로젝트: stephentu/xpr
def test5():
    # logpdf of Gaussian
    mu = 10.0
    sigma = 2.0

    x = Variable("x", float)
    loggauss = -0.5 * math.log( 2.0 * math.pi * sigma * sigma ) - 0.5 * ((x - mu) ** 2) / (sigma * sigma)
    f = Function(
            name="foo",
            params=(x,),
            rettype=float,
            expr=loggauss)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double)(func_ptr)

    from scipy.stats import norm
    assert almost_eq(cfunc(5.0), norm.logpdf(5.0, loc=mu, scale=sigma))