Exemplo n.º 1
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))
Exemplo n.º 2
0
 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)
Exemplo n.º 3
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))
Exemplo n.º 4
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)
Exemplo n.º 5
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)))
Exemplo n.º 6
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))
Exemplo n.º 7
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)
Exemplo n.º 8
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)
Exemplo n.º 9
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))
Exemplo n.º 10
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)
Exemplo n.º 11
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))
Exemplo n.º 12
0
 def simulate(self, args):
     (alpha, beta) = args.operandValues()
     output = TypedPSP(CBetaBernoulliOutputPSP(alpha, beta),
                       SPType([], t.BoolType()))
     return VentureSPRecord(SuffBernoulliSP(NullRequestPSP(), output))
Exemplo n.º 13
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))
Exemplo n.º 14
0
def no_request(output):
    return SP(NullRequestPSP(), output)
Exemplo n.º 15
0
        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)
Exemplo n.º 16
0
 def simulate(self, args):
     (alpha, beta) = args.operandValues()
     output = TypedPSP(CGammaPoissonOutputPSP(alpha, beta),
                       SPType([], t.CountType()))
     return VentureSPRecord(SuffPoissonSP(NullRequestPSP(), output))
Exemplo n.º 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))
Exemplo n.º 18
0
 def simulate(self, args):
     mu = args.operandValues()[0]
     output = TypedPSP(SuffPoissonOutputPSP(mu), SPType([], t.CountType()))
     return VentureSPRecord(SuffPoissonSP(NullRequestPSP(), output))
Exemplo n.º 19
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,