Exemple #1
0
def full(scaledparams, x, y, zstates):
    params = hyperparameters.descale(scaledparams)
    pi = params[0]
    mu = params[1]
    sigma = params[2]
    sigmabg = params[3]
    tau = params[4]
    nvar = x.shape[0]
    nsample = x.shape[1]
    constpi = math.pi
    marglik_k = 0
    lmlzlist = list()
    logk = 0

    sigma2 = sigma * sigma
    sigmabg2 = sigmabg * sigmabg
    muz0 = np.zeros(nvar)
    sigz0 = np.repeat(sigmabg2, nvar)

    for z in zstates:
        nz = len(z)
        muz = muz0.copy()
        muz[z] = mu
        sigz = sigz0.copy()
        sigz[z] = sigma2

        # P(z | theta)
        log_probz = nz * np.log(pi) + (nvar - nz) * np.log(1 - pi)

        # N(y | Mz, Sz)
        #Sz = np.einsum('ki, k, kj -> ij', x, sigz, x)
        Sz = mat3mul(x.T, np.diag(sigz), x)
        Sz[np.diag_indices_from(Sz)] += 1 / tau
        Mz = np.einsum('ij, i -> j', x, muz)
        logdetS = np.linalg.slogdet(Sz)[1]
        invS = np.linalg.inv(Sz)
        y_minus_M = y - Mz
        nterm = np.einsum('i, ij, j', y_minus_M, invS, y_minus_M)
        log_normz = -0.5 * (logdetS + (nsample * np.log(2 * constpi)) + nterm)

        # Combine the log values
        lmlzlist.append(log_probz + log_normz)

    lmlzarr = np.array(lmlzlist)
    logk = np.max(lmlzarr)
    marglik_k = np.sum(np.exp(lmlzarr - logk))
    logmarglik = logk + np.log(marglik_k)
    return lmlzlist, -logmarglik
Exemple #2
0
def raw_comps(scaledparams, x, y, zstates):
    params = hyperparameters.descale(scaledparams)
    pi = params[0]
    mu = params[1]
    sigma = params[2]
    sigmabg = params[3]
    tau = params[4]

    logmarglik, der, pz, l = iterative_inverse(pi,
                                               mu,
                                               sigma,
                                               sigmabg,
                                               tau,
                                               x,
                                               y,
                                               zstates,
                                               grad=False)

    return l
Exemple #3
0
def func_grad(scaledparams, x, y, zstates):
    params = hyperparameters.descale(scaledparams)
    pi = params[0]
    mu = params[1]
    sigma = params[2]
    sigmabg = params[3]
    tau = params[4]

    logmarglik, der, pz, l = iterative_inverse(pi, mu, sigma, sigmabg, tau, x,
                                               y, zstates)
    der = hyperparameters.gradscale(params, der)

    #delta = 0.0001
    #newparams = scaledparams.copy()
    #newparams[4] += delta
    #newtau = hyperparameters.descale(newparams)[4]
    #newm, dummy1, dummy2 = iterative_inverse(pi, mu, sigma, sigmabg, newtau, x, y, zstates)
    #tau_grad = (newm - logmarglik) / delta
    #sigmatau = np.sqrt(1 / tau)
    #print ("Params here are: {:g} {:g} {:g} {:g} {:g}".format(pi, mu, sigma, sigmabg, sigmatau))
    #print ("Derivative of tau by brute force is {:f}".format(tau_grad))
    #print ("Derivative of tau from equation is {:f}".format(der[4]))

    return -logmarglik, -der
Exemple #4
0
def model_exp(scaledparams, x, y, zstates):
    params = hyperparameters.descale(scaledparams)
    success, logmarglik, zprob, grad, zexp = czcompgrad(params, x, y, zstates, get_grad=False, get_exp=True)
    zexp = zexp.reshape(len(zstates), x.shape[0])
    return zprob, zexp
Exemple #5
0
def prob_comps(scaledparams, x, y, zstates):
    params = hyperparameters.descale(scaledparams)
    success, logmarglik, zprob, grad, zexp = czcompgrad(params, x, y, zstates, get_grad=False)
    return zprob
Exemple #6
0
def func_grad(scaledparams, x, y, zstates):
    params = hyperparameters.descale(scaledparams)
    success, logmarglik, zprob, grad, zexp = czcompgrad(params, x, y, zstates, get_grad=True)
    grad = hyperparameters.gradscale(params, grad)
    return success, -logmarglik, -grad
Exemple #7
0
 def params(self):
     return hyperparameters.descale(self._params)