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 __init__(self, mean, covariance): self.mean = mean self.covariance = covariance output = dispatching_psp( [gpType, gp1Type], [GPOutputPSP(mean, covariance), GPOutputPSP1(mean, covariance)]) super(GPSP, self).__init__(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 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): 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, _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 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 simulate(self, args): (alpha, beta) = args.operandValues() output = TypedPSP(CBetaBernoulliOutputPSP(alpha, beta), SPType([], t.BoolType())) return VentureSPRecord(SuffBernoulliSP(NullRequestPSP(), output))
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 no_request(output): return SP(NullRequestPSP(), output)
else: args.append(VentureArray(flat_args[i:i + s])) i += s assert i == len(flat_args) return args class ApplyFunctionOutputPSP(DeterministicPSP): def simulate(self, args): vals = args.operandValues() function = vals[0] arguments = vals[1:] sp_type = function.sp_type unwrapped_args = sp_type.unwrap_arg_list(arguments) #print sp_type.name(), unwrapped_args returned = function.f(*unwrapped_args) wrapped_return = sp_type.wrap_return(returned) return wrapped_return def description(self, _name=None): return "Apply a VentureFunction to arguments." # TODO Add type signature. Look at signature of apply? applyFunctionSP = SP(NullRequestPSP(), ApplyFunctionOutputPSP()) registerBuiltinSP("apply_function", applyFunctionSP)
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 simulate(self, args): mu = args.operandValues()[0] output = TypedPSP(SuffPoissonOutputPSP(mu), SPType([], t.CountType())) return VentureSPRecord(SuffPoissonSP(NullRequestPSP(), output))
def name(self): return self._name or '<covariance>' def distribution(self, base, **kwargs): return None def gradient_type(self): return t.ArrayUnboxedType(t.NumericArrayType()) makeGPType = SPType( [GPMeanType('mean function'), GPCovarianceType('covariance kernel')], gpType) makeGPSP = SP(NullRequestPSP(), TypedPSP(MakeGPOutputPSP(), makeGPType)) registerBuiltinSP('make_gp', makeGPSP) xType = t.NumericArrayType('x') oType = t.NumberType('o') def _mean_sp(F, argtypes): def mean_gradientOfSimulate(args, direction): return parameter_nest(F(*args).parameters, direction.getArray()) return deterministic_typed(F, argtypes, GPMeanType(), sim_grad=mean_gradientOfSimulate,