This file is indexed.

/usr/share/pyshared/pypsignifit/psigsimultaneous.py is in python-pypsignifit 3.0~beta.20120611.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python

from scipy import stats
from scipy.special import gamma,digamma,polygamma
from numpy import log,mean,var,exp,array
from math import sqrt

__all__ = ["derive_informed_priors"]
__doc__ = """Routines to derive informed priors for quasi-simultaneous bayesian inference

These routines are described in more detail in the document located in

documents/simultaneous.pdf

In short, these routines take a number of fits that were performed with (improper) flat priors
and derive an list of informed priors. These informed priors can be used to perform the bayesian
inference a second time, this time with non flat prios, such that in the end the posterior
distributions of certain parameters are equal.
"""

def normpdf ( x, prm ):
    """normal pdf

    Parameters
    ----------
    x : array
        array on which the normal pdf should be evaluated
    prm : sequence
        pair of mean and variance for the gaussian

    Returns
    -------
    p : array
        an array of densities at the positions of x
    """
    return stats.norm.pdf ( x, prm[0], sqrt(prm[1]) )

def gammapdf ( x, prm ):
    """gamma pdf

    Parameters
    ----------
    x : array
        array on which the gamma pdf should be evaluated
    prm : sequence
        pair of k (shape) and theta (scale) parameters for the gamma distribution

    Returns
    -------
    p : array
        an array of densities at the positions of x
    """
    k,th = prm
    return x**(k-1) * exp ( - x/th )/(gamma(k)*th**k)

def betapdf ( x, prm ):
    """beta pdf

    Parameters
    ----------
    x : array
        array on which the gamma pdf should be evaluated
    prm : sequence
        pair of alpha (prior successes) and beta (prior misses) parameters for the beta distribution

    Returns
    -------
    p : array
        an array of densities at the positions of x
    """
    al,bt = prm
    return stats.beta.pdf(x, al, bt )

def fitgauss ( samples ):
    """fit a normal distribution using maximum likelihood

    Parameters
    ----------
    samples : array
        array of samples on which the distribution should be fitted

    Returns
    -------
    prm : sequence
        pair of mean and variance for the fitted gaussian
    l : double
        likelihood of the data at the fitted parameter values
    """
    m = mean ( samples )
    s = var ( samples )
    l = sum(log(normpdf(samples,(m,s))))
    return (m,s),l

def fitgamma ( samples ):
    """fit a gamma distribution using maximum likelihood

    Parameters
    ----------
    samples : array
        array of samples on which the distribution should be fitted

    Returns
    -------
    prm : sequence
        pair of k (shape) and theta (scale) parameters for the fitted gamma distribution
    l : double
        likelihood of the data at the fitted parameter values
    """
    s = log ( mean(samples) ) - mean ( log(samples) )
    k = 3 - s + sqrt ( (s-3)**2 + 24*s)
    k /= 12 * s
    for i in xrange ( 5 ):
        k -= ( log(k) - digamma ( k ) -s ) / ( 1./k - polygamma( 1, k ) )
    th = mean ( samples ) / k
    l = sum(log(gammapdf ( samples, (k,th) ) ) )
    return (k,th),l

def fitbeta ( samples ):
    """fit a beta distribution using maximum likelihood

    Parameters
    ----------
    samples : array
        array of samples on which the distribution should be fitted

    Returns
    -------
    prm : sequence
        pair of alpha (prior successes) and beta (prior misses) parameters for the beta distribution
    l : double
        likelihood of the data at the fitted parameter values
    """
    m = mean ( samples )
    s = var ( samples )
    al = m * ( m*(1-m)/s - 1 )
    bt = (1-m) * ( m*(1-m)/s - 1 )
    l = sum(log(betapdf(samples, (al, bt) ) ) )
    return (al,bt),l

def combinegauss ( prm ):
    """combine gaussian parameter estimates to give the product prior

    Parameters
    ----------
    prm : sequence
        a sequence of pairs of parameter estimates

    Returns:
    --------
    mubar,varbar : double
        mean an variance of the product prior
    """
    prm = array ( prm )
    varbar = 1./sum(1./prm[:,1])
    mubar = sum(prm[:,0]/prm[:,1]) * varbar
    return mubar,varbar

def combinegamma ( prm ):
    """combine gamma parameter estimates to give the product prior

    Parameters
    ----------
    prm : sequence
        a sequence of pairs of parameter estimates

    Returns:
    --------
    kbar,thetabar : double
        shape and scale parameter for the product prior gamma distribution
    """
    prm = array ( prm )
    kbar = 1 + sum (prm[:,0]-1)
    thbar = 1./sum(1./prm[:,1])
    return kbar,thbar

def combinebeta ( prm ):
    """combine gamma parameter estimates to give the product prior

    Parameters
    ----------
    prm : sequence
        a sequence of pairs of parameter estimates

    Returns:
    --------
    alphabar,betabar : double
        prior successes and prior misses parameters for the product prior beta distribution
    """
    prm = array ( prm )
    albar = 1 + sum ( prm[:,0]-1 )
    btbar = 1 + sum ( prm[:,1]-1 )
    return albar,btbar

def derive_informed_priors ( mcmcobjects, distribution="Gamma", parameter="w" ):
    """Combine mcmc samples to give informed priors for quasi-simultaneous inference

    For further information see the file 'simultaneous' in the documents folder.

    Parameters
    ----------
    mcmcobjects : sequence of BayesInference objects
        BayesInference objects containing samples obtained with flat priors that are used to determine
        the combined informed 'product prior'
    distribution : string
        name of the desired prior. Currently, only 'Gamma', 'Gauss', 'Beta' are supported.
    parameter : string
        name of the parameter to be constrained to be equal across conditions. Currently,
        'm', 'alpha', 'w', 'beta', 'lambda', and 'gamma' are supported

    Returns
    -------
    priors : sequence of strings
        list of priors to be used for performing the quasi simultaneous fit
    """
    # Check that the mcmc objects are comparable!!!
    params_assign = {'m': 0, 'alpha': 0, 'w': 1, 'beta': 1, 'lambda': 2, 'gamma': -1}
    if distribution=="Gamma":
        fit,combine = fitgamma,combinegamma
    elif distribution=="Gauss":
        fit,combine = fitgauss,combinegauss
    elif distribution=="Beta":
        fit,combine = fitbeta,combinebeta
    else:
        raise ArgumentError,"Invalid model to fit posterior distributions"

    fitted_parameters = []
    for mfit in mcmcobjects:
        prm,l = fit ( mfit.mcestimates[:,params_assign[parameter]] )
        fitted_parameters.append(prm)

    priors = []
    for j in xrange(len(fitted_parameters)):
        current = fitted_parameters.pop(0)
        prm = combine ( fitted_parameters )
        priors.append ( distribution+"(%g,%g)"%prm )
        fitted_parameters.append ( current )

    return priors