def simulate(self, args): vals = args.operandValues() alpha = vals[0] os = vals[1] if len(vals) > 1 \ else [VentureInteger(i) for i in range(len(alpha))] if len(os) != len(alpha): raise VentureValueError( "Set of objects to choose from is the wrong length") output = TypedPSP(CDirCatOutputPSP(alpha, os), SPType([], t.AnyType())) return VentureSPRecord( DirCatSP(NullRequestPSP(), output, alpha, len(alpha)))
def simulate(self, args): vals = args.operandValues() (alpha, n) = (float(vals[0]), int(vals[1])) os = vals[2] if len(vals) > 2 else [ VentureInteger(i) for i in range(n) ] if len(os) != n: raise VentureValueError( "Set of objects to choose from is the wrong length") output = TypedPSP(CSymDirCatOutputPSP(alpha, n, os), SPType([], t.AnyType())) return VentureSPRecord(DirCatSP(NullRequestPSP(), output, alpha, n))
def simulate(self, args): vals = args.operandValues() alpha = vals[0] n = len(alpha) os = vals[1] if len(vals) > 1 else [ VentureInteger(i) for i in range(n) ] if len(os) != n: raise VentureValueError( "Set of objects to choose from is the wrong length") theta = args.np_prng().dirichlet(alpha) output = TypedPSP(UDirCatOutputPSP(theta, os), SPType([], t.AnyType())) return VentureSPRecord(DirCatSP(NullRequestPSP(), output, alpha, n))
def simulate(self, _trace, args): vals = args.operandValues() alpha = vals[0] os = vals[1] if len(vals) > 1 \ else [VentureInteger(i) for i in range(len(alpha))] madeaux = args.madeSPAux() assert isinstance(madeaux, DirCatSPAux) counts = [count + a for (count, a) in zip(madeaux.counts, alpha)] newTheta = args.np_prng().dirichlet(counts) output = TypedPSP(UDirCatOutputPSP(newTheta, os), SPType([], t.AnyType())) return VentureSPRecord( DirCatSP(NullRequestPSP(), output, alpha, len(alpha)), madeaux)
def simulate(self, _trace, args): vals = args.operandValues() (alpha, n) = (float(vals[0]), int(vals[1])) os = vals[2] if len(vals) > 2 else [ VentureInteger(i) for i in range(n) ] madeaux = args.madeSPAux() assert isinstance(madeaux, DirCatSPAux) counts = [count + alpha for count in madeaux.counts] newTheta = args.np_prng().dirichlet(counts) output = TypedPSP(USymDirCatOutputPSP(newTheta, os), SPType([], t.AnyType())) return VentureSPRecord(DirCatSP(NullRequestPSP(), output, alpha, n), madeaux)
def record(tag, arity): typ = RecordType(tag) tester = sp_help.type_test(typ) constructor = sp_help.deterministic_typed(lambda *fields: VentureRecord(tag, fields), [t.AnyType()] * arity, typ, descr="%s" + " constructs a %s record" % tag) def accessor_func(r, i): if r in typ: return r.fields[i] else: raise VentureTypeError("Accessor for field %s expected record of type %s but got %s" % (i, tag, r)) def accessor(i): return sp_help.deterministic_typed(lambda r: accessor_func(r, i), [typ], t.AnyType(), descr="%s" + " extracts the %s field of a %s record" % (i, tag)) return (tester, constructor, [accessor(i) for i in range(arity)])
def __venture_start__(ripl): ripl.execute_program(''' assume gp_cov_wn = (c) -> { gp_cov_scale(c, gp_cov_bump(1e-9, 1e-11)) }; define gp_cov_wn = (c) -> { gp_cov_scale(c, gp_cov_bump(1e-9, 1e-11)) }; ''') ripl.bind_foreign_inference_sp( 'sort', deterministic_typed(np.sort, [ vt.ArrayUnboxedType(vt.NumberType()), ], vt.ArrayUnboxedType(vt.NumberType()), min_req_args=1)) ripl.bind_foreign_inference_sp( 'get_mean', deterministic_typed(np.mean, [ vt.ArrayUnboxedType(vt.NumberType()), ], vt.NumberType(), min_req_args=1)) ripl.bind_foreign_inference_sp( 'load_csv', deterministic_typed(load_csv, [vt.StringType()], vt.ArrayUnboxedType(vt.NumberType()), min_req_args=1)) ripl.bind_foreign_sp( 'compile_ast_to_venturescript', deterministic_typed(compile_ast_to_embedded_dsl, [vt.AnyType()], vt.StringType(), min_req_args=1)) ripl.bind_foreign_sp( 'eval_expr', deterministic_typed(interpret_embedded_dsl, [vt.StringType()], gp.gpType, min_req_args=1))
### Inference SPs relating to printing, plotting and datasets def print_fun(*args): def convert_arg(arg): if isinstance(arg, v.VentureForeignBlob) and \ isinstance(arg.getForeignBlob(), Dataset): return arg.getForeignBlob().asPandas() else: return arg if len(args) == 1: print convert_arg(args[0]) else: print " ".join([str(convert_arg(a)) for a in args]) inf.registerBuiltinInferenceSP("print", deterministic_typed(print_fun, [t.AnyType()], t.NilType(), variadic=True, descr="""\ Print the given values to the terminal. If you are trying to add a debugging print statement to a VentureScript expression that is not already an inference action, consider using `debug`, which does not require sequencing. """)) def plot_fun(spec, dataset): spec = t.ExpressionType().asPython(spec) if isinstance(dataset, Dataset): PlotSpec(spec).plot(dataset.asPandas(), dataset.ind_names) else: # Assume a raw data frame PlotSpec(spec).plot(dataset, list(dataset.columns.values))
def logDensityOfData(self, aux): assert isinstance(aux, DirCatSPAux) N = aux.counts.total A = self.alpha.total term1 = scipy.special.gammaln(A) - scipy.special.gammaln(N + A) term2 = sum([ scipy.special.gammaln(alpha + count) - scipy.special.gammaln(alpha) for (alpha, count) in zip(self.alpha, aux.counts) ]) return term1 + term2 registerBuiltinSP("make_dir_cat", \ typed_nr(MakerCDirCatOutputPSP(), [t.HomogeneousArrayType(t.PositiveType()), t.ArrayType()], SPType([], t.AnyType()), min_req_args=1)) #### Uncollapsed dirichlet categorical class MakerUDirCatOutputPSP(RandomPSP): def childrenCanAAA(self): return True def getAAALKernel(self): return UDirCatAAALKernel(self) def simulate(self, args): vals = args.operandValues() alpha = vals[0] n = len(alpha)
def canAbsorb(self, _trace, appNode, parentNode): return parentNode != appNode.operandNodes[2] @override(DeterministicPSP) def description(self,name): return "%s returns its third argument unchanged at runtime, " \ "but tags the subexpression creating the object as being " \ "within the given scope and block." % name def isTagOutputPSP(thing): return isinstance(thing, TagOutputPSP) or \ (isinstance(thing, TypedPSP) and isTagOutputPSP(thing.psp)) registerBuiltinSP("tag", typed_nr(TagOutputPSP(), [t.AnyType("<scope>"), t.AnyType("<block>"), t.AnyType()], t.AnyType())) class TagExcludeOutputPSP(DeterministicPSP): @override(DeterministicPSP) def simulate(self,args): return args.operandValues()[1] @override(DeterministicPSP) def gradientOfSimulate(self, _args, _value, direction): return [0, direction] @override(DeterministicPSP) def canAbsorb(self, _trace, appNode, parentNode): return parentNode != appNode.operandNodes[1]
registerBuiltinSP( "any_p", deterministic_typed( any, [t.HomogeneousListType(t.BoolType())], t.BoolType(), descr="any returns true if any of the elements in the input are true")) registerBuiltinSP("is_number", type_test(t.NumberType())) registerBuiltinSP("is_integer", type_test(t.IntegerType())) registerBuiltinSP("is_probability", type_test(t.ProbabilityType())) registerBuiltinSP("is_atom", type_test(t.AtomType())) registerBuiltinSP("is_boolean", type_test(t.BoolType())) registerBuiltinSP("is_symbol", type_test(t.SymbolType())) registerBuiltinSP("is_procedure", type_test(SPType([t.AnyType()], t.AnyType(), variadic=True))) def grad_list(args, direction): if direction == 0: return [0 for _ in args] else: (list_, tail) = direction.asPossiblyImproperList() assert tail is None or tail == 0 or tail == v.VentureInteger(0) tails = [0 for _ in range(len(args) - len(list_))] return list_ + tails registerBuiltinSP( "list", deterministic_typed(lambda *args: args, [t.AnyType()],
from numbers import Number import numpy as np from venture.lite.exception import VentureValueError from venture.lite.sp_help import deterministic_typed from venture.lite.sp_help import type_test from venture.lite.sp_registry import registerBuiltinSP import venture.lite.value as vv import venture.lite.types as t import venture.lite.utils as u registerBuiltinSP("array", deterministic_typed(lambda *args: np.array(args), [t.AnyType()], t.ArrayType(), variadic=True, sim_grad=lambda args, direction: direction.getArray(), descr="array returns an array initialized with its arguments")) registerBuiltinSP("vector", deterministic_typed(lambda *args: np.array(args), [t.NumberType()], t.ArrayUnboxedType(t.NumberType()), variadic=True, sim_grad=lambda args, direction: direction.getArray(), descr="vector returns an unboxed numeric array initialized with its arguments")) registerBuiltinSP("is_array", type_test(t.ArrayType())) registerBuiltinSP("is_vector", type_test(t.ArrayUnboxedType(t.NumberType()))) registerBuiltinSP("to_array", deterministic_typed(lambda seq: seq.getArray(), [t.HomogeneousSequenceType(t.AnyType())], t.ArrayType(),
exp = [operator] + operands env = VentureEnvironment() return Request([ESR(args.node, exp, addr.req_frame(0), env)]) def description(self, name): return "%s(func, vals) returns the result of applying a variadic" \ " function to an array of operands" % name registerBuiltinSP( "apply", esr_output( TypedPSP( ApplyRequestPSP(), SPType([ SPType([t.AnyType("a")], t.AnyType("b"), variadic=True), t.HomogeneousArrayType(t.AnyType("a")) ], t.RequestType("b"))))) class ArrayMapRequestPSP(DeterministicPSP): def simulate(self, args): (operator, operands) = args.operandValues() exps = [[operator, e.quote(operand)] for operand in operands] env = VentureEnvironment() return Request([ ESR((args.node, i), exp, addr.req_frame(i), env) for i, exp in enumerate(exps) ]) def description(self, name):
typed_func(lambda args: env.VentureEnvironment(), [], env.EnvironmentType(), descr="get_empty_environment returns the empty environment")) registerBuiltinSP("is_environment", type_test(env.EnvironmentType())) class ExtendEnvOutputPSP(DeterministicPSP): def simulate(self,args): (en, sym, _) = args.operandValues() node = args.operandNodes[2] return env.VentureEnvironment(en,[sym],[node]) def description(self,name): return "%s returns an extension of the given environment where the given symbol is bound to the given object" % name registerBuiltinSP("extend_environment", typed_nr(ExtendEnvOutputPSP(), [env.EnvironmentType(), t.SymbolType(), t.AnyType()], env.EnvironmentType())) class EvalRequestPSP(DeterministicPSP): def simulate(self,args): (exp, en) = args.operandValues() # Point to the desugared source code location of expression. # This is not a full address, because the call stack is gone. source_loc = addr.append(addr.top_frame(args.operandNodes[0].address), 1) return Request([ESR(args.node,exp,source_loc,en)]) def description(self,name): return "%s evaluates the given expression in the given environment and returns the result. Is itself deterministic, but the given expression may involve a stochasitc computation." % name registerBuiltinSP("eval", esr_output(TypedPSP(EvalRequestPSP(), SPType([t.ExpressionType(), env.EnvironmentType()],
class MakeMSPOutputPSP(DeterministicPSP): def simulate(self, args): sharedOperatorNode = args.operandNodes[0] return VentureSPRecord( SP(MSPRequestPSP(sharedOperatorNode), ESRRefOutputPSP())) def description(self, name): return "%s returns the stochastically memoized version of the input SP." % name class MSPRequestPSP(DeterministicPSP): def __init__(self, sharedOperatorNode): self.sharedOperatorNode = sharedOperatorNode def simulate(self, args): vals = args.operandValues() id = str(vals) exp = ["memoizedSP"] + [["quote", val] for val in vals] env = VentureEnvironment(None, ["memoizedSP"], [self.sharedOperatorNode]) return Request([ESR(id, exp, addr.req_frame(id), env)]) registerBuiltinSP( "mem", typed_nr(MakeMSPOutputPSP(), [SPType([t.AnyType("a")], t.AnyType("b"), variadic=True)], SPType([t.AnyType("a")], t.AnyType("b"), variadic=True)))
def accessor(i): return sp_help.deterministic_typed(lambda r: accessor_func(r, i), [typ], t.AnyType(), descr="%s" + " extracts the %s field of a %s record" % (i, tag))
def testEquality(): checkTypedProperty(propEquality, t.AnyType())
def convert_arg(arg): if isinstance(arg, v.VentureForeignBlob) and \ isinstance(arg.getForeignBlob(), Dataset): return arg.getForeignBlob().asPandas() else: return arg if len(args) == 1: print convert_arg(args[0]) else: print " ".join([str(convert_arg(a)) for a in args]) inf.registerBuiltinInferenceSP( "print", deterministic_typed(print_fun, [t.AnyType()], t.NilType(), variadic=True, descr="""\ Print the given values to the terminal. If you are trying to add a debugging print statement to a VentureScript expression that is not already an inference action, consider using `debug`, which does not require sequencing. """)) def plot_fun(spec, dataset): spec = t.ExpressionType().asPython(spec) if isinstance(dataset, Dataset): PlotSpec(spec).plot(dataset.asPandas(), dataset.ind_names)
def type_test(tp): return deterministic_typed(lambda thing: thing in tp, [t.AnyType()], t.BoolType(), sim_grad=zero_gradient, descr="%s returns true iff its argument is a " + tp.name())
def binaryPred(f, descr=None): return deterministic_typed(f, [t.AnyType(), t.AnyType()], t.BoolType(), sim_grad=zero_gradient, descr=descr)
else: return [vals[1][i] for i in indexes] def description(self, name): return ' %s(weights, objects) samples a categorical with the given '\ 'weights. In the one argument case, returns the index of the chosen '\ 'option as an integer; in the two argument case returns the item at that '\ 'index in the second argument. It is an error if the two arguments '\ 'have different length.' % name registerBuiltinSP( "categorical", typed_nr(CategoricalOutputPSP(), [t.SimplexType(), t.ArrayType()], t.AnyType(), min_req_args=1)) class LogCategoricalOutputPSP(DiscretePSP): # (log_categorical log_ps outputs) def simulate(self, args): vals = args.operandValues() if len(vals) == 1: # Default values to choose from return sampleLogCategorical( vals[0], args.np_prng(), [VentureInteger(i) for i in range(len(vals[0]))]) else: if len(vals[0]) != len(vals[1]): raise VentureValueError( "Categorical passed different length arguments.")
# (at your option) any later version. # # Venture is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Venture. If not, see <http://www.gnu.org/licenses/>. import numpy as np from venture.lite.sp import SPType from venture.lite.sp_help import deterministic_psp from venture.lite.sp_help import dispatching_psp from venture.lite.sp_help import no_request from venture.lite.sp_registry import registerBuiltinSP import venture.lite.types as t generic_biplex = dispatching_psp( [SPType([t.BoolType(), t.AnyType(), t.AnyType()], t.AnyType()), SPType([t.ArrayUnboxedType(t.NumberType()), t.ArrayUnboxedType(t.NumberType()), t.ArrayUnboxedType(t.NumberType())], t.ArrayUnboxedType(t.NumberType()))], [deterministic_psp(lambda p, c, a: c if p else a, sim_grad=lambda args, direction: [0, direction, 0] if args[0] else [0, 0, direction], descr="biplex returns either its second or third argument, depending on the first."), deterministic_psp(np.where, # TODO sim_grad descr="vector-wise biplex")]) registerBuiltinSP("biplex", no_request(generic_biplex))
def __venture_start__(ripl): ripl.execute_program(''' define set_value_at_scope_block = (scope, block, value) -> { set_value_at2(scope, block, value) }; ''') ripl.bind_foreign_inference_sp( 'sort', deterministic_typed( np.sort, [ vt.ArrayUnboxedType(vt.NumberType()), ], vt.ArrayUnboxedType(vt.NumberType()), min_req_args=1 ) ) ripl.bind_foreign_inference_sp( 'get_mean', deterministic_typed( np.mean, [ vt.ArrayUnboxedType(vt.NumberType()), ], vt.NumberType(), min_req_args=1 ) ) ripl.bind_foreign_inference_sp( 'get_predictive_mean', deterministic_typed( lambda x: np.mean(x, axis=0), [ vt.ArrayUnboxedType(vt.ArrayUnboxedType(vt.NumberType())), ], vt.ArrayUnboxedType(vt.NumberType()), min_req_args=1 ) ) ripl.bind_foreign_inference_sp( 'load_csv', deterministic_typed( load_csv, [vt.StringType()], vt.ArrayUnboxedType(vt.NumberType()), min_req_args=1 ) ) ripl.bind_foreign_inference_sp( 'concatenate', deterministic_typed( concatenate, [ vt.ArrayUnboxedType(vt.NumberType()), vt.ArrayUnboxedType(vt.NumberType()), ], vt.ArrayUnboxedType(vt.NumberType()), min_req_args=2 ) ) ripl.bind_foreign_inference_sp( 'scatter_plot', deterministic_typed( scatter_plot, [ vt.ArrayUnboxedType(vt.NumberType()), vt.ArrayUnboxedType(vt.NumberType()), vt.HomogeneousDictType(vt.StringType(), vt.AnyType()) ], vt.NilType(), min_req_args=2 ) ) ripl.bind_foreign_inference_sp( 'line_plot', deterministic_typed( line_plot, [ vt.ArrayUnboxedType(vt.NumberType()), vt.ArrayUnboxedType(vt.NumberType()), vt.HomogeneousDictType(vt.StringType(), vt.AnyType()) ], vt.NilType(), min_req_args=2 ) ) ripl.bind_foreign_inference_sp( 'legend', deterministic_typed( legend, [vt.StringType()], vt.NilType(), min_req_args=0 ) ) ripl.bind_foreign_inference_sp( 'square_heatmap', deterministic_typed( square_heatmap, [ vt.ArrayUnboxedType(vt.NumberType()), vt.ArrayUnboxedType(vt.NumberType()), vt.HomogeneousDictType(vt.StringType(), vt.AnyType()) ], vt.NilType(), min_req_args=2 ) ) ripl.bind_foreign_sp( 'gp_cov_cp', _cov_sp( change_point, [ vt.NumberType(), vt.NumberType(), GPCovarianceType('K'), GPCovarianceType('H') ] ) )
def testLiteToString(): checkTypedProperty(propLiteToStack, t.AnyType())
else: return set_fmap(thing1, lambda nodes: f(nodes, as_set(thing2))) inf.registerBuiltinInferenceSP( "by_intersection", deterministic_typed( Intersect, [t.ForeignBlobType(), t.ForeignBlobType()], t.ForeignBlobType(), descr=""" Intersect the selected choices. """)) inf.registerBuiltinInferenceSP("by_tag", \ deterministic_typed(FetchTag, [t.AnyType("<tag>")], t.ForeignBlobType(), descr=""" Select the choices tagged by the given tag. They remain keyed by their values, so that `random_singleton` will pick all the choices given by a random tag value, rather than a single choice at random from all choices under that tag. """)) def by_tag_value_fun(tag, val): return Lookup(val, FetchTag(tag)) inf.registerBuiltinInferenceSP("by_tag_value", \ deterministic_typed(by_tag_value_fun, [t.AnyType("<tag>"), t.AnyType("<value>")], t.ForeignBlobType(), descr=""" Select the choices tagged by the given tag at the given value. """))