Ejemplo n.º 1
0
def renyi(probs):
    e=0
    one=numberType.const(1.0)
    d=one/(one-alpha)
    for p in probs:
        e=e+numberType.pow(p,alpha)
    return numberType.log(e)*d
Ejemplo n.º 2
0
def randomEntropyData(seed,N,dag,falsePos=False,falseNeg=False,maxCount=10):
    random.seed(seed)
        
    loc=random.randint(0,N-1)
    
    locList=[False for x in range(N)]
    locList[loc]=True

    detectable=listOr(dag.anyUpto(locList),locList)
        


    counts=[(random.randint(0,maxCount),random.randint(0,maxCount)) for x in range(N)]

    # eliminate impossible counts for this test
    if not falsePos:
        counts=[ cond(not detectable[x],(counts[x][0],0),counts[x])  for x in range(N)]
    if not falseNeg:
        counts=[ cond(detectable[x],(0,counts[x][1]),counts[x])  for x in range(N)]

    UlocPrior=[random.random() for x in range(N)]
    norm=sum(UlocPrior)
    locPrior=[numberType.const(i/norm) for i in UlocPrior]
    
    return (counts,locPrior)
Ejemplo n.º 3
0
def entropiesFast(counts,locPrior,likelihoodsObj,d):

#    d=dag.linearTestDag(len(locPrior))

    one=numberType.const(1.0)
    renyiFactor=one/(one-entropy.alpha)

    lk=likelihoodsObj.calc(counts,locPrior,entropy.alpha,d)

    (rsum,osum)=lk.orig()
    currEntropy=rsum/numberType.pow(osum,entropy.alpha)
    currEntropy =numberType.log(currEntropy)*renyiFactor
    
    entropyResults=[]
    findProbs=[]


    for i in xrange(len(locPrior)): 

        (findProb,renyLksFoundTot,renyLksNFoundTot,evDProb,NfoundNorm)=lk[i]
        findProbs.append(findProb)
        #entropyFound:normalise
        try: 
            eFound=renyLksFoundTot/numberType.pow(evDProb,entropy.alpha)
            
            eFound=numberType.log(eFound)*renyiFactor
        except numberType.zeroDivisionError:
            eFound=0
        except numberType.overflowError:
            eFound=0


        #entropyNotFound:normalise
        try:
            eNotFound=renyLksNFoundTot/numberType.pow(NfoundNorm,entropy.alpha)            
            eNotFound=numberType.log(eNotFound)*renyiFactor
        except numberType.zeroDivisionError:
            eNotFound=0
        except numberType.overflowError:
            eNotFound=0

        # expected entropy after testing at i:
        if debug: print "b",eFound,eNotFound,findProb
        
        eResult=eFound*findProb+eNotFound*(1-findProb)
        
        entropyResults.append(eResult)

    return (currEntropy,entropyResults,findProbs)
Ejemplo n.º 4
0
def entropies(counts,locPrior,likelihoodsObj,dag):
    (locProbs,evProb)=likelihoodsObj.probs(counts,locPrior,dag)
    deb_ef=[]
    deb_enf=[]
    findProbs=[]
    entropyResults=[]
    entropyFunc=entropy.renyi
    currEntropy=entropyFunc(locProbs)

    if debug: print "ac",counts

    for i in xrange(len(locProbs)):
        testFound=copy.copy(counts)
        testNotFound=copy.copy(counts)
        
        (t,d)=counts[i]
        testFound[i]=(t,d+1)
        testNotFound[i]=(t+1,d)
        
        try:
            (probsIfFound,evDProb)=likelihoodsObj.probs(testFound,locPrior,dag)
            eFound=entropyFunc(probsIfFound)
        except Impossible:
            eFound=numberType.zero
            evDProb=numberType.zero
        
        try:
            (probsIfNotFound,junk)=likelihoodsObj.probs(testNotFound,locPrior,dag)
            eNotFound=entropyFunc(probsIfNotFound)
        except Impossible:
            eNotFound=numberType.zero
        
        # probability of finding at i:
        
        findProb=evDProb/evProb
        findProbs.append(findProb)
        if debug: print "a",eFound,eNotFound,evDProb,probsIfFound
        
        # expected entropy after testing at i:
        
        eResult=eFound*findProb+eNotFound*(numberType.const(1)-findProb)
        
        entropyResults.append(eResult)

    return (currEntropy,entropyResults,findProbs)
Ejemplo n.º 5
0
    def __init__(self,
                 locPrior,
                 certainty,
                 interactor,
                 likelihoodsObj,
                 dag,
                 strategy=greedyStrat,
                 skipProbsFunc=skipProbability.skipProbsSimple):

        
        self.locPrior=numberType.copyList(locPrior)
        self.certainty=numberType.const(certainty)
        self.counts=[(0,0) for p in locPrior]
        self.skipProbsFunc=skipProbsFunc
        self.skipped=[False for p in locPrior]
        self.dag=dag
        self.skipProbs = self.skipProbsFunc(self.skipped,self.dag)
        self.interactor=interactor
        self.total=0
        self.likelihoodsObj=likelihoodsObj
        self.strategy=strategy
Ejemplo n.º 6
0
        return 1
    return fact(x-1)



# logGamma
def logGamma(x):
    if(x==0):
        raise "Gamma called on zero!"
    if(x==1):
        return 0
    return logFact(x-1)

# standard factorial function

factMemo=[numberType.const('1')]

# standard factorial function
def fact(x):
    while(x>=len(factMemo)):
        factMemo.append(factMemo[-1]*len(factMemo))
    return factMemo[x]

# log-factorial function

logFactMemo=[numberType.const('0')]

def logFact(x):
    while(x>=len(logFactMemo)):
        logFactMemo.append(logFactMemo[-1]+numberType.log(len(logFactMemo)))
    return logFactMemo[x]
Ejemplo n.º 7
0
 def __init__(self,decayFactor):
     self.decayFactor=decayFactor/numberType.const(2)
Ejemplo n.º 8
0
 def __init__(self,decayFactor):
     self.decayFactor=numberType.pow(decayFactor,numberType.const(0.5))
Ejemplo n.º 9
0
#
#    BBChop 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 BBChop.  If not, see <http://www.gnu.org/licenses/>.
import evidence
import random

import dag
import testCases
import numberType

epsilon=numberType.const(0.000000001)
epCoeff=1+epsilon
epDelta=numberType.const(0.00000000000001)



def rough_eq(a,b):
    r=True
    minS_a=a-epDelta
    maxS_a=a+epDelta
    if a<0:
        minP_a=a*epCoeff
        maxP_a=a/epCoeff
    else:
        minP_a=a/epCoeff
        maxP_a=a*epCoeff
Ejemplo n.º 10
0
        return 1
    return fact(x - 1)


# logGamma
def logGamma(x):
    if (x == 0):
        raise "Gamma called on zero!"
    if (x == 1):
        return 0
    return logFact(x - 1)


# standard factorial function

factMemo = [numberType.const('1')]


# standard factorial function
def fact(x):
    while (x >= len(factMemo)):
        factMemo.append(factMemo[-1] * len(factMemo))
    return factMemo[x]


# log-factorial function

logFactMemo = [numberType.const('0')]


def logFact(x):
Ejemplo n.º 11
0
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.
#
#    BBChop 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 BBChop.  If not, see <http://www.gnu.org/licenses/>.

import numberType
def shannon(probs):
    e=0
    for p in probs:
        if(p>0):
            e-=p*numberType.log(p)
    return e



alpha=numberType.const('1.2')

def renyi(probs):
    e=0
    one=numberType.const(1.0)
    d=one/(one-alpha)
    for p in probs:
        e=e+numberType.pow(p,alpha)
    return numberType.log(e)*d
Ejemplo n.º 12
0
def prod(l):
    return reduce(operator.mul,l,numberType.const(1.0))