Example #1
0
 def parse_expr(self, expr, command):
     special_names = [VentureSymbol(x) for x in ['iter', 'time', 'score']]
     if (isinstance(expr, VentureArray) and expr.lookup(VentureInteger(0))
             == VentureSymbol('labelled')):
         # The first element is the command, the second is the label for
         # the command
         stack_dict = expr.lookup(VentureInteger(1)).asStackDict()
         name = expr.lookup(VentureInteger(2)).symbol
     elif command == 'printf' and expr in special_names:
         name = expr.getSymbol()
         stack_dict = None
     else:
         # Generate the default name, get the stack dict
         stack_dict = expr.asStackDict()
         name = self.default_name_for_exp(ExpressionType().asPython(expr))
     return name, stack_dict
Example #2
0
 def marginalLogDensityOfData(self, aux, 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)
     ]
     return CSymDirCatOutputPSP(alpha, n, os).logDensityOfData(aux)
Example #3
0
 def logDensity(self, val, args):
     vals = args.operandValues()
     if len(vals) == 1:  # Default values to choose from
         return logDensityLogCategorical(
             val, vals[0], [VentureInteger(i) for i in range(len(vals[0]))])
     else:
         return logDensityLogCategorical(val, *vals)
Example #4
0
 def marginalLogDensityOfData(self, aux, 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)
     ]
     return CDirCatOutputPSP(alpha, os).logDensityOfData(aux)
Example #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)))
Example #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))
Example #7
0
 def simulate(self, args):
     vals = args.operandValues()
     if len(vals) == 1:  # Default values to choose from
         return sampleLogCategorical(
             vals[0], args.np_prng(),
             [VentureInteger(i) for i in range(len(vals[0]))])
     else:
         if len(vals[0]) != len(vals[1]):
             raise VentureValueError(
                 "Categorical passed different length arguments.")
         ps, os = vals
         return sampleLogCategorical(ps, args.np_prng(), os)
Example #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)
Example #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))
Example #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)