示例#1
0
文件: s82.py 项目: AstroVPK/kali
    def fit(self, pMin=1, pMax=1, qMin=-1, qMax=-1, nwalkers=200, nsteps=1000, xTol=0.001, maxEvals=10000):
        self.taskDict = dict()
        self.DICDict = dict()
        self.totalTime = 0.0
        if (qMax >= pMax):
            raise ValueError('pMax must be greater than qMax')
        if (qMax == -1):
            qMax = pMax - 1
        if (qMin == -1):
            qMin = 0
        if (pMin < 1):
            raise ValueError('pMin must be greater than or equal to 1')
        if (qMin < 0):
            raise ValueError('qMin must be greater than or equal to 0')
        self.pMax = pMax
        self.pMin = pMin
        self.qMax = qMax
        self.qMin = qMin

        for p in xrange(pMin, pMax + 1):
            for q in xrange(qMin, min(p, qMax + 1)):
                nt = kali.carma.CARMATask(
                    p, q, nwalkers=nwalkers, nsteps=nsteps, xTol=xTol, maxEvals=maxEvals)

                print 'Starting carma fitting for p = %d and q = %d...'%(p, q)
                startLCARMA = time.time()
                nt.fit(self)
                stopLCARMA = time.time()
                timeLCARMA = stopLCARMA - startLCARMA
                print 'carma took %4.3f s = %4.3f min = %4.3f hrs'%(timeLCARMA,
                                                                    timeLCARMA/60.0, timeLCARMA/3600.0)
                self.totalTime += timeLCARMA

                Deviances = copy.copy(nt.LnPosterior[:, nsteps/2:]).reshape((-1))
                DIC = 0.5*math.pow(np.nanstd(-2.0*Deviances), 2.0) + np.nanmean(-2.0*Deviances)
                print 'C-ARMA(%d,%d) DIC: %+4.3e'%(p, q, DIC)
                self.DICDict['%d %d'%(p, q)] = DIC
                self.taskDict['%d %d'%(p, q)] = nt
        print 'Total time taken by carma is %4.3f s = %4.3f min = %4.3f hrs'%(self.totalTime,
                                                                              self.totalTime/60.0,
                                                                              self.totalTime/3600.0)

        sortedDICVals = sorted(self.DICDict.items(), key=operator.itemgetter(1))
        self.pBest = int(sortedDICVals[0][0].split()[0])
        self.qBest = int(sortedDICVals[0][0].split()[1])
        print 'Best model is C-ARMA(%d,%d)'%(self.pBest, self.qBest)

        self.bestTask = self.taskDict['%d %d'%(self.pBest, self.qBest)]
        self.bestFigTitle = 'Best Model: CARMA(%d,%d); DIC: %+4.3e'%(
            self.pBest, self.qBest, self.DICDict['%d %d'%(self.pBest, self.qBest)])
        self.bestLabelList = list()
        for i in range(self.pBest):
            self.bestLabelList.append("$a_{%d}$"%(i + 1))
        for i in range(self.qBest + 1):
            self.bestLabelList.append("$b_{%d}$"%(i))
        mcmcviz.vizTriangle(self.pBest, self.qBest, self.bestTask.Chain,
                            labelList=self.bestLabelList, figTitle=self.bestFigTitle)
示例#2
0
    def view(self):
        notDone = True
        while notDone:
            whatToView = -1
            while whatToView < 0 or whatToView > 3:
                try:
                    whatToView = int(
                        raw_input(
                            'View walkers in C-ARMA coefficients (0) or C-ARMA roots (1) or C-ARMA \
                        timescales (2):'))
                except ValueError:
                    print 'Bad input: integer required!'
            pView = -1
            while pView < 1 or pView > self.pMax:
                try:
                    pView = int(raw_input('C-AR model order:'))
                except ValueError:
                    print 'Bad input: integer required!'
            qView = -1
            while qView < 0 or qView >= pView:
                try:
                    qView = int(raw_input('C-MA model order:'))
                except ValueError:
                    print 'Bad input: integer required!'

            if whatToView == 0:
                figTitle = 'CARMA(%d,%d); DIC: %+4.3e' % (
                    pView, qView, self.DICDict['%d %d' % (pView, qView)])
                labelList = list()
                dimCtr = 0
                for i in range(pView):
                    labelList.append(r'$a_{%d}$: $\mathrm{dim}%d$' %
                                     (i + 1, dimCtr))
                    dimCtr += 1
                for i in range(qView + 1):
                    labelList.append(r'$b_{%d}$: $\mathrm{dim}%d$' %
                                     (i, dimCtr))
                    dimCtr += 1
                mcmcviz.vizTriangle(pView,
                                    qView,
                                    self.taskDict['%d %d' %
                                                  (pView, qView)].Chain,
                                    labelList=labelList,
                                    figTitle=figTitle)
                dim1 = -1
                while dim1 < 0 or dim1 > pView + qView + 1:
                    try:
                        dim1 = int(raw_input('1st Dimension to view:'))
                    except ValueError:
                        print 'Bad input: integer required!'
                dim2 = -1
                while dim2 < 0 or dim2 > pView + qView + 1 or dim2 == dim1:
                    try:
                        dim2 = int(raw_input('2nd Dimension to view:'))
                    except ValueError:
                        print 'Bad input: integer required!'
                if dim1 < pView:
                    dim1Name = r'$a_{%d}$' % (dim1)
                if dim1 >= pView and dim1 < pView + qView + 1:
                    dim1Name = r'$b_{%d}$' % (dim1 - pView)
                if dim2 < pView:
                    dim2Name = r'$a_{%d}$' % (dim2)
                if dim2 >= pView and dim2 < pView + qView + 1:
                    dim2Name = r'$b_{%d}$' % (dim2 - pView)
                res = mcmcviz.vizWalkers(
                    self.taskDict['%d %d' % (pView, qView)].Chain,
                    self.taskDict['%d %d' % (pView, qView)].LnPosterior, dim1,
                    dim1Name, dim2, dim2Name)

            elif whatToView == 1:
                figTitle = 'CARMA(%d,%d); DIC: %+4.3e' % (
                    pView, qView, self.DICDict['%d %d' % (pView, qView)])
                labelList = list()
                dimCtr = 0
                for i in range(pView):
                    labelList.append(r'$r_{%d}$: $\mathrm{dim}%d$' %
                                     (i, dimCtr))
                    dimCtr += 1
                for i in range(qView):
                    labelList.append(r'$m_{%d}$: $\mathrm{dim}%d$' %
                                     (i, dimCtr))
                    dimCtr += 1
                labelList.append(r'$\mathrm{Amp.}$: $\mathrm{dim}%d$' %
                                 (dimCtr))
                dimCtr += 1
                mcmcviz.vizTriangle(pView,
                                    qView,
                                    self.taskDict['%d %d' %
                                                  (pView, qView)].rootChain,
                                    labelList=labelList,
                                    figTitle=figTitle)
                dim1 = -1
                while dim1 < 0 or dim1 > pView + qView + 1:
                    try:
                        dim1 = int(raw_input('1st Dimension to view:'))
                    except ValueError:
                        print 'Bad input: integer required!'
                dim2 = -1
                while dim2 < 0 or dim2 > pView + qView + 1 or dim2 == dim1:
                    try:
                        dim2 = int(raw_input('2nd Dimension to view:'))
                    except ValueError:
                        print 'Bad input: integer required!'
                if dim1 < pView:
                    dim1Name = r'$r_{%d}$' % (dim1)
                if dim1 >= pView and dim1 < pView + qView:
                    dim1Name = r'$m_{%d}$' % (dim1 - pView)
                if dim1 == pView + qView:
                    dim1Name = r'$\mathrm{Amp.}$'
                if dim2 < pView:
                    dim2Name = r'$r_{%d}$' % (dim2)
                if dim2 >= pView and dim2 < pView + qView:
                    dim2Name = r'$m_{%d}$' % (dim2 - pView)
                if dim2 == pView + qView:
                    dim2Name = r'$\mathrm{Amp.}$'
                res = mcmcviz.vizWalkers(
                    self.taskDict['%d %d' % (pView, qView)].rootChain,
                    self.taskDict['%d %d' % (pView, qView)].LnPosterior, dim1,
                    dim1Name, dim2, dim2Name)

            else:
                figTitle = 'CARMA(%d,%d); DIC: %+4.3e' % (
                    pView, qView, self.DICDict['%d %d' % (pView, qView)])
                labelList = list()
                dimCtr = 0
                for i in range(pView + qView):
                    labelList.append(r'$\tau_{%d}$: $\mathrm{dim}%d$' %
                                     (i, dimCtr))
                    dimCtr += 1
                labelList.append(r'$\mathrm{Amp.}$: $\mathrm{dim}%d$' %
                                 (dimCtr))
                dimCtr += 1
                mcmcviz.vizTriangle(
                    pView,
                    qView,
                    self.taskDict['%d %d' % (pView, qView)].timescaleChain,
                    labelList=labelList,
                    figTitle=figTitle)
                dim1 = -1
                while dim1 < 0 or dim1 > pView + qView + 1:
                    try:
                        dim1 = int(raw_input('1st Dimension to view:'))
                    except ValueError:
                        print 'Bad input: integer required!'
                dim2 = -1
                while dim2 < 0 or dim2 > pView + qView + 1 or dim2 == dim1:
                    try:
                        dim2 = int(raw_input('2nd Dimension to view:'))
                    except ValueError:
                        print 'Bad input: integer required!'
                if dim1 < pView + qView:
                    dim1Name = r'$\tau_{%d}$' % (dim1)
                if dim1 == pView + qView:
                    dim1Name = r'$\mathrm{Amp.}$'
                if dim2 < pView + qView:
                    dim2Name = r'$\tau_{%d}$' % (dim2)
                if dim2 == pView + qView:
                    dim2Name = r'$\mathrm{Amp.}$'
                res = mcmcviz.vizWalkers(
                    self.taskDict['%d %d' % (pView, qView)].timescaleChain,
                    self.taskDict['%d %d' % (pView, qView)].LnPosterior, dim1,
                    dim1Name, dim2, dim2Name)

            var = str(
                raw_input('Do you wish to view any more MCMC walkers? (y/n):')
            ).lower()
            if var == 'n':
                notDone = False
示例#3
0
    def fit(self,
            pMin=1,
            pMax=1,
            qMin=-1,
            qMax=-1,
            nwalkers=200,
            nsteps=1000,
            xTol=0.001,
            maxEvals=10000,
            doShow=True):
        self.taskDict = dict()
        self.DICDict = dict()
        self.totalTime = 0.0
        if (qMax >= pMax):
            raise ValueError('pMax must be greater than qMax')
        if (qMax == -1):
            qMax = pMax - 1
        if (qMin == -1):
            qMin = 0
        if (pMin < 1):
            raise ValueError('pMin must be greater than or equal to 1')
        if (qMin < 0):
            raise ValueError('qMin must be greater than or equal to 0')
        self.pMax = pMax
        self.pMin = pMin
        self.qMax = qMax
        self.qMin = qMin

        for p in xrange(pMin, pMax + 1):
            for q in xrange(qMin, min(p, qMax + 1)):
                nt = kali.carma.CARMATask(p,
                                          q,
                                          nwalkers=nwalkers,
                                          nsteps=nsteps,
                                          xTol=xTol,
                                          maxEvals=maxEvals)

                print 'Starting carma fitting for p = %d and q = %d...' % (p,
                                                                           q)
                startLCARMA = time.time()
                nt.fit(self)
                stopLCARMA = time.time()
                timeLCARMA = stopLCARMA - startLCARMA
                print 'carma took %4.3f s = %4.3f min = %4.3f hrs' % (
                    timeLCARMA, timeLCARMA / 60.0, timeLCARMA / 3600.0)
                self.totalTime += timeLCARMA

                Deviances = copy.copy(nt.LnPosterior[:, nsteps / 2:]).reshape(
                    (-1))
                DIC = 0.5 * math.pow(np.nanstd(-2.0 * Deviances),
                                     2.0) + np.nanmean(-2.0 * Deviances)
                print 'C-ARMA(%d,%d) DIC: %+4.3e' % (p, q, DIC)
                self.DICDict['%d %d' % (p, q)] = DIC
                self.taskDict['%d %d' % (p, q)] = nt
        print 'Total time taken by carma is %4.3f s = %4.3f min = %4.3f hrs' % (
            self.totalTime, self.totalTime / 60.0, self.totalTime / 3600.0)

        sortedDICVals = sorted(self.DICDict.items(),
                               key=operator.itemgetter(1))
        self.pBest = int(sortedDICVals[0][0].split()[0])
        self.qBest = int(sortedDICVals[0][0].split()[1])
        print 'Best model is C-ARMA(%d,%d)' % (self.pBest, self.qBest)

        self.bestTask = self.taskDict['%d %d' % (self.pBest, self.qBest)]
        self.bestFigTitle = 'Best Model: CARMA(%d,%d); DIC: %+4.3e' % (
            self.pBest, self.qBest, self.DICDict['%d %d' %
                                                 (self.pBest, self.qBest)])
        self.bestLabelList = list()
        for i in range(self.pBest):
            self.bestLabelList.append("$a_{%d}$" % (i + 1))
        for i in range(self.qBest + 1):
            self.bestLabelList.append("$b_{%d}$" % (i))
        if doShow:
            mcmcviz.vizTriangle(self.pBest,
                                self.qBest,
                                self.bestTask.Chain,
                                labelList=self.bestLabelList,
                                figTitle=self.bestFigTitle)
示例#4
0
labelT = []
labelRoots = []
for k in xrange(1, pBest + 1):
	labelList.append('a$_{%d}$'%(k))
	labelT.append('$\tao_{%d}$'%(k))
	labelRoots.append('r$_{%d}$'%(k))
for u in xrange(0, pBest):
	labelList.append('b$_{%d}$'%(u))	
	labelT.append('$\tao_{MA%d}$'%(u))
	labelRoots.append('r$_{MA%d}$'%(u))

figTitle = args.sdssid	
#plot_res =  mcmcviz.vizTriangle(p, q, nt.Chain, labelList, figTitle+'%d_%d'%(p, q))
res = mcmcviz.vizWalkers(taskDict['%d %d'%(pBest, qBest)].timescaleChain, taskDict['%d %d'%(pBest, qBest)].LnPosterior, 0, labelT[0], 1, labelT[1])
plt.clf()
res_coeffs = mcmcviz.vizTriangle(pBest, qBest, taskDict['%d %d'%(pBest, qBest)].Chain, labelList , str(figTitle+'BestChain'+'%d-%d'%(pBest, qBest)))
plt.clf()
res_times = mcmcviz.vizTriangle(pBest, qBest, taskDict['%d %d'%(pBest, qBest)].timescaleChain, labelT, str(figTitle+'BestTimescales'+'%d-%d'%(pBest, qBest)))

plt.clf()

#reset labels list
labelList2 = []
labelT2 = []
labelRoots2 = []
for k in xrange(1, pNext + 1):
	labelList2.append('a$_{%d}$'%(k))
	labelT2.append('$tao_{%d}$'%(k))
	labelRoots2.append('r$_{%d}$'%(k))
for u in xrange(0, pNext):
	labelList2.append('b$_{%d}$'%(u))	
示例#5
0
文件: s82.py 项目: AstroVPK/kali
    def view(self):
        notDone = True
        while notDone:
            whatToView = -1
            while whatToView < 0 or whatToView > 3:
                try:
                    whatToView = int(
                        raw_input('View walkers in C-ARMA coefficients (0) or C-ARMA roots (1) or C-ARMA \
                        timescales (2):'))
                except ValueError:
                    print 'Bad input: integer required!'
            pView = -1
            while pView < 1 or pView > self.pMax:
                try:
                    pView = int(raw_input('C-AR model order:'))
                except ValueError:
                    print 'Bad input: integer required!'
            qView = -1
            while qView < 0 or qView >= pView:
                try:
                    qView = int(raw_input('C-MA model order:'))
                except ValueError:
                    print 'Bad input: integer required!'

            if whatToView == 0:
                figTitle = 'CARMA(%d,%d); DIC: %+4.3e'%(pView, qView, self.DICDict['%d %d'%(pView, qView)])
                labelList = list()
                dimCtr = 0
                for i in range(pView):
                    labelList.append(r'$a_{%d}$: $\mathrm{dim}%d$'%(i + 1, dimCtr))
                    dimCtr += 1
                for i in range(qView + 1):
                    labelList.append(r'$b_{%d}$: $\mathrm{dim}%d$'%(i, dimCtr))
                    dimCtr += 1
                mcmcviz.vizTriangle(pView, qView, self.taskDict[
                                    '%d %d'%(pView, qView)].Chain, labelList=labelList, figTitle=figTitle)
                dim1 = -1
                while dim1 < 0 or dim1 > pView + qView + 1:
                    try:
                        dim1 = int(raw_input('1st Dimension to view:'))
                    except ValueError:
                        print 'Bad input: integer required!'
                dim2 = -1
                while dim2 < 0 or dim2 > pView + qView + 1 or dim2 == dim1:
                    try:
                        dim2 = int(raw_input('2nd Dimension to view:'))
                    except ValueError:
                        print 'Bad input: integer required!'
                if dim1 < pView:
                    dim1Name = r'$a_{%d}$'%(dim1)
                if dim1 >= pView and dim1 < pView + qView + 1:
                    dim1Name = r'$b_{%d}$'%(dim1 - pView)
                if dim2 < pView:
                    dim2Name = r'$a_{%d}$'%(dim2)
                if dim2 >= pView and dim2 < pView + qView + 1:
                    dim2Name = r'$b_{%d}$'%(dim2 - pView)
                res = mcmcviz.vizWalkers(self.taskDict['%d %d'%(pView, qView)].Chain, self.taskDict[
                                         '%d %d'%(pView, qView)].LnPosterior, dim1, dim1Name, dim2, dim2Name)

            elif whatToView == 1:
                figTitle = 'CARMA(%d,%d); DIC: %+4.3e'%(pView, qView, self.DICDict['%d %d'%(pView, qView)])
                labelList = list()
                dimCtr = 0
                for i in range(pView):
                    labelList.append(r'$r_{%d}$: $\mathrm{dim}%d$'%(i, dimCtr))
                    dimCtr += 1
                for i in range(qView):
                    labelList.append(r'$m_{%d}$: $\mathrm{dim}%d$'%(i, dimCtr))
                    dimCtr += 1
                labelList.append(r'$\mathrm{Amp.}$: $\mathrm{dim}%d$'%(dimCtr))
                dimCtr += 1
                mcmcviz.vizTriangle(pView, qView, self.taskDict[
                                    '%d %d'%(pView, qView)].rootChain, labelList=labelList, figTitle=figTitle)
                dim1 = -1
                while dim1 < 0 or dim1 > pView + qView + 1:
                    try:
                        dim1 = int(raw_input('1st Dimension to view:'))
                    except ValueError:
                        print 'Bad input: integer required!'
                dim2 = -1
                while dim2 < 0 or dim2 > pView + qView + 1 or dim2 == dim1:
                    try:
                        dim2 = int(raw_input('2nd Dimension to view:'))
                    except ValueError:
                        print 'Bad input: integer required!'
                if dim1 < pView:
                    dim1Name = r'$r_{%d}$'%(dim1)
                if dim1 >= pView and dim1 < pView + qView:
                    dim1Name = r'$m_{%d}$'%(dim1 - pView)
                if dim1 == pView + qView:
                    dim1Name = r'$\mathrm{Amp.}$'
                if dim2 < pView:
                    dim2Name = r'$r_{%d}$'%(dim2)
                if dim2 >= pView and dim2 < pView + qView:
                    dim2Name = r'$m_{%d}$'%(dim2 - pView)
                if dim2 == pView + qView:
                    dim2Name = r'$\mathrm{Amp.}$'
                res = mcmcviz.vizWalkers(self.taskDict['%d %d'%(pView, qView)].rootChain, self.taskDict[
                                         '%d %d'%(pView, qView)].LnPosterior, dim1, dim1Name, dim2, dim2Name)

            else:
                figTitle = 'CARMA(%d,%d); DIC: %+4.3e'%(pView, qView, self.DICDict['%d %d'%(pView, qView)])
                labelList = list()
                dimCtr = 0
                for i in range(pView + qView):
                    labelList.append(r'$\tau_{%d}$: $\mathrm{dim}%d$'%(i, dimCtr))
                    dimCtr += 1
                labelList.append(r'$\mathrm{Amp.}$: $\mathrm{dim}%d$'%(dimCtr))
                dimCtr += 1
                mcmcviz.vizTriangle(pView, qView, self.taskDict['%d %d'%(pView, qView)].timescaleChain,
                                    labelList=labelList, figTitle=figTitle)
                dim1 = -1
                while dim1 < 0 or dim1 > pView + qView + 1:
                    try:
                        dim1 = int(raw_input('1st Dimension to view:'))
                    except ValueError:
                        print 'Bad input: integer required!'
                dim2 = -1
                while dim2 < 0 or dim2 > pView + qView + 1 or dim2 == dim1:
                    try:
                        dim2 = int(raw_input('2nd Dimension to view:'))
                    except ValueError:
                        print 'Bad input: integer required!'
                if dim1 < pView + qView:
                    dim1Name = r'$\tau_{%d}$'%(dim1)
                if dim1 == pView + qView:
                    dim1Name = r'$\mathrm{Amp.}$'
                if dim2 < pView + qView:
                    dim2Name = r'$\tau_{%d}$'%(dim2)
                if dim2 == pView + qView:
                    dim2Name = r'$\mathrm{Amp.}$'
                res = mcmcviz.vizWalkers(self.taskDict['%d %d'%(pView, qView)].timescaleChain,
                                         self.taskDict['%d %d'%(pView, qView)].LnPosterior, dim1, dim1Name,
                                         dim2, dim2Name)

            var = str(raw_input('Do you wish to view any more MCMC walkers? (y/n):')).lower()
            if var == 'n':
                notDone = False