Beispiel #1
0
 def __init__(self, p0, T, O):
     req = TypedPSP(UncollapsedHMMRequestPSP(),
                    SPType([t.CountType()], t.RequestType()))
     output = TypedPSP(UncollapsedHMMOutputPSP(O),
                       SPType([t.CountType()], t.IntegerType()))
     super(UncollapsedHMMSP, self).__init__(req, output)
     self.p0 = p0
     self.T = T
     self.O = O
Beispiel #2
0
 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))
Beispiel #3
0
def deterministic_typed_psp(f,
                            args_types,
                            return_type,
                            descr=None,
                            sim_grad=None,
                            **kwargs):
    return TypedPSP(deterministic_psp(f, descr, sim_grad),
                    SPType(args_types, return_type, **kwargs))
Beispiel #4
0
    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))
Beispiel #5
0
def typed_func_psp(f,
                   args_types,
                   return_type,
                   descr=None,
                   sim_grad=None,
                   **kwargs):
    return TypedPSP(FunctionPSP(f, descr, sim_grad),
                    SPType(args_types, return_type, **kwargs))
Beispiel #6
0
 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)
Beispiel #7
0
 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)))
Beispiel #8
0
 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))
Beispiel #9
0
 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)
Beispiel #10
0
 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))
Beispiel #11
0
 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)
Beispiel #12
0
 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)
Beispiel #13
0
 def simulate(self, args):
     (alpha, beta) = args.operandValues()
     output = TypedPSP(CBetaBernoulliOutputPSP(alpha, beta),
                       SPType([], t.BoolType()))
     return VentureSPRecord(SuffBernoulliSP(NullRequestPSP(), output))
Beispiel #14
0
        (operator, operands) = args.operandValues()
        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):
Beispiel #15
0
 def simulate(self, args):
     (alpha, beta) = args.operandValues()
     output = TypedPSP(CGammaPoissonOutputPSP(alpha, beta),
                       SPType([], t.CountType()))
     return VentureSPRecord(SuffPoissonSP(NullRequestPSP(), output))
Beispiel #16
0
 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))
Beispiel #17
0
 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))
Beispiel #18
0
 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))
Beispiel #19
0
 def simulate(self, args):
     mu = args.operandValues()[0]
     output = TypedPSP(SuffPoissonOutputPSP(mu), SPType([], t.CountType()))
     return VentureSPRecord(SuffPoissonSP(NullRequestPSP(), output))
Beispiel #20
0
    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,
Beispiel #21
0
def typed_nr(output, args_types, return_type, **kwargs):
    return no_request(
        TypedPSP(output, SPType(args_types, return_type, **kwargs)))
Beispiel #22
0
def dispatching_psp(types, psps):
    return DispatchingPSP(
        types, [TypedPSP(psp, tp) for (psp, tp) in zip(psps, types)])
Beispiel #23
0
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()))