def simulate(self, _trace, args): (alpha, beta) = args.operandValues() madeaux = args.madeSPAux() [ctY, ctN] = madeaux.cts() new_weight = args.np_prng().beta(a=(alpha + ctY), b=(beta + ctN)) output = TypedPSP(SuffBernoulliOutputPSP(new_weight), SPType([], t.BoolType())) return VentureSPRecord(SuffBernoulliSP(NullRequestPSP(), output), madeaux)
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, _trace, args): madeaux = args.madeSPAux() [xsum, ctN] = madeaux.cts() (alpha, beta) = args.operandValues() new_alpha = alpha + xsum new_beta = beta + ctN new_mu = args.np_prng().gamma(shape=(alpha + xsum), scale=1. / new_beta) output = TypedPSP(UGammaPoissonOutputPSP(new_mu, new_alpha, new_beta), SPType([], t.CountType())) return VentureSPRecord(SuffPoissonSP(NullRequestPSP(), output), madeaux)
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 __init__(self, f, args_types=None, return_type=None, sp_type=None, **kwargs): if sp_type is not None: args_types = sp_type.args_types return_type = sp_type.return_type else: sp_type = SPType(args_types, return_type) self.f = f self.sp_type = sp_type self.stuff = kwargs
else: return math.log1p(-weight) def logDensityOfData(self, aux): [ctY, ctN] = aux.cts() trues = ctY + self.alpha falses = ctN + self.beta numerator = scipy.special.betaln(trues, falses) denominator = scipy.special.betaln(self.alpha, self.beta) return numerator - denominator registerBuiltinSP( "make_beta_bernoulli", typed_nr(MakerCBetaBernoulliOutputPSP(), [t.PositiveType(), t.PositiveType()], SPType([], t.BoolType()))) #### Uncollapsed AAA Beta Bernoulli class MakerUBetaBernoulliOutputPSP(RandomPSP): def childrenCanAAA(self): return True def getAAALKernel(self): return UBetaBernoulliAAALKernel(self) def simulate(self, args): (alpha, beta) = args.operandValues() weight = args.np_prng().beta(a=alpha, b=beta) output = TypedPSP(SuffBernoulliOutputPSP(weight), SPType([], t.BoolType()))
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)
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()],
aux.N += 1 aux.xTotal += x aux.STotal += x * x.T def unincorporate(self, x, args): x = np.mat(x).reshape((self.d, 1)) aux = args.spaux() aux.N -= 1 aux.xTotal -= x aux.STotal -= x * x.T def logDensityOfData(self, aux): (mN, kN, vN, SN) = self.updatedParams(aux) term1 = -(aux.N * self.d * math.log(math.pi)) / 2. term2 = logGenGamma(self.d, vN / 2.) term3 = -logGenGamma(self.d, self.v0 / 2.) term4 = (self.v0 / 2.) * np.linalg.slogdet(self.S0)[1] # first is sign term5 = -(vN / 2.) * np.linalg.slogdet(SN)[1] term6 = (self.d / 2.) * math.log(float(self.k0) / kN) return term1 + term2 + term3 + term4 + term5 + term6 registerBuiltinSP( "make_niw_normal", typed_nr(MakeCMVNOutputPSP(), [ t.HomogeneousArrayType(t.NumberType()), t.NumberType(), t.NumberType(), t.MatrixType() ], SPType([], t.HomogeneousArrayType(t.NumberType()))))
def logDensity(self, value, args): n = args.operandValues()[0] xs = args.spaux().xs assert len(xs) > n theta = np.dot(xs[n], self.O) return math.log(theta[value]) def incorporate(self, value, args): n = args.operandValues()[0] os = args.spaux().os if n not in os: os[n] = [] os[n].append(value) def unincorporate(self, value, args): n = args.operandValues()[0] os = args.spaux().os del os[n][os[n].index(value)] if not os[n]: del os[n] class UncollapsedHMMRequestPSP(DeterministicPSP): def simulate(self, args): return Request([], [args.operandValues()[0]]) registerBuiltinSP( "make_lazy_hmm", typed_nr(MakeUncollapsedHMMOutputPSP(), [t.SimplexType(), t.MatrixType(), t.MatrixType()], SPType([t.CountType()], t.IntegerType())))
def simulate(self, args): vals = args.operandValues() alpha = vals[0] d = vals[1] if len(vals) == 2 else 0 output = TypedPSP(CRPOutputPSP(alpha, d), SPType([], t.AtomType())) return VentureSPRecord(CRPSP(NullRequestPSP(), output))
def simulate(self, args): (alpha, beta) = args.operandValues() mu = args.np_prng().gamma(shape=alpha, scale=1. / beta) output = TypedPSP(UGammaPoissonOutputPSP(mu, alpha, beta), SPType([], t.CountType())) return VentureSPRecord(SuffPoissonSP(NullRequestPSP(), output))
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)))
# instead of being unpacked by f_type.gradient_type()? return v.VentureNumber(sum(venture_array.getArray(t.NumberType()))) def symbolic_zero_left(n, obj): assert n == 0, "Cannot add non-zero integer %r to %r" % (n, obj) return obj def symbolic_zero_right(obj, n): assert n == 0, "Cannot add non-zero integer %r to %r" % (n, obj) return obj generic_add = dispatching_psp([ SPType([t.Int], t.Int, variadic=True), SPType([t.Int, t.Number], t.Number), SPType([t.Number, t.Int], t.Number), SPType([t.NumberType()], t.NumberType(), variadic=True), SPType([t.ArrayUnboxedType(t.NumberType()), t.NumberType()], t.ArrayUnboxedType(t.NumberType())), SPType([t.NumberType(), t.ArrayUnboxedType(t.NumberType())], t.ArrayUnboxedType(t.NumberType())), SPType([t.ArrayUnboxedType(t.NumberType())], t.ArrayUnboxedType(t.NumberType()), variadic=True), SPType([t.Int, t.Object], t.Object), SPType([t.Object, t.Int], t.Object), SPType([t.Object, t.Object], t.Object), ], [ deterministic_psp(
x = args.operandValues() return _gp_gradientOfLogDensity(self.mean, self.covariance, samples, [x], [o]) def incorporate(self, o, args): samples = args.spaux().samples x = args.operandValues()[0] samples[x] = o def unincorporate(self, _o, args): samples = args.spaux().samples x = args.operandValues()[0] del samples[x] gpType = SPType([t.ArrayUnboxedType(t.NumericArrayType())], t.ArrayUnboxedType(t.NumberType())) gp1Type = SPType([t.NumberType()], t.NumberType()) class GPSPAux(SPAux): def __init__(self, samples): self.samples = samples def copy(self): return GPSPAux(copy.copy(self.samples)) def asVentureValue(self): def encode(xy): # (x,y) = xy # Since we are assuming the domain of the GP is numeric, the
def simulate(self, args): (alpha, beta) = args.operandValues() output = TypedPSP(CBetaBernoulliOutputPSP(alpha, beta), SPType([], t.BoolType())) return VentureSPRecord(SuffBernoulliSP(NullRequestPSP(), output))
def simulate(self, args): mu = args.operandValues()[0] output = TypedPSP(SuffPoissonOutputPSP(mu), SPType([], t.CountType())) return VentureSPRecord(SuffPoissonSP(NullRequestPSP(), output))
def simulate(self, args): (alpha, beta) = args.operandValues() weight = args.np_prng().beta(a=alpha, b=beta) output = TypedPSP(SuffBernoulliOutputPSP(weight), SPType([], t.BoolType())) return VentureSPRecord(SuffBernoulliSP(NullRequestPSP(), output))
# (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))
# that there are more distinct possibilities than actually exist, # if more than one table was emptied by recent unincorporations. # This is Github issue #462: # https://github.com/probcomp/Venturecxx/issues/462 if aux.cachedTables: tables += sorted(aux.cachedTables.values()) else: tables.append(aux.nextTable) return tables registerBuiltinSP( 'make_crp', typed_nr(MakeCRPOutputPSP(), [t.NumberType(), t.NumberType()], SPType([], t.AtomType()), min_req_args=1)) def draw_crp_samples(n, alpha, np_rng=None): """Jointly draw n samples from CRP(alpha). This returns an assignment of n objects to clusters, given by a length-n list of cluster ids. """ aux = CRPSPAux() args = MockArgs([], aux, np_rng=np_rng) psp = CRPOutputPSP(alpha, 0) # No dispersion def draw_sample(): ans = psp.simulate(args)
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): return "%s(func, vals) returns the results of applying a function" \
def simulate(self, args): (alpha, beta) = args.operandValues() output = TypedPSP(CGammaPoissonOutputPSP(alpha, beta), SPType([], t.CountType())) return VentureSPRecord(SuffPoissonSP(NullRequestPSP(), output))
def typed_nr(output, args_types, return_type, **kwargs): return no_request( TypedPSP(output, SPType(args_types, return_type, **kwargs)))
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()], t.RequestType("<object>"))))) class AddressOfOutputPSP(DeterministicPSP): def simulate(self,args): place = args.operandNodes[0] node = args.trace.getOutermostNonReferenceNode(place) return jsonable_address(node) def description(self,name): return "%s returns a string representing the address of the top nontrivial node of its argument" % name registerBuiltinSP("address_of", typed_nr(AddressOfOutputPSP(), [t.AnyType()], t.StringType()))