def test_var(func, vals): x = pb.Variable('x', vtype=float) y = pb.Variable('y', vtype=float) a = pb.Variable('a', vtype=float) b = pb.Variable('b', vtype=float) xy = x & y ab = a & b functional = pb.Functional(ab, xy) functional.add_func(a, func[0](x[:])) functional.add_func(b, func[1](y[:])) eval_a = functional[a](vals[0]) eval_b = functional[b](vals[1]) assert np.isclose(eval_a, vals[1]) assert np.isclose(eval_b, vals[0])
def test_inc(inp, out): # Sympy x = pb.Variable('x', vtype=float, vset=[zero, inf]) x.set_ufun(x[:] + 1.) output = x.ufun[0](inp) assert ismatch(out, output), \ "Observed/expected match {}/{}".format(output, out) output = x.ufun[-1](output) assert ismatch(inp, output), \ "Observed/expected match {}/{}".format(output, inp) # Numpy y = pb.Variable('y', vtype=float, vset=[zero, inf]) y.set_ufun((lambda z: z + 1, lambda z: z - 1)) output = y.ufun[0](inp) assert ismatch(out, output), \ "Observed/expected match {}/{}".format(output, out) output = y.ufun[-1](output) assert ismatch(inp, output), \ "Observed/expected match {}/{}".format(output, inp)
def test_log(inp, out): # Sympy x = pb.Variable('x', vtype=float, vset=[zero, inf]) x.set_ufun(sympy.log(~x)) output = x.ufun[0](inp) assert ismatch(out, output), \ "Observed/expected match {}/{}".format(output, out) output = x.ufun[-1](output) assert ismatch(inp, output), \ "Observed/expected match {}/{}".format(output, inp) # Numpy y = pb.Variable('y', vtype=float, vset=[zero, inf]) y.set_ufun((np.log, np.exp)) output = y.ufun[0](inp) assert ismatch(out, output), \ "Observed/expected match {}/{}".format(output, out) output = y.ufun[-1](output) assert ismatch(inp, output), \ "Observed/expected match {}/{}".format(output, inp)
def test_ran(ran, val): # Sympy x = pb.Variable('x', vtype=float, vset=ran) x.set_ufun(sympy.log(x[:])) vals = x(val)[x.name] assert np.max(vals) <= x.vlims[1] and np.min(vals) >= x.vlims[0] # Numpy y = pb.Variable('y', vtype=float, vset=ran) y.set_ufun((np.log, np.exp)) vals = y(val)[y.name] assert np.max(vals) <= x.vlims[1] and np.min(vals) >= x.vlims[0] # Delta steps = int(abs(list(val)[0])) value = np.mean(vals) y.set_delta([1], bound=True) vals = np.empty(steps, dtype=float) for i in range(steps): vals[i] = y.apply_delta(value) value = vals[i] assert np.max(vals) <= x.vlims[1] and np.min(vals) >= x.vlims[0]
def test_log(inp, out): for use_variable in False, True: if not use_variable: x = sy.Symbol('x') x_expr = pb.Expr(sy.log(x)) output = x_expr({'x': inp}) else: y = pb.Variable('y', vtype=float) y_expr = pb.Expr(sy.log(~y)) output = y_expr({'y': inp}) close = np.isclose(output, out) if isinstance(inp, np.ndarray): assert np.all(close), "Output values not as expected" else: assert close, "Output value {} not as expected {}".format( output, out)
# Change of variables example import collections import probayes as pb import numpy as np from pylab import * ion() num_samples = 500 x = pb.Variable('x', vtype=float, vset=[0, 1]) y = pb.Variable('y', vtype=float, vset=[0, 1]) s = pb.Variable('s', vtype=float) d = pb.Variable('d', vtype=float) xy = x & y sd = s & d sd_from_xy = sd | xy sd_from_xy.func.add_func(s[:], x[:] + y[:]) sd_from_xy.func.add_func(d[:], x[:] - y[:])
# Example of sampling from a Zipfian probability density function import numpy as np from pylab import * ion() import probayes as pb zipf_range = [0.1, 10.] set_size = {-10000} # size negation denotes random sampling var = pb.Variable("var", zipf_range) var.set_ufun((np.log, np.exp)) samples = var(set_size) figure() hist(samples[var.name], 100)