Ejemplo n.º 1
0
def main():
    b = sentence.Atom("the sky is blue")
    g = sentence.Atom("the sky is green")
    r = sentence.Atom("the world is round")

    b_or_g = sentence.Disjunction(b, g)
    if_g_then_r = sentence.Implication(g, r)

    def trading_algorithm_1():
        yield {
            b:
            formula.Constant(3.5),
            b_or_g:
            formula.Product(formula.Constant(2), formula.Price(if_g_then_r,
                                                               1)),
        }
        yield {
            b: formula.Constant(-1),
            b_or_g: formula.Max(formula.Price(b, 1), formula.Price(g, 1)),
        }

    def trading_algorithm_2():
        yield {
            b:
            formula.Constant(3.5),
            b_or_g:
            formula.Product(formula.Constant(2), formula.Price(if_g_then_r,
                                                               2)),
        }
        yield {
            b: formula.Constant(-1),
            if_g_then_r: formula.Max(formula.Price(b, 1), formula.Price(g, 2)),
        }

    li = inductor.LogicalInductor()

    # update 1
    credences = li.update(b, trading_algorithm_1())
    print("\nafter update 1:")
    for s, credence in credences.items():
        print("  {:40s} {:f}".format(str(s), float(credence)))

    # update 2
    credences = li.update(b, trading_algorithm_2())
    print("\nafter update 2:")
    for s, credence in credences.items():
        print("  {:40s} {:f}".format(str(s), float(credence)))
Ejemplo n.º 2
0
def test_compute_budget_factor_already_overran_budget():
    phi = sentence.Atom("ϕ")
    psi = sentence.Atom("Ψ")

    # there was one previous observation, which was "phi OR psi"
    past_observations = [sentence.Disjunction(phi, psi)]

    # on our one previous update, our credences were as follows
    past_credences = credence.History([{
        phi: .6,
        psi: .7,
    }])

    # on the previous update we purchased one token of psi
    past_trading_formulas = [{
        psi: formula.Constant(10),
    }]

    # we observe psi in our most recent update
    latest_observation = sentence.Disjunction(phi, psi)

    # our trading formula says to always purchase 10 tokens of phi
    latest_trading_formulas = {
        phi: formula.Constant(10),
    }

    # our budget is $2, which means we can lose up to $2, or, in other words,
    # the value of our holdings is allowed to go as low as -$2
    budget = 2

    # compute the budget factor
    budget_factor = inductor.compute_budget_factor(budget, past_observations,
                                                   latest_observation,
                                                   past_trading_formulas,
                                                   latest_trading_formulas,
                                                   past_credences)

    # on our previous update we purchased one token of PSI for $7 without being
    # able to rule out the possibility that PHI could turn out to be false, in
    # which case we would have lost $7, which is more than our budget of $2, so
    # our budget factor should be the constant 0 which eliminates all further
    # trading
    assert_is_instance(budget_factor, formula.Constant)
    assert_equal(budget_factor.k, 0)
Ejemplo n.º 3
0
def test_logical_inductor_simple():
    phi = sentence.Atom("ϕ")
    psi = sentence.Atom("Ψ")

    # create a trading algorithm that purchases 1, 2, 3, ... tokens of phi
    def trading_algorithm(sentence, start=1, step=1):
        for quantity in enumerator.integers(start=start, step=step):
            yield {sentence: formula.Constant(quantity)}

    lia = inductor.LogicalInductor()

    credences = lia.update(sentence.Negation(phi),
                           trading_algorithm(phi, start=1, step=1))
    print(credences)

    credences = lia.update(psi, trading_algorithm(psi, start=-1, step=-1))
    print(credences)

    credences = lia.update(psi, trading_algorithm(psi, start=-1, step=-1))
    print(credences)
Ejemplo n.º 4
0
def test_combine_trading_algorithms_simple():
    phi = sentence.Atom("ϕ")
    psi = sentence.Atom("Ψ")

    # in this test we are on the first update, so there is one trading
    # algorithm, one observation, and no historical credences
    trading_formula = {phi: formula.Constant(1)}
    trading_histories = [[trading_formula]]
    observation_history = [psi]
    credence_history = credence.History([])

    # create the compound trader, which just has one internal trader
    compound_trader = inductor.combine_trading_algorithms(
        trading_histories,
        observation_history,
        credence_history,
    )

    assert_equal(len(compound_trader), 1)
    assert_is_instance(compound_trader[phi], formula.Sum)
    assert_equal(len(compound_trader[phi].terms), 3)
def main():
    # the trading algorithms are constant from the beginning


    # each step we:
    # - learn that the "i-th digit of pi is d" is true/false for a random value of d
    # - add a trading algorithm that trades constant p probability on all observed sentences

    num_days = 1

    # sentences we're going to bet on
    sentences_by_place = []
    for place in range(num_days):
        sentences_by_place.append([
            sentence.Atom("digit {} of pi is {}".format(place+1, d))
            for d in range(10)
        ])

    # flatten the list of sentences
    flat_sentences = []
    for sentences in sentences_by_place:
        flat_sentences.extend(sentences)

    # create a trading algorithm that trades on all sentences according to a fixed probability
    def trading_algorithm(p):
        for day in enumerator.integers(start=1):
            yield {
                sentence: trade_on_probability(sentence, day, p)
                for sentence in flat_sentences
            }

    # set up a search order that fixes all probabilities as equal
    def search_order(sentences):
        for r in enumerator.rationals_between(0, 1):
            yield {sentence: r for sentence in sentences}

    # create the logical inductor
    li = inductor.LogicalInductor()

    # run the logical inductor
    for day in range(num_days):
        foo = .55

        observation = sentences_by_place[day][0]

        trader_probability = .3

        credences = li.update(
            observation,
            trading_algorithm(trader_probability),
            search_order=search_order)

        print("\nafter update {}: p={}".format(day, foo))
Ejemplo n.º 6
0
def main():
    # the sentence we're going to bet on
    digit_n_is_k = sentence.Atom("n-th digit of pi is k")

    # create a nonsense atom
    irrelevant = sentence.Atom("foo")

    # create a trading algorithm that bets according to a fixed probability
    def trading_algorithm():
        for day in enumerator.integers(start=1):
            yield {
                digit_n_is_k: make_s_curve(formula.Price(digit_n_is_k, day), .1, 10)
            }

    # create the logical inductor
    li = inductor.LogicalInductor()

    # number of days to run trading for
    num_days = 15

    # run the logical inductor
    history = []
    for day in range(1, num_days+1):
        credences = li.update(irrelevant, trading_algorithm())
        history.append(credences[digit_n_is_k])
        print("\nafter update {}:".format(day))
        for s, credence in credences.items():
            print("  {:40s} {:f}".format(str(s), float(credence)))

    # plot the credences over time
    fig = matplotlib.figure.Figure()
    ax = fig.add_axes([.1, .1, .8, .8],
        xlim=[1, num_days],
        ylim=[0, 1],
        xlabel="day",
        ylabel="credence")
    ax.plot(range(1, num_days+1), history)
    fig.savefig("out/history.pdf")
Ejemplo n.º 7
0
def test_compute_budget_factor_simple():
    phi = sentence.Atom("ϕ")

    # we are on the first update; our history is all empty
    past_credences = credence.History([])  # no past credences
    past_trading_formulas = []  # no past trading formulas
    past_observations = []  # no past observations

    # we observed phi in our most recent update
    latest_observation = phi

    # our trading formula says to always purchase 10 tokens of phi
    latest_trading_formulas = {
        phi: formula.Constant(10),
    }

    # our budget is $2, which means we can lose up to $2, or, in other words,
    # the value of our holdings is allowed to go as low as -$2
    budget = 2

    # compute the budget factor
    budget_factor = inductor.compute_budget_factor(budget, past_observations,
                                                   latest_observation,
                                                   past_trading_formulas,
                                                   latest_trading_formulas,
                                                   past_credences)

    assert_is_instance(budget_factor, formula.SafeReciprocal)

    # our world consists of only one base fact (phi), and we observed phi, so
    # the world where phi=true is only one world propositionally consistent with
    # our observations, and in this world we purchase 10 tokens of phi, which
    # could cost anywhere from $0 to $10 depending on the as-yet-unknown
    # credence for phi, and will have a value of exactly $10 in this world. In
    # no case will the value of our holdings drop below -$2, so we expect a
    # budget factor that evaluates to 1 for all credences in [0, 1].
    assert_equal(
        budget_factor.evaluate(past_credences.with_next_update({phi: 0.})), 1.)
    assert_equal(
        budget_factor.evaluate(past_credences.with_next_update({phi: .2})), 1.)
    assert_equal(
        budget_factor.evaluate(past_credences.with_next_update({phi: .6})), 1.)
    assert_equal(
        budget_factor.evaluate(past_credences.with_next_update({phi: 1.})), 1.)
Ejemplo n.º 8
0
def test_compute_budget_factor_two_base_facts():
    phi = sentence.Atom("ϕ")
    psi = sentence.Atom("Ψ")

    # we are on the first update; our history is all empty
    past_credences = credence.History([])  # no past credences
    past_trading_formulas = []  # no past trading formulas
    past_observations = []  # no past observations

    # we observe psi in our most recent update
    latest_observation = sentence.Disjunction(phi, psi)

    # our trading formula says to always purchase 10 tokens of phi
    latest_trading_formulas = {
        phi: formula.Constant(10),
    }

    # our budget is $2, which means we can lose up to $2, or, in other words,
    # the value of our holdings is allowed to go as low as -$2
    budget = 2

    # compute the budget factor
    budget_factor = inductor.compute_budget_factor(budget, past_observations,
                                                   latest_observation,
                                                   past_trading_formulas,
                                                   latest_trading_formulas,
                                                   past_credences)

    assert_is_instance(budget_factor, formula.SafeReciprocal)

    print()
    print(budget_factor.tree())

    # Our world consists of two base facts, phi and psi, and we observed "phi OR psi", so
    # there are three worlds consistent with this observation:
    #   phi=True   psi=True
    #   phi=True   psi=False
    #   phi=False  psi=True
    #
    # Our trading formula says to purchase 10 tokens of phi no matter what. This
    # could cost us between $0 and $10 depending on the credence for phi. The
    # value of these 10 tokens could turn out to be either $0 if phi=False or $10
    # if phi=True:
    #   phi=True   psi=True    -> value of 10 tokens of phi = $10, so net worth between $0 and $10
    #   phi=True   psi=False   -> value of 10 tokens of phi = $10, so net worth between $0 and $10
    #   phi=False  psi=True    -> value of 10 tokens of phi = $0, so net worth between -$10 and $0
    #
    # In the third world, our net worth could drop below our budget for some
    # possible credences, so:

    # if the credence for phi were 1 then in the third world we would end up with a
    # net worth of -$10, so we should multiply our trading volume by 0.2
    assert_almost_equal(
        budget_factor.evaluate(past_credences.with_next_update({phi: 1.})), .2)

    # if the credence for phi were 0.4 then in the third world we would end up with a
    # net worth of -$4, so we should multiply our trading volume by 0.5
    assert_almost_equal(
        budget_factor.evaluate(past_credences.with_next_update({phi: .4})), .5)

    # if the credence for phi were 0.2 then in the third world we would end up with a
    # net worth of -$2, which is right on budget, so our scaling factor should be 1
    assert_almost_equal(
        budget_factor.evaluate(past_credences.with_next_update({phi: .2})), 1.)

    # if the credence for phi were 0 then in the third world we would end up with a
    # net worth of $0, which is above budget, so our scaling factor should be 1
    assert_almost_equal(
        budget_factor.evaluate(past_credences.with_next_update({phi: 0.})), 1.)