Пример #1
0
def chi2_test_statistic(M, Obs, K, num_M, num_Obs):
    #Getting frequencies from observations
    Ns = T.dot(Obs, T.ones((K, 1)))
    p = Obs / Ns

    #Find the zeros so we can deal with them later
    pZEROs = T.eq(p, 0)
    mZEROs = T.eq(M, 0)

    #log probabilities, with -INF as log(0)
    lnM = T.log(M + mZEROs) - INF * mZEROs
    lnp = T.log(p + pZEROs) - INF * pZEROs

    #Using kroneker products so every row of M hits every row of P in the difference klnM - kln
    O_ones = T.ones((num_Obs, 1))
    M_ones = T.ones((num_M, 1))
    klnM = kron(lnM, O_ones)
    klnP = kron(M_ones, lnp)
    klnP_M = klnP - klnM
    kObs = kron(M_ones, Obs)

    G = 2.0 * T.dot(klnP_M, kObs.T)

    G = G * T.identity_like(G)
    G = T.dot(G, T.ones((num_M * num_Obs, 1)))
    G = T.reshape(G, (num_M, num_Obs))

    #The following quotient improves the convergence to chi^2 by an order of magnitude
    #source: http://en.wikipedia.org/wiki/Multinomial_test

    #numerator = T.dot(- 1.0/(M + 0.01),T.ones((K,1))) - T.ones((num_M,1))
    #q1 = T.ones((num_M,num_Obs)) + T.dot(numerator,1.0/Ns.T/6.0)/(K-1.0)

    return G  #/q1
Пример #2
0
def chi2_test_statistic(M, Obs, K, num_M, num_Obs):
    #Getting frequencies from observations
    Ns = T.dot(Obs,T.ones((K,1)))
    p = Obs/Ns
        
    #Find the zeros so we can deal with them later
    pZEROs = T.eq(p, 0)
    mZEROs = T.eq(M, 0)
    
    #log probabilities, with -INF as log(0)
    lnM = T.log(M + mZEROs) - INF*mZEROs
    lnp = T.log(p + pZEROs) - INF*pZEROs


    #Using kroneker products so every row of M hits every row of P in the difference klnM - kln
    O_ones = T.ones((num_Obs,1))
    M_ones = T.ones((num_M,1))
    klnM = kron(lnM,O_ones)
    klnP = kron(M_ones, lnp)
    klnP_M = klnP - klnM
    kObs = kron(M_ones, Obs)
    
    G = 2.0*T.dot(klnP_M ,kObs.T)
    
    G = G*T.identity_like(G)
    G = T.dot(G,T.ones((num_M*num_Obs,1)))   
    G = T.reshape(G,(num_M,num_Obs))
    
    #The following quotient improves the convergence to chi^2 by an order of magnitude
    #source: http://en.wikipedia.org/wiki/Multinomial_test
    
    #numerator = T.dot(- 1.0/(M + 0.01),T.ones((K,1))) - T.ones((num_M,1))    
    #q1 = T.ones((num_M,num_Obs)) + T.dot(numerator,1.0/Ns.T/6.0)/(K-1.0)
        
    return G#/q1 
Пример #3
0
def sample_h_given_v_2wise(v, W, Wh, bh, nh):
	phi = T.dot(v, W) + bh
	ephi = T.exp(phi)

	adder = np.zeros((nh/2, nh), dtype=theano.config.floatX)
	for i in xrange(len(adder)):
		adder[i, 2*i] = 1
		adder[i, 2*i+1] = 1
	adder = theano.shared(adder)
	# wobble =  1 + exp(phi_2i) + exp(phi_{2i+1}) + exp(phi_2i + phi_{21+1} + Wh_i)
	# p(h_2i = 1 | v) = (exp(phi_2i) + exp(phi_2i + phi_{21+1} + Wh_i ) / wobble
	# p(h_{2i+1} = 1 | v) = (exp(phi_2i) + exp(phi_2i + phi_{2i+1} + Wh_i )) / wobble
	# the second term is the same in both - the pair term.  but it must be broadcasted (the kron!)
	# dotting by adder returns a vector of half the size of sums of pairs of elements

	pairsum = T.dot(ephi, adder.T)
	first = ephi.T[T.arange(0, nh, 2)].T
	pairprod = pairsum*first - first**2
	pairterm = pairprod*T.exp(Wh)

	wobble = 1 + pairsum + pairterm

	pairterm_broadcast = kron(pairterm.dimshuffle(0, 'x'), T.ones(2))
	wobble_broadcast = kron(wobble.dimshuffle(0, 'x'), T.ones(2))

	prop_up = (ephi + pairterm_broadcast) / wobble_broadcast

	h = theano_rng.binomial(n=1, p = prop_up, dtype=theano.config.floatX, size=(nh,), ndim=1)

	return h
Пример #4
0
def underfit_choice(Agression,
                    num_alpha,
                    Models,
                    Obs,
                    K,
                    num_M,
                    num_Obs,
                    pvalues,
                    alpha,
                    Preds=None):
    #Switch for pvalue algorithm.. first is fast and an approximation, second slow and exact

    if Preds == None:
        Preds = predictiveness_profiles(Models, K, num_M)

    # Letting P denote the whole predictiveness matrix, we will tile num_alpha*num_Obs of them together as such:
    #   PPPPPPPPPPPPPPPPPP..
    #Then the pvalues will be broadcastand tiled so that if we number blocks of pvalues their ordering will be
    #   123412341234,, with each block having the pvalues of generating models behind the predictiveness profiles
    #Then alpha will be tiled so that there's a new alpha for each time the pvalues repeat:
    #111111222222333333...  The idea is to get every triple (alpha, observation, P)
    #
    #This visualization might help:
    #
    #PPPPPPPPPPPP
    #123412341234
    #111122223333
    #
    #Finally the code:

    Preds = kron(T.ones((1, num_alpha * num_Obs)), Preds)
    pvalues = kron(pvalues, T.ones((1, num_M)))
    pvalues = kron(T.ones((1, num_alpha)), pvalues)
    alpha = kron(T.reshape(alpha, (1, num_alpha)),
                 T.ones((num_M, num_M * num_Obs)))

    # This "knocks out" rejected universes from predictiveness profiles
    Scores = Preds + INF * (pvalues < alpha)
    # The worst case predictiveness for each alpha is found
    Scores = T.min(Scores, axis=0)
    # Rearranging to put vary the alpha between the rows
    Scores = T.reshape(Scores, (num_alpha, num_M * num_Obs))
    # Integrating out the information/significance levels with agression
    Scores = T.dot(Agression.T, Scores)
    # Finally get the num_M by num_Obs matrix of scores for each observation
    Scores = T.reshape(Scores, (num_Obs, num_M)).T
    # Return the model choice
    Choice = T.argmax(Scores, axis=0)

    return Choice
Пример #5
0
    def min_risk_choice(Posterior):

        #The Loss function is a function of the predictiveness profiles
        Preds = predictiveness_profiles(Models, K, num_M)
        
        Loss = ifelse(T.eq(Choice_type, 1), T.pow(1.0 - Preds,2), ifelse(T.eq(Choice_type, 2), T.abs_(1.0 - Preds), - Preds))             
        
        #Kroneckering Loss up num_Obs times (tile Loss, making it num_M by num_M*num_Obs)
        Loss = kron(T.ones((1,num_Obs)), Loss)        
        #Kroneckering up the Posterior, making it num_M by num_Obs*numM
        Posterior = kron(Posterior, T.ones((1,num_M)))

        #Dotting and reshaping down to give num_M by num_Obs expected loss matrix
        Expected_Loss = T.dot(T.ones((1,num_M)),Posterior*Loss)            
        Expected_Loss = T.reshape(Expected_Loss, (num_Obs,num_M)).T
        
        #Choice minimizes risk
        Choice = T.argmin(Expected_Loss, axis = 0) 
        return Choice 
Пример #6
0
    def test_perform(self):
        x = tensor.dmatrix()
        y = tensor.dmatrix()
        f = function([x, y], kron(x, y))

        for shp0 in [(8, 6), (5, 8)]:
            for shp1 in [(5, 7), (3, 3)]:
                a = numpy.random.rand(*shp0)
                b = numpy.random.rand(*shp1)
                out = f(a, b)
                assert numpy.allclose(out, scipy.linalg.kron(a, b))
Пример #7
0
    def test_perform(self):
        x = tensor.dmatrix()
        y = tensor.dmatrix()
        f = function([x, y], kron(x, y))

        for shp0 in [(8, 6), (5, 8)]:
            for shp1 in [(5, 7), (3, 3)]:
                a = numpy.random.rand(*shp0)
                b = numpy.random.rand(*shp1)
                out = f(a, b)
                assert numpy.allclose(out, scipy.linalg.kron(a, b))
Пример #8
0
def underfit_choice(Agression, num_alpha, Models, Obs, K, num_M, num_Obs, pvalues, alpha, Preds = None):
    #Switch for pvalue algorithm.. first is fast and an approximation, second slow and exact
    
    if Preds == None:
        Preds = predictiveness_profiles(Models, K, num_M)    
    

    # Letting P denote the whole predictiveness matrix, we will tile num_alpha*num_Obs of them together as such:
    #   PPPPPPPPPPPPPPPPPP..
    #Then the pvalues will be broadcastand tiled so that if we number blocks of pvalues their ordering will be
    #   123412341234,, with each block having the pvalues of generating models behind the predictiveness profiles 
    #Then alpha will be tiled so that there's a new alpha for each time the pvalues repeat:
    #111111222222333333...  The idea is to get every triple (alpha, observation, P)
    #
    #This visualization might help:
    #
    #PPPPPPPPPPPP
    #123412341234
    #111122223333
    #
    #Finally the code:
    
    Preds = kron(T.ones((1,num_alpha*num_Obs)),Preds)
    pvalues = kron(pvalues,T.ones((1,num_M)))
    pvalues = kron(T.ones((1,num_alpha)),pvalues)    
    alpha = kron(T.reshape(alpha, (1, num_alpha)),T.ones((num_M,num_M*num_Obs)))

    # This "knocks out" rejected universes from predictiveness profiles
    Scores = Preds + INF*(pvalues < alpha) 
    # The worst case predictiveness for each alpha is found
    Scores = T.min(Scores, axis = 0)
    # Rearranging to put vary the alpha between the rows 
    Scores = T.reshape(Scores,(num_alpha,num_M*num_Obs))
    # Integrating out the information/significance levels with agression
    Scores = T.dot(Agression.T,Scores)
    # Finally get the num_M by num_Obs matrix of scores for each observation
    Scores = T.reshape(Scores,(num_Obs,num_M)).T
    # Return the model choice
    Choice = T.argmax(Scores, axis = 0)
    
    return Choice
Пример #9
0
    def min_risk_choice(Posterior):

        #The Loss function is a function of the predictiveness profiles
        Preds = predictiveness_profiles(Models, K, num_M)

        Loss = ifelse(
            T.eq(Choice_type, 1), T.pow(1.0 - Preds, 2),
            ifelse(T.eq(Choice_type, 2), T.abs_(1.0 - Preds), -Preds))

        #Kroneckering Loss up num_Obs times (tile Loss, making it num_M by num_M*num_Obs)
        Loss = kron(T.ones((1, num_Obs)), Loss)
        #Kroneckering up the Posterior, making it num_M by num_Obs*numM
        Posterior = kron(Posterior, T.ones((1, num_M)))

        #Dotting and reshaping down to give num_M by num_Obs expected loss matrix
        Expected_Loss = T.dot(T.ones((1, num_M)), Posterior * Loss)
        Expected_Loss = T.reshape(Expected_Loss, (num_Obs, num_M)).T

        #Choice minimizes risk
        Choice = T.argmin(Expected_Loss, axis=0)
        return Choice
Пример #10
0
    def test_perform(self):
        assert imported_scipy, (
            "Scipy not available. Scipy is needed for TestKron")
        x = tensor.dmatrix()
        y = tensor.dmatrix()
        f = function([x, y], kron(x, y))

        for shp0 in [(8, 6), (5, 8)]:
            for shp1 in [(5, 7), (3, 3)]:
                a = numpy.random.rand(*shp0)
                b = numpy.random.rand(*shp1)
                out = f(a, b)
                assert numpy.allclose(out, scipy.linalg.kron(a, b))
Пример #11
0
 def test_numpy_2d(self):
     for shp0 in [(2, 3)]:
         for shp1 in [(6, 7)]:
             if len(shp0) + len(shp1) == 2:
                 continue
             x = tensor.tensor(dtype='floatX',
                               broadcastable=(False,) * len(shp0))
             y = tensor.tensor(dtype='floatX',
                               broadcastable=(False,) * len(shp1))
             f = function([x, y], kron(x, y))
             a = numpy.asarray(self.rng.rand(*shp0)).astype(floatX)
             b = self.rng.rand(*shp1).astype(floatX)
             out = f(a, b)
             assert numpy.allclose(out, numpy.kron(a, b))
Пример #12
0
    def test_perform(self):
        if not imported_scipy:
            raise SkipTest('kron tests need the scipy package to be installed')

        for shp0 in [(2,), (2, 3), (2, 3, 4), (2, 3, 4, 5)]:
            for shp1 in [(6,), (6, 7), (6, 7, 8), (6, 7, 8, 9)]:
                if len(shp0) + len(shp1) == 2:
                    continue
                x = tensor.tensor(dtype='floatX',
                                  broadcastable=(False,) * len(shp0))
                y = tensor.tensor(dtype='floatX',
                                  broadcastable=(False,) * len(shp1))
                f = function([x, y], kron(x, y))
                a = numpy.asarray(self.rng.rand(*shp0)).astype(floatX)
                b = self.rng.rand(*shp1).astype(floatX)
                out = f(a, b)
                assert numpy.allclose(out, scipy.linalg.kron(a, b))