コード例 #1
0
    def fasterMCMC(self, niter, nburn, nthin=1, grid=False):
        from Sampler import SimpleSample as sample
        import pymc, numpy, ndinterp

        axn = [a for a in self.model.axes_names]
        axes = self.model.axes

        pmags = []
        lpModel = None
        magModel = None
        Modaxes = None
        magUC = 0.
        order = None
        for i in range(len(self.data)):
            f = self.data[i]['filter']
            z = self.data[i]['redshift']
            pmags.append(self.model.models[f][z].z)
            if magModel is None:
                magModel = self.model.models[f][z].z * 0.
                lpModel = magModel.copy()
                Modaxes = self.model.models[f][z].axes
                order = self.model.models[f][z].order
            m, me = self.data[i]['mag'], self.data[i]['error']**2
            d = (m - pmags[-1])
            lpModel += -0.5 * d**2 / me
            magModel += d / me
            magUC += -0.5 / me

        for i in range(len(self.data) - 1):
            d1 = self.data[i]
            d2 = self.data[i + 1]
            f1, m1, me1 = d1['filter'], d1['mag'], d1['error']
            f2, m2, me2 = d2['filter'], d2['mag'], d2['error']
            c = pmags[i] - pmags[i + 1]
            dc = m1 - m2
            if i == 0:
                logp = (c - dc)**2 / (me1**2 + me2**2)
            else:
                logp += (c - dc)**2 / (me1**2 + me2**2)

        indx = numpy.unravel_index(logp.argmin(), logp.shape)
        m = 0.
        w = 0.
        for i in range(len(self.data)):
            d, e = self.data[i]['mag'], self.data[i]['error']
            M = (pmags[i][indx] - d) / 2.5
            m += M / e**2
            w += 1. / e**2
        m /= w

        M = numpy.linspace(m - 0.6, m + 0.6, 13)
        a = []
        for i in range(len(self.model.axes_names)):
            a.append(
                self.model.axes[self.model.axes_names[i]]['points'].copy())
        for key in self.names:
            p = self.priors[key]['prior']
            if key.find('mass') >= 0:
                continue
            if key.find('log') == 0:
                key = key[3:]
                a[axn.index(key)] = numpy.log10(a[axn.index(key)])
            i = axn.index(key)
            for j in range(len(a[i])):
                p.value = a[i][j]
                try:
                    a[i][j] = p.logp
                except:
                    a[i][j] = -1e300
        logp = lpModel + ndinterp.create_axes_array(a).sum(0)

        logp = numpy.expand_dims(logp, logp.ndim).repeat(M.size, logp.ndim)
        for i in range(M.size):
            M0 = M[i] * -2.5
            logp[:, :, :, :, i] += magModel * M0 + M0**2 * magUC
        logp -= logp.max()

        wt = numpy.exp(logp)
        wt /= wt.sum()
        a = []
        for i in range(len(self.model.axes_names)):
            a.append(self.model.axes[self.model.axes_names[i]]['points'])
        a.append(M)
        for key in self.names:
            if key.find('mass') >= 0:
                if key.find('log') != 0:
                    a[-1] == 10**a[-1]
                axn.append('mass')
            elif key.find('log') == 0:
                key = key[3:]
                a[axn.index(key)] = numpy.log10(a[axn.index(key)])
        vals = ndinterp.create_axes_array(a)
        if grid == True:
            m = (wt * vals[-1]).sum()
            st = ((wt * (vals[-1] - m)**2).sum())**0.5
            return m, st

        pars = []
        cov = []

        for key in self.names:
            pars.append(self.priors[key]['prior'])
            if key.find('log') == 0:
                key = key[3:]
            i = axn.index(key)
            m = (wt * vals[i]).sum()
            st = ((wt * (vals[i] - m)**2).sum())**0.5
            pars[-1].value = m
            cov.append(st)

        for key in self.names:
            continue
            pars.append(self.priors[key]['prior'])
            if key == 'logmass':
                pars[-1].value = m
                cov.append(0.03)
            elif key == 'mass':
                pars[-1].value = 10**m
                cov.append(self.priors[key]['prior'].value / 20.)
            else:
                if key.find('log') != 0:
                    i = axn.index(key)
                    mn = (vals[i] * wt).sum()
                    cov.append(((wt * (vals[i] - mn)**2).sum())**0.5)
                    pars[-1].value = mn
                else:
                    key = key.split('log')[1]
                    i = axn.index(key)
                    mn = (numpy.log10(vals[i]) * wt).sum()
                    cov.append(
                        ((wt * (numpy.log10(vals[i]) - mn)**2).sum())**0.5)
                    pars[-1].value = mn
        for key in self.names:
            continue
            pars.append(self.priors[key]['prior'])
            if key == 'logmass':
                pars[-1].value = m
                cov.append(0.03)
            elif key == 'mass':
                pars[-1].value = 10**m
                cov.append(self.priors[key]['prior'].value / 20.)
            elif key == 'age':
                pars[-1].value = axes['age']['points'][indx[axn.index('age')]]
                cov.append(0.5)
            elif key == 'logage':
                pars[-1].value = numpy.log10(
                    axes['age']['points'][indx[axn.index('age')]])
                cov.append(0.03)
            elif key == 'tau':
                pars[-1].value = axes['tau']['points'][indx[axn.index('tau')]]
                cov.append(0.2)
            elif key == 'logtau':
                pars[-1].value = numpy.log10(
                    axes['tau']['points'][indx[axn.index('tau')]])
                cov.append(0.03)
            elif key == 'tau_V':
                pars[-1].value = axes['tau_V']['points'][indx[axn.index(
                    'tau_V')]]
                cov.append(self.priors[key]['prior'].value / 20.)
            elif key == 'logtau_V':
                pars[-1].value = numpy.log10(
                    axes['tau_V']['points'][indx[axn.index('tau_V')]])
                cov.append(0.1)
            elif key == 'Z':
                pars[-1].value = axes['Z']['points'][indx[axn.index('Z')]]
                cov.append(self.priors[key]['prior'].value / 20.)
            elif key == 'logZ':
                pars[-1].value = numpy.log10(
                    axes['Z']['points'][indx[axn.index('Z')]])
                cov.append(0.05)

        lpModel = ndinterp.ndInterp(Modaxes, lpModel, order)
        magModel = ndinterp.ndInterp(Modaxes, magModel, order)

        cov = numpy.array(cov)

        @pymc.observed
        def loglikelihood(value=0., pars=pars):
            from math import log10
            points = numpy.zeros((1, len(pars) - 1))
            i = 0
            for key in self.names:
                if key == 'mass':
                    M = -2.5 * log10(pars[i])
                elif key == 'logmass':
                    M = -2.5 * pars[i]
                elif key.find('log') == 0:
                    key = key.split('log')[1]
                    points[0, axn.index(key)] = 10**pars[i]
                else:
                    points[0, axn.index(key)] = pars[i]
                i += 1
            lp = lpModel.eval(points)
            if lp == 0:
                return -1e300  # Short circuit if out of range
            lp += magModel.eval(points) * M + M**2 * magUC
            return lp

        costs = [loglikelihood]
        logps, trace, dets = sample(pars,
                                    costs, [],
                                    nburn / 2,
                                    cov=cov,
                                    jump=[0., 0.])
        #        cov = numpy.cov(trace.T)
        logps, trace, dets = sample(pars,
                                    costs, [],
                                    nburn / 2,
                                    cov=cov,
                                    jump=[0., 0.])
        cov = numpy.cov(trace.T)
        logps, trace, dets = sample(pars,
                                    costs, [],
                                    niter,
                                    cov=cov,
                                    jump=[0., 0.])
        self.proposal_cov = cov
        self.trace = trace
        self.logp = logps
        cnt = 0
        o = {'logp': logps}
        for key in self.names:
            o[key] = trace[:, cnt].copy()
            cnt += 1
        return o
コード例 #2
0
ファイル: makeSersic.py プロジェクト: lindzlebean/pylathon
from pylens import MassModels
import numpy,pylab
import indexTricks as iT
from scipy import interpolate
from scipy.special import gamma,gammainc
import ndinterp,time
import sersic
import cPickle

x0 = numpy.logspace(-4.,1.,51)
n0 = numpy.linspace(0.5,6.,12)
q0 = numpy.linspace(0.1,1.,19)

x,y,n,q = ndinterp.create_axes_array([x0,x0,n0,q0])
yout = x*0.
xout = y*0.
for i in range(x.shape[2]):
    for j in range(x.shape[3]):
        X = x[:,:,i,j]
        Y = y[:,:,i,j]
        N = n[0,0,i,j]
        Q = q[0,0,i,j]
        k = 2.*N-1./3+4./(405.*N)+46/(25515.*N**2)
        amp = k**(2*N)/(2*N*gamma(2*N))
        yi,xi = sersic.sersicdeflections(-Y.ravel(),X.ravel(),amp,1.,N,Q)
        yout[:,:,i,j] = -1*yi.reshape(Y.shape)
        xout[:,:,i,j] = xi.reshape(X.shape)

axes = {}
axes[0] = interpolate.splrep(x0,numpy.arange(x0.size))
axes[1] = interpolate.splrep(x0,numpy.arange(x0.size))
コード例 #3
0
ファイル: origMassEstimator.py プロジェクト: bnord/LensPop
    def fasterMCMC(self,niter,nburn,nthin=1,grid=False):
        from Sampler import SimpleSample as sample
        import pymc,numpy,ndinterp

        axn = [a for a in self.model.axes_names]
        axes = self.model.axes

        pmags = []
        lpModel = None
        magModel = None
        Modaxes = None
        magUC = 0.
        order = None
        for i in range(len(self.data)):
            f = self.data[i]['filter']
            z = self.data[i]['redshift']
            pmags.append(self.model.models[f][z].z)
            if magModel is None:
                magModel = self.model.models[f][z].z*0.
                lpModel = magModel.copy()
                Modaxes = self.model.models[f][z].axes
                order = self.model.models[f][z].order
            m,me = self.data[i]['mag'],self.data[i]['error']**2
            d = (m-pmags[-1])
            lpModel += -0.5*d**2/me
            magModel += d/me
            magUC += -0.5/me

        for i in range(len(self.data)-1):
            d1 = self.data[i]
            d2 = self.data[i+1]
            f1,m1,me1 = d1['filter'],d1['mag'],d1['error']
            f2,m2,me2 = d2['filter'],d2['mag'],d2['error']
            c = pmags[i]-pmags[i+1]
            dc = m1-m2
            if i==0:
                logp = (c-dc)**2/(me1**2+me2**2)
            else:
                logp += (c-dc)**2/(me1**2+me2**2)

        indx = numpy.unravel_index(logp.argmin(),logp.shape)
        m = 0.
        w = 0.
        for i in range(len(self.data)):
            d,e = self.data[i]['mag'],self.data[i]['error']
            M = (pmags[i][indx]-d)/2.5
            m += M/e**2
            w += 1./e**2
        m /= w

        M = numpy.linspace(m-0.6,m+0.6,13)
        a = []
        for i in range(len(self.model.axes_names)):
            a.append(self.model.axes[self.model.axes_names[i]]['points'].copy())
        for key in self.names:
            p = self.priors[key]['prior']
            if key.find('mass')>=0:
                continue
            if key.find('log')==0:
                key = key[3:]
                a[axn.index(key)] = numpy.log10(a[axn.index(key)])
            i = axn.index(key)
            for j in range(len(a[i])):
                p.value = a[i][j]
                try:
                    a[i][j] = p.logp
                except:
                    a[i][j] = -1e300
        logp = lpModel+ndinterp.create_axes_array(a).sum(0)

        logp = numpy.expand_dims(logp,logp.ndim).repeat(M.size,logp.ndim)
        for i in range(M.size):
            M0 = M[i]*-2.5
            logp[:,:,:,:,i] += magModel*M0 +  M0**2*magUC
        logp -= logp.max()

        wt = numpy.exp(logp)
        wt /= wt.sum()
        a = []
        for i in range(len(self.model.axes_names)):
            a.append(self.model.axes[self.model.axes_names[i]]['points'])
        a.append(M)
        for key in self.names:
            if key.find('mass')>=0:
                if key.find('log')!=0:
                    a[-1] == 10**a[-1]
                axn.append('mass')
            elif key.find('log')==0:
                key = key[3:]
                a[axn.index(key)] = numpy.log10(a[axn.index(key)])
        vals = ndinterp.create_axes_array(a)
        if grid==True:
            m = (wt*vals[-1]).sum()
            st = ((wt*(vals[-1]-m)**2).sum())**0.5
            return m,st

        pars = []
        cov = []

        for key in self.names:
            pars.append(self.priors[key]['prior'])
            if key.find('log')==0:
                key = key[3:]
            i = axn.index(key)
            m = (wt*vals[i]).sum()
            st = ((wt*(vals[i]-m)**2).sum())**0.5
            pars[-1].value = m
            cov.append(st)

        for key in self.names:
            continue
            pars.append(self.priors[key]['prior'])
            if key=='logmass':
                pars[-1].value = m
                cov.append(0.03)
            elif key=='mass':
                pars[-1].value = 10**m
                cov.append(self.priors[key]['prior'].value/20.)
            else:
                if key.find('log')!=0:
                    i = axn.index(key)
                    mn = (vals[i]*wt).sum()
                    cov.append(((wt*(vals[i]-mn)**2).sum())**0.5)
                    pars[-1].value = mn
                else:
                    key = key.split('log')[1]
                    i = axn.index(key)
                    mn = (numpy.log10(vals[i])*wt).sum()
                    cov.append(((wt*(numpy.log10(vals[i])-mn)**2).sum())**0.5)
                    pars[-1].value = mn
        for key in self.names:
            continue
            pars.append(self.priors[key]['prior'])
            if key=='logmass':
                pars[-1].value = m
                cov.append(0.03)
            elif key=='mass':
                pars[-1].value = 10**m
                cov.append(self.priors[key]['prior'].value/20.)
            elif key=='age':
                pars[-1].value = axes['age']['points'][indx[axn.index('age')]]
                cov.append(0.5)
            elif key=='logage':
                pars[-1].value = numpy.log10(axes['age']['points'][indx[axn.index('age')]])
                cov.append(0.03)
            elif key=='tau':
                pars[-1].value = axes['tau']['points'][indx[axn.index('tau')]]
                cov.append(0.2)
            elif key=='logtau':
                pars[-1].value = numpy.log10(axes['tau']['points'][indx[axn.index('tau')]])
                cov.append(0.03)
            elif key=='tau_V':
                pars[-1].value = axes['tau_V']['points'][indx[axn.index('tau_V')]]
                cov.append(self.priors[key]['prior'].value/20.)
            elif key=='logtau_V':
                pars[-1].value = numpy.log10(axes['tau_V']['points'][indx[axn.index('tau_V')]])
                cov.append(0.1)
            elif key=='Z':
                pars[-1].value = axes['Z']['points'][indx[axn.index('Z')]]
                cov.append(self.priors[key]['prior'].value/20.)
            elif key=='logZ':
                pars[-1].value = numpy.log10(axes['Z']['points'][indx[axn.index('Z')]])
                cov.append(0.05)

        lpModel = ndinterp.ndInterp(Modaxes,lpModel,order)
        magModel = ndinterp.ndInterp(Modaxes,magModel,order)

        cov = numpy.array(cov)
        @pymc.observed
        def loglikelihood(value=0.,pars=pars):
            from math import log10
            points = numpy.zeros((1,len(pars)-1))
            i = 0
            for key in self.names:
                if key=='mass':
                    M = -2.5*log10(pars[i])
                elif key=='logmass':
                    M = -2.5*pars[i]
                elif key.find('log')==0:
                    key = key.split('log')[1]
                    points[0,axn.index(key)] = 10**pars[i]
                else:
                    points[0,axn.index(key)] = pars[i]
                i += 1
            lp = lpModel.eval(points)
            if lp==0:
                return -1e300  # Short circuit if out of range
            lp += magModel.eval(points)*M + M**2*magUC
            return lp


        costs = [loglikelihood]
        logps,trace,dets = sample(pars,costs,[],nburn/2,cov=cov,jump=[0.,0.])
#        cov = numpy.cov(trace.T)
        logps,trace,dets = sample(pars,costs,[],nburn/2,cov=cov,jump=[0.,0.])
        cov = numpy.cov(trace.T)
        logps,trace,dets = sample(pars,costs,[],niter,cov=cov,jump=[0.,0.])
        self.proposal_cov = cov
        self.trace= trace
        self.logp = logps
        cnt = 0
        o = {'logp':logps}
        for key in self.names:
            o[key] = trace[:,cnt].copy()
            cnt += 1
        return o
コード例 #4
0
ファイル: test.py プロジェクト: lindzlebean/pylathon
import pylab,numpy
import indexTricks as iT
from scipy import ndimage
import ndinterp
from scipy.special import gamma,gammainc
import sersic
from pylens import powerlaw

x0 = numpy.logspace(-4.,1.,51)
#x0 = numpy.linspace(0.1,15.,151)
x,y = ndinterp.create_axes_array([x0,x0])

N = 3.7
Q = 0.73

k = 2.*N-1./3+4./(405.*N)+46/(25515.*N**2)
amp = k**(2*N)/(2*N*gamma(2*N))
print amp,x.size
amp = 1.
import time
t = time.time()
yi,xi = sersic.sersicdeflections(-y.ravel(),x.ravel(),amp,1.,N,Q)
yi *= -1
print time.time()-t
t = time.time()
yy,xx = powerlaw.powerlawdeflections(x.ravel(),y.ravel(),2.4,1.1,1e-5,0.73)
print time.time()-t
xr = x.ravel()
yr = y.ravel()
t = time.time()
gam = 1.1/2.
コード例 #5
0
from pylens import MassModels
import numpy, pylab
import indexTricks as iT
from scipy import interpolate
from scipy.special import gamma, gammainc
import ndinterp, time
import sersic
import cPickle

x0 = numpy.logspace(-4., 1., 51)
n0 = numpy.linspace(0.5, 6., 12)
q0 = numpy.linspace(0.1, 1., 19)

x, y, n, q = ndinterp.create_axes_array([x0, x0, n0, q0])
yout = x * 0.
xout = y * 0.
for i in range(x.shape[2]):
    for j in range(x.shape[3]):
        X = x[:, :, i, j]
        Y = y[:, :, i, j]
        N = n[0, 0, i, j]
        Q = q[0, 0, i, j]
        k = 2. * N - 1. / 3 + 4. / (405. * N) + 46 / (25515. * N**2)
        amp = k**(2 * N) / (2 * N * gamma(2 * N))
        yi, xi = sersic.sersicdeflections(-Y.ravel(), X.ravel(), amp, 1., N, Q)
        yout[:, :, i, j] = -1 * yi.reshape(Y.shape)
        xout[:, :, i, j] = xi.reshape(X.shape)

axes = {}
axes[0] = interpolate.splrep(x0, numpy.arange(x0.size))
axes[1] = interpolate.splrep(x0, numpy.arange(x0.size))