示例#1
0
    def updateProbs(self, probs: Tuple[float]):
        # Updates probability distribution with new cumulative probability distribution

        # Checks we're not passing anything that's not cumulative.
        sums = sum(probs)
        assert sums <= 1.00000005 and sums >= .99999995, "Probs must be a cumulative probability distribution."

        self.cmlProbs = genCmlProbs(probs)
示例#2
0
    def __init__(self, dimen: Tuple[int]):
        self.dimen = dimen

        # Caclulates the size of space
        self.range = 1
        for N in self.dimen:
            self.range *= N

        # Generates probabilities for later measurements
        self.cmlProbs = genCmlProbs(genProbs(self.range))
示例#3
0
    def __init__(self, dimen: Tuple[int], K: int):
        ## Dimen is representation of measurement space M
        ## K is amount of classes to be assigned

        self.dimen = dimen
        self.probs = genProbs(K)  # For testing
        self.cmlProbs = genCmlProbs(self.probs)
        self.range = K

        # Amount of possible measures is cardinality of M as represented by dimen
        self.mSpaceSize = 1
        for N in dimen:
            self.mSpaceSize *= N

        self.tags = None
        self.genTags()
示例#4
0
def genBiasTags(measures: Tuple[int],
                posteriors: Tuple[Tuple[float]]) -> Tuple[int]:
    # Assigns measures by posterior conditional probability distribution p(c|d)
    # Note, we can't use the classAssign function as it uses same class probabilities
    # for all measures - and thus produces overlap.
    Z = len(measures)
    tags = []

    # For each measure
    for d in range(Z):

        # We find its set of class conditionals
        conds = posteriors[measures[d]]

        # We generate a cumulative probability distribution
        cmlConds = genCmlProbs(conds)

        # generate a sample
        tag = genSamples(1, cmlConds)

        # Adds sample to tags (since outputs a tuple we need to index)
        tags.append(tag[0])

    return tuple(tags)
示例#5
0
 def updateProbs(self, probs: Tuple[float]):
     self.cmlProbs = genCmlProbs(probs)
示例#6
0
def main():
    #### Unit tests

    ####### Tests for sampling function.
    # Tests for distributions to be within range
    for i in range(1, 1000):
        sampSize = randint(1, 1000)
        spaceRang = randint(3, 1000)
        a = genProbs(spaceRang)
        b = genCmlProbs(a)
        c = genSamples(sampSize, b)
        for j in c:
            assert j <= (spaceRang - 1) and j >= 0

    # Tests for suitable randomness so same samples aren't being generated.
    for _ in range(1000):

        sampSize = randint(2, 1000)
        spaceRang = randint(3, 1000)  # Provides sufficient space for variety

        a = genProbs(spaceRang)
        b = genCmlProbs(a)

        c = genSamples(sampSize, b)
        z = genSamples(sampSize, b)

        assert c != z, c

#####Tests genMeas method
# Checks for measurement space to be of proper length
    Z = randint(2, 1000)
    dimen = genSpace(6)
    size = randint(2, 1000)
    generator = MeasurementGenerator(dimen)
    x = generator.genMeas(size)
    assert len(x) == size

    # Checks for values to actually exist in measurement space
    for _ in range(1, 1000):
        maxValue = 1
        for i in dimen:
            maxValue *= i
        for j in x:
            assert j <= maxValue - 1, maxValue - j


###### Tests ClassAssign class
# Verifies no class is assigned outside class values
    for _ in range(1000):
        dimen = genSpace(5)
        maxValue = 1
        for i in dimen:
            maxValue *= i
        tagSize = randint(1, 100)
        tagAssign = ClassAssign(dimen, tagSize)

        for j in tagAssign.tags:
            assert j <= tagSize - 1, (j, tagSize - 1)

        # Verfies assignment funciton is of proper length.
        assert len(tagAssign.tags) == maxValue
示例#7
0
 def regenTags(self, probs: List[float]):
     # Regenerates tags given probability set.
     self.cmlProbs = genCmlProbs(probs)
     self.genTags()