Example #1
0
    def __init__(self, thisData, thisEerObject, thisCllrObject, thisConfig,
                 thisExpName, thisDebug):
        self._debug = thisDebug
        self.data = thisData
        self._eerObject = thisEerObject
        self._cllrObject = thisCllrObject
        self.config = thisConfig
        self._expName = thisExpName
        self._printToFilename = thisExpName
        self.debug = thisDebug
        Probability.__init__(self, self.data, self.config, self.debug)
        self.plotType = 'det_plot'
        self.fig = None
        self.event = None

        # Values required for linear scale => gaussian scale conversion at __ppndf__()
        self.__SPLIT__ = 0.42
        self.__A0__ = 2.5066282388
        self.__A1__ = -18.6150006252
        self.__A2__ = 41.3911977353
        self.__A3__ = -25.4410604963
        self.__B1__ = -8.4735109309
        self.__B2__ = 23.0833674374
        self.__B3__ = -21.0622410182
        self.__B4__ = 3.1308290983
        self.__C0__ = -2.7871893113
        self.__C1__ = -2.2979647913
        self.__C2__ = 4.8501412713
        self.__C3__ = 2.3212127685
        self.__D1__ = 3.5438892476
        self.__D2__ = 1.6370678189
        self.__EPS__ = 2.2204e-16
Example #2
0
def test_probability_class():
    shoe = Shoe(8)
    prob = Probability(shoe)

    hand = Hand([c9, c4])
    dealer = Hand([c6])

    prob1 = prob.probability_of_hand(hand, 0)
    exp1 = 0.0059314179796107515
    prob2 = prob.probability_of_card(c0)
    exp2 = 0.07692307692307693

    if prob1 == exp1:
        print("SUCCESS: probability class return correct probability")
    else:
        print(
            f"FAIL: probability return '{prob1}' probability expected '{exp1}'"
        )

    if prob2 == exp2:
        print("SUCCESS: probability class return correct probability")
    else:
        print(
            f"FAIL: probability return '{prob2}' probability expected '{exp2}'"
        )
Example #3
0
    def __init__(self, thisData, thisEerObject, thisCllrObject, thisConfig, thisExpName, thisDebug):
        self._debug = thisDebug
        self.data = thisData
        self._eerObject = thisEerObject
        self._cllrObject = thisCllrObject
        self.config = thisConfig
        self._expName = thisExpName
        self._printToFilename = thisExpName
        self.debug = thisDebug
        Probability.__init__(self, self.data, self.config, self.debug)
        self.plotType = 'det_plot'
        self.fig = None
        self.event = None

        # Values required for linear scale => gaussian scale conversion at __ppndf__()
        self.__SPLIT__ = 0.42
        self.__A0__ = 2.5066282388
        self.__A1__ = -18.6150006252
        self.__A2__ = 41.3911977353
        self.__A3__ = -25.4410604963
        self.__B1__ = -8.4735109309
        self.__B2__ = 23.0833674374
        self.__B3__ = -21.0622410182
        self.__B4__ = 3.1308290983
        self.__C0__ = -2.7871893113
        self.__C1__ = -2.2979647913
        self.__C2__ = 4.8501412713
        self.__C3__ = 2.3212127685
        self.__D1__ = 3.5438892476
        self.__D2__ = 1.6370678189
        self.__EPS__ = 2.2204e-16
Example #4
0
def ID(y, x, G, verbose=False):
    G._construct_observed_and_confounded()
    P = Probability()
    y = [y] if type(
        y
    ) == str else y  # If I just have a single vertex, I need to wrap it in a list.
    x = [x] if type(x) == str else x
    y = set(y)
    x = set(x)
    tree = Tree()
    top_ord = G.get_topological_order()
    prettyPrint("Topological order: {:}", top_ord, depth=0, verbose=verbose)
    try:
        output = ID_internal(y,
                             x,
                             G,
                             Probability(),
                             top_ord,
                             tree=tree,
                             depth=0,
                             verbose=verbose)
        output.P.query = {'Y': y, 'X': x}
        return output.P
    except UnidentifiableEffect as e:
        return e
Example #5
0
    def __init__(self, thisData, thisCllrWrapper, thisConfig, thisExpName, thisDebug=True):
        self.data = thisData
        self.cllr = thisCllrWrapper
        self.config = thisConfig
        self._printToFilename = thisExpName
        self._expName = thisExpName
        self.debug = thisDebug
        Probability.__init__(self, self.data, self.config, self.debug)
        self.plotType = 'eer_plot'
        self.fig = None
        self.event = None

        metaDataValues = self.data.getMetaDataValues()
        metaColors = self.config.getMetaColors()
        self.colors = assignColors2MetaDataValue(metaDataValues, metaColors)

        self.eerData = self.computeProbabilities(self.eerFunc)
        self.eerValue = {}
        self.score = {}
        for thisMetaValue in sorted(self.colors.keys()):
            for metaValue, PD, PP, X in self.eerData:
                if thisMetaValue == metaValue:
                    try:
                        self.eerValue[metaValue], self.score[metaValue] = self.computeEer(PD, PP, X)
                        print("EER: {:.4f} % at score: {:.4f} and meta value: {}".format(
                            self.eerValue[metaValue] * 100, self.score[metaValue], metaValue)
                        )
                    except Exception as e:
                        print("Problem computing EER for %s: %s" % (thisMetaValue, e))
                    else:
                        self.eerValue[metaValue] *= 100
                    break
Example #6
0
def fromSong(song, cut=util.CUT, p=None):
    p = Probability()
    p.addSong(song, cut)
    h = []
    for part in song.parts:
        if len(part.flat) > cut:
            h.append(calculate(part, p))
    return h
Example #7
0
def main():
    filename = sys.argv[1] if len(sys.argv) > 1 else util.FAKE_FILE
    song = converter.parse(filename)
    p = Probability()
    p.addSong(song)

    h = fromSong(song, p=p)
    print(h)
    log.info("%d parts.", len(h))

    h = mean(p)
    print(h)
Example #8
0
 def __init__(self, thisData, thisEer, thisCllr, thisConfig, thisExpName, thisDebug=True):
     self.data = thisData
     self._eerObject = thisEer
     self._cllrObject = thisCllr
     self.config = thisConfig
     self._expName = thisExpName
     self._printToFilename = thisExpName
     self.debug = thisDebug
     self.plotType = 'roc_plot'
     self.fig = None
     self.event = None
     Probability.__init__(self, self.data, self.config, self.debug)
Example #9
0
def test_changing_sample_value_will_change_total_probability():
    data = [{1: TRUE, 2: FALSE, 3: TRUE}, {1: FALSE, 2: FALSE, 3: FALSE}]
    key = FALSE

    probability_service = Probability()
    probability_service.calculate_for_many_samples(data, key)
    results = probability_service.total

    assert results == 0.25

    probability_service.probability_samples[1] = 1

    assert probability_service.total == 0.5
Example #10
0
def main():
    playTournament(
        numOfGames=200,
        players=[NN(),
                 Rotate(),
                 BeatPrevious(),
                 Probability(),
                 Conditional()])
Example #11
0
 def __init__(self,
              thisData,
              thisEer,
              thisCllr,
              thisConfig,
              thisExpName,
              thisDebug=True):
     self.data = thisData
     self._eerObject = thisEer
     self._cllrObject = thisCllr
     self.config = thisConfig
     self._expName = thisExpName
     self._printToFilename = thisExpName
     self.debug = thisDebug
     self.plotType = 'roc_plot'
     self.fig = None
     self.event = None
     Probability.__init__(self, self.data, self.config, self.debug)
Example #12
0
    def __init__(self,
                 thisData,
                 thisCllrWrapper,
                 thisConfig,
                 thisExpName,
                 thisDebug=True):
        self.data = thisData
        self.cllr = thisCllrWrapper
        self.config = thisConfig
        self._printToFilename = thisExpName
        self._expName = thisExpName
        self.debug = thisDebug
        Probability.__init__(self, self.data, self.config, self.debug)
        self.plotType = 'eer_plot'
        self.fig = None
        self.event = None

        metaDataValues = self.data.getMetaDataValues()
        metaColors = self.config.getMetaColors()
        self.colors = assignColors2MetaDataValue(metaDataValues, metaColors)

        self.eerData = self.computeProbabilities(self.eerFunc)
        self.eerValue = {}
        self.eerScore = {}
        for thisMetaValue in sorted(self.colors.keys()):
            for metaValue, PD, PP, X in self.eerData:
                if thisMetaValue == metaValue:
                    try:
                        self.eerValue[metaValue], self.eerScore[
                            metaValue] = self.computeEer(PD, PP, X)
                        print(
                            "EER: {:.4f} % at score: {:.4f} and meta value: {}"
                            .format(self.eerValue[metaValue] * 100,
                                    self.eerScore[metaValue], metaValue))
                    except Exception as e:
                        print("Problem computing EER for %s: %s" %
                              (thisMetaValue, e))
                    else:
                        self.eerValue[metaValue] *= 100
                    break
Example #13
0
def dealer_class_test(card, exp):

    shoe = Shoe(8)
    prob = Probability(shoe)
    dealer = Dealer(shoe, prob)
    dealer.deal_card(card)
    probs = dealer.dealer_outcome_probs()

    # make sure probability isn't greated than one
    if round(sum(probs), 2) != 1.0:
        print(f"FAIL: dealer outcome probabilites < or > 1 : '{sum(probs)}'")
        return

    if exp == probs:
        print("\tSUCCESS: dealer outcome probabilites as expected")
    else:
        print("\tFAIL: dealer outcome probabilites NOT as expected")
Example #14
0
def probabilityPage():
    if request.method == "POST":
        n = int(request.form["n"])
        n = 10 if n > 10 else n
        r = int(request.form["r"])
        r = 10 if r > 10 else n
        p = request.form["p"]
        sp = p.split("/")
        sp = int(sp[0]) / int(sp[1])
        prob = Probability(n, r, sp)
        return render_template("probability.html",
                               page="probability",
                               n=n,
                               r=r,
                               p=p,
                               prob=prob)
    else:
        return render_template("probability.html", page="probability")
Example #15
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from argparse import ArgumentParser

from deck import Deck
from probability import Probability

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('slate', help='provide a slate file')
    parser.add_argument('deck', help='provide a deck file')
    parser.add_argument('-c',
                        '--count',
                        help='output the count of hands',
                        action='store_true')
    parser.add_argument('-p',
                        '--precise',
                        help='round less',
                        action='store_true')
    parser.add_argument('-v',
                        '--verbose',
                        help='increase output verbosity',
                        action='store_true')
    p = parser.parse_args()
    deck = Deck(file=p.deck, slate=p.slate, verbose=p.verbose)
    probability = Probability(count=p.count,
                              deck=deck,
                              precise=p.precise,
                              slate=p.slate)
    probability.run()
Example #16
0
def ID_internal(y, x, G, P, top_ord, tree, depth, verbose):
    try:
        #prettyPrint("Computing " + causalEffectStr(x,y) + " from " + str(P), depth=depth, verbose=verbose)

        # The following variables will be useful throughout the algorithm
        v = set(G.get_vertices())
        n = G.get_num_nodes()
        ancestors = set(G.get_ancestors(y))

        # Record the basic information about this call to the ID algorithm
        if len(P.var) == 0 and not (P.is_product or P.is_fraction):
            tree.call = Call(y, x, Probability(var=list(v)), G, line=None)
        else:
            tree.call = Call(y, x, P, G, line=None)

        # The topological order we were given may contain variables that don't exist in the subgraph we're working with.
        # We'll filter it to make sure it fits our subgraph.
        # We'll still keep the original around to sort sets we deal with throughout.
        new_top_ord = [v_i for v_i in top_ord[:] if v_i in v]

        ########## LINE 1 ##########
        if len(set(x)) == 0:
            if P.is_product or P.is_fraction:
                P_sumset = (v - y) | set(P.sumset)
                P.sumset = utilities.sort_subset_by_list(P_sumset, top_ord)
            else:
                P.var = utilities.sort_subset_by_list(y, top_ord)
            tree.call.line = 1
            tree.root = P
            return IDOutput(P, tree)

        ########## LINE 2 ##########
        nonancestors = v - ancestors
        #prettyPrint("Nonancestors of {:}: {:}", y, nonanc, depth=depth, verbose=verbose)
        if len(nonancestors) > 0:
            #prettyPrint("LINE 2: non-ancestors are {:}", nonanc, depth=depth, verbose=verbose)
            G_ancestors = G.induced_subgraph(ancestors)
            if (P.is_product or P.is_fraction):
                P_sumset = (v - ancestors) | set(P.sumset)
                P.sumset = utilities.sort_subset_by_list(P_sumset, top_ord)
            else:
                P.var = utilities.sort_subset_by_list(ancestors, top_ord)
            out = ID_internal(y,
                              x & ancestors,
                              G_ancestors,
                              P,
                              top_ord,
                              tree=Tree(),
                              depth=depth + 1,
                              verbose=verbose)
            tree.branch = [out.tree]
            tree.call.line = 2
            tree.call.anc = ancestors
            #prettyPrint(out.P, depth=depth, verbose=verbose)
            return IDOutput(P=out.P, tree=tree)

        ########## LINE 3 ##########
        G.post_intervention = G.construct_post_intervention_subgraph(x)
        ancestors_avoiding_x = G.post_intervention.get_ancestors(y)
        w = (v - x) - ancestors_avoiding_x
        if len(w) > 0:
            #prettyPrint("LINE 3", depth=depth, verbose=verbose)
            w_connected = w & G.post_intervention.get_all_connected_vertices(y)

            if len(w_connected) < len(w):
                v_new = v - (w - w_connected)
                G = G.induced_subgraph(v_new)

            out = ID_internal(y,
                              x | w_connected,
                              G,
                              P,
                              top_ord,
                              Tree(),
                              depth=depth + 1,
                              verbose=verbose)
            tree.branch = [out.tree]
            tree.call.line = 3
            tree.call.w = w
            tree.call.anc_post_intervention = ancestors_avoiding_x
            #prettyPrint(out.P, depth=depth, verbose=verbose)
            return IDOutput(P=out.P, tree=tree)

        # I want to get all the C-components of G[V\X].
        G.withoutX = G.induced_subgraph(v - x)
        components = G.withoutX.get_c_components()
        num_components = len(components)

        ########### LINE 4 ##########
        if num_components > 1:
            tree.call.line = 4
            #prettyPrint("LINE 4", depth=depth, verbose=verbose)
            recursive_calls = []
            for s_i in components:
                #prettyPrint("Executing ID_internal({:}, {:}, G, P)", s_i, v-s_i, depth=depth, verbose=verbose)
                recursive_calls.append(
                    ID_internal(s_i,
                                v - s_i,
                                G,
                                P,
                                top_ord[:],
                                Tree(),
                                depth=depth + 1,
                                verbose=verbose))
            tree.branch = [child.tree for child in recursive_calls]
            sumset = v - (y | x)
            P = Probability(sumset=utilities.sort_subset_by_list(
                sumset, top_ord),
                            is_product=True,
                            children=[call.P for call in recursive_calls])
            #prettyPrint(P, depth=depth, verbose=verbose)
            return IDOutput(P=P, tree=tree)

        ########### LINE 5 ##########
        # By the time we get here, we know that G[V\X] has a single C-component
        s = components[0]
        # prettyPrint("G[V\X] has only a single C-component: {{{:}}}".format(",".join(s)), depth=depth, verbose=verbose)
        components_G = G.get_c_components()
        # If G has only a single C-component, then the effect is unidentifiable.
        if len(components_G) == 1:
            #prettyPrint("LINE 5", depth=depth, verbose=verbose)
            raise UnidentifiableEffect(x, y, components_G[0], s)

        # Here we figure out which c-component of G is a superset of the c-component of G[V\X]
        s_prime = None
        for s_i in components_G:
            if s <= s_i:
                s_prime = s_i
                break

        ########### LINE 6 ##########
        # This is the case where S is a C-component of G itself.
        if len(s ^ s_prime) == 0:
            #prettyPrint("LINE 6", depth=depth, verbose=verbose)
            tree.call.line = 6
            tree.call.s = s
            product_list = [None for _ in range(len(s))]
            P_prod = Probability()
            s = utilities.sort_subset_by_list(s, top_ord)
            for (i, v_i) in enumerate(s):
                ix = new_top_ord.index(v_i)
                cond_set = new_top_ord[0:ix]
                if P.is_product:
                    P_prod = P.parse_joint(set([v_i]), set(cond_set), v,
                                           top_ord)
                else:
                    P_prod = P.copy()
                    P_prod.var = [v_i]
                    P_prod.cond = cond_set

                product_list[len(s) - i - 1] = P_prod

            if len(s) > 1:
                P_new_sumset = set(s) - y
                P_new = Probability(sumset=utilities.sort_subset_by_list(
                    P_new_sumset, top_ord),
                                    is_product=True,
                                    children=product_list)
                tree.root = P_new
                #prettyPrint(P_new, depth=depth, verbose=verbose)
                return IDOutput(P=P_new, tree=tree)

            if P_prod.is_product or P_prod.is_fraction:
                P_prod_sumset = set(P_prod.sumset) | (set(s) - y)
                P_prod.sumset = utilities.sort_subset_by_list(
                    P_prod_sumset, top_ord)
            else:
                P_prod_var = set(P_prod.var) - (set(P_prod.sumset) |
                                                (set(s) - y))
                P_prod.var = utilities.sort_subset_by_list(P_prod_var, top_ord)
            tree.root = P_prod_var
            #prettyPrint(P_prod, depth=depth, verbose=verbose)
            return IDOutput(P=P_prod, tree=tree)

        ########## LINE 7 ##########
        # This is the case where S is a subset of larger C-component S'
        tree.call.s = s
        tree.call.line = 7
        # Note that we're going to set s <- s_prime to match the code from Tikka and Karvanen.
        # I think it's confusing, but I'll try it this way first.
        s = utilities.sort_subset_by_list(s_prime, top_ord)
        tree.call.s_prime = s

        G.s = G.induced_subgraph(s)
        product_list = [None for _ in range(len(s))]
        for (i, v_i) in enumerate(s):
            ix = new_top_ord.index(v_i)
            cond_set = new_top_ord[0:ix]
            P_prod = P.copy()
            P_prod.var = [v_i]
            P_prod.cond = cond_set
            product_list[len(s) - i - 1] = P_prod

        x_new = set(s) & x
        out = None
        if len(s) > 1:
            P_recursive = Probability(is_product=True, children=product_list)
            out = ID_internal(y,
                              x_new,
                              G.s,
                              P_recursive,
                              top_ord,
                              Tree(),
                              depth=depth + 1,
                              verbose=verbose)
        else:
            out = ID_internal(y,
                              x_new,
                              G.s,
                              product_list[0],
                              top_ord,
                              Tree(),
                              depth=depth + 1,
                              verbose=verbose)

        tree.branch = [out.tree]
        #prettyPrint(out.P, depth=depth, verbose=verbose)
        return IDOutput(P=out.P, tree=tree)

    except UnidentifiableEffect as e:
        raise e
Example #17
0
from probability import Probability
from repositories.answers_repository import CSVAnswersRepository
from repositories.questions_repository import CSVQuestionsRepository
from survey import Survey

if __name__ == '__main__':
    questions_repository = CSVQuestionsRepository('data/questions.csv')
    answers_repository = CSVAnswersRepository('data/first_survey.csv')
    probability_service = Probability()

    s = Survey(questions_repository, answers_repository, probability_service)
    s.start()
Example #18
0
def test_should_calculate_probability(data, key, expected):

    assert Probability().calculate_for_key(data, key) == expected
Example #19
0
def test_should_calculate_probability_for_many_samples(data, key, expected):
    probability_service = Probability()
    results = probability_service.calculate_for_many_samples(data, key)

    assert results == expected