Beispiel #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))
Beispiel #2
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 #3
0
    "integer",
    deterministic_typed(
        int, [t.NumberType()],
        t.IntegerType(),
        descr="integer returns the floor of its argument number as an integer")
)

registerBuiltinSP("probability", deterministic_typed(lambda x:x,
    [t.ProbabilityType()], t.ProbabilityType(),
    descr="probability converts its argument to a probability " \
          "(in direct space)"))

registerBuiltinSP(
    "not",
    deterministic_typed(
        lambda x: not x, [t.BoolType()],
        t.BoolType(),
        descr="not returns the logical negation of its argument"))
registerBuiltinSP(
    "xor",
    deterministic_typed(
        lambda x, y: x != y, [t.BoolType(), t.BoolType()],
        t.BoolType(),
        descr="xor(x,y) returns true if exactly one of x and y is true"))

registerBuiltinSP(
    "all_p",
    deterministic_typed(
        all, [t.HomogeneousListType(t.BoolType())],
        t.BoolType(),
        descr="all returns true if all of the elements in the input are true"))
Beispiel #4
0
def type_test(tp):
    return deterministic_typed(lambda thing: thing in tp, [t.AnyType()],
                               t.BoolType(),
                               sim_grad=zero_gradient,
                               descr="%s returns true iff its argument is a " +
                               tp.name())
Beispiel #5
0
def binaryPred(f, descr=None):
    return deterministic_typed(f, [t.AnyType(), t.AnyType()],
                               t.BoolType(),
                               sim_grad=zero_gradient,
                               descr=descr)
Beispiel #6
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 #7
0
 def simulate(self, args):
     (alpha, beta) = args.operandValues()
     output = TypedPSP(CBetaBernoulliOutputPSP(alpha, beta),
                       SPType([], t.BoolType()))
     return VentureSPRecord(SuffBernoulliSP(NullRequestPSP(), output))
Beispiel #8
0
            return [True, False]

    def description(self, name):
        return '  {name}(p) returns true with probability p and false otherwise. '\
          'If omitted, p is taken to be 0.5. '\
          'If you are tempted to write ({name} (exp x)),'\
          ' write (log_{name} x) instead. '\
          'If you are tempted to write ({name} (logistic x)),'\
          ' write (log_odds_{name} x) instead.'\
          .format(name=name)


registerBuiltinSP(
    "flip",
    typed_nr(BernoulliOutputPSP(), [t.ProbabilityType()],
             t.BoolType(),
             min_req_args=0))

registerBuiltinSP(
    "bernoulli",
    typed_nr(BernoulliOutputPSP(), [t.ProbabilityType()],
             t.IntegerType(),
             min_req_args=0))


class LogBernoulliOutputPSP(DiscretePSP):
    def simulate(self, args):
        logp = args.operandValues()[0]
        return math.log(args.py_prng().random()) < logp

    def logDensity(self, val, args):
# (at your option) any later version.
#
# Venture is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Venture.  If not, see <http://www.gnu.org/licenses/>.

import numpy as np

from venture.lite.sp import SPType
from venture.lite.sp_help import deterministic_psp
from venture.lite.sp_help import dispatching_psp
from venture.lite.sp_help import no_request
from venture.lite.sp_registry import registerBuiltinSP
import venture.lite.types as t

generic_biplex = dispatching_psp(
  [SPType([t.BoolType(), t.AnyType(), t.AnyType()], t.AnyType()),
   SPType([t.ArrayUnboxedType(t.NumberType()), t.ArrayUnboxedType(t.NumberType()), t.ArrayUnboxedType(t.NumberType())], t.ArrayUnboxedType(t.NumberType()))],
  [deterministic_psp(lambda p, c, a: c if p else a,
                     sim_grad=lambda args, direction: [0, direction, 0] if args[0] else [0, 0, direction],
                     descr="biplex returns either its second or third argument, depending on the first."),
   deterministic_psp(np.where,
                     # TODO sim_grad
                     descr="vector-wise biplex")])

registerBuiltinSP("biplex", no_request(generic_biplex))