def simulate(self, args): f_node = args.operandNodes[0] prior_mean_function = args.operandValues()[1] prior_covariance_function = args.operandValues()[2] f_compute = VentureSPRecord( SP(GPMComputerRequestPSP(f_node), GPMComputerOutputPSP())) f_emu = VentureSPRecord(GPSP(prior_mean_function, prior_covariance_function)) f_emu.spAux = self.shared_aux f_compute.spAux = self.shared_aux return v.pythonListToVentureList([f_compute, f_emu])
def simulate(self, args): assert len(args.operandValues()) == 6 f_node = args.operandNodes[0] a, b, c, noisestd, L_ = args.operandValues()[1:6] L = int(L_) f_compute = VentureSPRecord( SP(QuadMComputerRequestPSP(f_node), QuadMComputerOutputPSP())) f_emu = VentureSPRecord(QuadSP(a, b, c, noisestd, L)) f_emu.spAux = self.shared_aux f_compute.spAux = self.shared_aux return t.pythonListToVentureList([f_compute, f_emu])
def simulate(self, args): f_node = args.operandNodes[0] prior_covariance_function = args.operandValues[1] f_compute = VentureSPRecord( SP(GPMComputerRequestPSP(f_node), GPMComputerOutputPSP())) # Prior mean is fixed to zero, because the current GP implementation # assumes this f_emu = VentureSPRecord(GPSP(zero_function, prior_covariance_function)) f_emu.spAux = self.shared_aux f_compute.spAux = self.shared_aux # TODO ways to get_Xseen and get_Yseen? maybe this belongs in the # inference side return t.pythonListToVentureList([f_compute, f_emu])
def simulate(self, args): weight = args.operandValues()[0] # The made SP is the same as in the conjugate case: flip coins # based on an explicit weight, and maintain sufficient statistics. output = TypedPSP(SuffBernoulliOutputPSP(weight), SPType([], t.BoolType())) return VentureSPRecord(SuffBernoulliSP(NullRequestPSP(), output))
def simulate(self, args): (m0, k0, v0, S0) = args.operandValues() m0 = np.mat(m0).transpose() d = np.size(m0) output = TypedPSP(CMVNOutputPSP(d, m0, k0, v0, S0), SPType([], t.HomogeneousArrayType(t.NumberType()))) return VentureSPRecord(CMVNSP(NullRequestPSP(), output, d))
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 fromStackDict(thing): # proxy for VentureValue.fromStackDict that handles SPs by unwrapping them # TODO: should foreign_sp be a recognized stack dict type? # should this become the normal stack representation for SPs? if thing is None: return None elif thing["type"] == "foreign_sp": return VentureSPRecord(thing["sp"].sp, thing["aux"]) elif thing["type"] == "request": return Request(thing["value"]["esrs"], thing["value"]["lsrs"]) else: return VentureValue.fromStackDict(thing)
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, _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, 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, 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 commit(self): # note that we do not call registerRandomChoice() because it in # turn calls registerRandomChoiceInScope() for node in self.rcs: self.base.rcs.add(node) # this iteration includes "default" for (scope, blocks) in self.scopes.iteritems(): for (block, pnodes) in blocks.iteritems(): for pnode in pnodes: self.base.registerRandomChoiceInScope(scope, block, pnode, unboxed=True) # note that we do not call registerConstrainedChoice() because it # in turn calls unregisterRandomChoice() for node in self.ccs: self.base.registerConstrainedChoice(node) for node in self.aes: self.base.registerAEKernel(node) for (node, value) in self.values.iteritems(): assert value is not None self.base.setValueAt(node, value) for (node, madeSP) in self.madeSPs.iteritems(): self.base.setMadeSPRecordAt(node, VentureSPRecord(madeSP)) for (node, esrParents) in self.esrParents.iteritems(): self.base.setEsrParentsAt(node, esrParents) for (node, numRequests) in self.numRequests.iteritems(): self.base.setNumRequestsAt(node, numRequests) for (node, newMadeSPFamilies) in self.newMadeSPFamilies.iteritems(): self.base.addNewMadeSPFamilies(node, newMadeSPFamilies) for (node, newChildren) in self.newChildren.iteritems(): self.base.addNewChildren(node, newChildren) for (node, spaux) in self.madeSPAuxs.iteritems(): self.base.setMadeSPAuxAt(node, spaux)
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))
def bindPrimitiveSP(self, name, sp): address = addr.builtin_address(name) spNode = self.createConstantNode(address, VentureSPRecord(sp)) processMadeSP(self, spNode, False) assert isinstance(self.valueAt(spNode), SPRef) self.globalEnv.addBinding(name, spNode)
def extractValue(self, did): ret = self.trace.extractValue(did) if ret["type"] == "foreign_sp": ret = VentureSPRecord(ret["sp"], ret["aux"]).asStackDict(None) return ret
def simulate(self, args): (alpha, beta) = args.operandValues() output = TypedPSP(CGammaPoissonOutputPSP(alpha, beta), SPType([], t.CountType())) return VentureSPRecord(SuffPoissonSP(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))
def bindPrimitiveSP(self, name, sp): # TODO Mess with SPRecords and SPRefs properly spVal = VentureSPRecord(sp) self.env.addBinding(name, node.Node(None, spVal))
def simulate(self, args): mu = args.operandValues()[0] output = TypedPSP(SuffPoissonOutputPSP(mu), SPType([], t.CountType())) return VentureSPRecord(SuffPoissonSP(NullRequestPSP(), output))
def simulate(self, _trace, args): madeaux = args.madeSPAux() (p0, T, O) = args.operandValues() sp = UncollapsedHMMSP(p0, np.transpose(T), np.transpose(O)) sp.forwardBackwardSample(madeaux, args.np_prng()) return VentureSPRecord(sp, madeaux)
def simulate(self, args): (p0, T, O) = args.operandValues() # Transposition for compatibility with Puma sp = UncollapsedHMMSP(p0, np.transpose(T), np.transpose(O)) return VentureSPRecord(sp)
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): sharedOperatorNode = args.operandNodes[0] return VentureSPRecord( SP(MSPRequestPSP(sharedOperatorNode), ESRRefOutputPSP()))
def madeSPRecordAt(self, node): return VentureSPRecord(self.madeSPAt(node), self.madeSPAuxAt(node))
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): (mean, covariance) = args.operandValues() return VentureSPRecord(GPSP(mean, covariance))