def plot2D(result,par1,par2, colorMap = getColorMap(), labelSize = 15, fontSize = 10, axisHandle = None): """ This function constructs a 2 dimensional marginal plot of the posterior density. This is the same plot as it is displayed in plotBayes in an unmodifyable way. The result struct is passed as result. par1 and par2 should code the two parameters to plot: 0 = threshold 1 = width 2 = lambda 3 = gamma 4 = eta Further plotting options may be passed. """ from utils import strToDim # convert strings to dimension number par1,label1 = strToDim(str(par1)) par2,label2 = strToDim(str(par2)) assert (par1 != par2), 'par1 and par2 must be different numbers to code for the parameters to plot' if axisHandle == None: axisHandle = plt.gca() try: plt.axes(axisHandle) except TypeError: raise ValueError('Invalid axes handle provided to plot in.') plt.set_cmap(colorMap) marg, _, _ = marginalize(result, np.array([par1, par2])) if par1 > par2 : marg = marg.T if 1 in marg.shape: if len(result['X1D'][par1])==1: plotMarginal(result,par2) else: plotMarginal(result,par2) else: e = [result['X1D'][par2][0], result['X1D'][par2][-1], \ result['X1D'][par1][0], result['X1D'][par1][-1]] plt.imshow(marg, extent = e) plt.ylabel(label1,fontsize = labelSize) plt.xlabel(label2,fontsize = labelSize) plt.tick_params(direction='out',right='off',top='off') for side in ['top','right']: axisHandle.spines[side].set_visible(False) plt.ticklabel_format(style='sci',scilimits=(-2,4)) plt.show()
def moveBorders(data,options): """ move parameter-boundaries to save computing power function borders=moveBorders(data, options) this function evaluates the likelihood on a much sparser, equally spaced grid definded by mbStepN and moves the borders in so that that marginals below tol are taken away from the borders. this is meant to save computing power by not evaluating the likelihood in areas where it is practically 0 everywhere. """ borders = [] tol = options['maxBorderValue'] d = options['borders'].shape[0] MBresult = {'X1D':[]} ''' move borders out should our borders be to tight, e.g. the distribution does not go to zero at the borders we move them out until this is the case. TODO it was disabled in MATLAB version. What to do with it? ''' ''' move borders inwards ''' for idx in range(0,d): if (len(options['mbStepN']) >= idx and options['mbStepN'][idx] >= 2 and options['borders'][idx,0] != options['borders'][idx,1]) : MBresult['X1D'].append(np.linspace(options['borders'][idx,0], options['borders'][idx,1], options['mbStepN'][idx])) else: if (options['borders'][idx,0] != options['borders'][idx,1] and options['expType'] != 'equalAsymptote'): warnings.warn('MoveBorders: You set only one evaluation for moving the borders!') MBresult['X1D'].append( np.array([0.5*np.sum(options['borders'][idx])])) MBresult['weight'] = getWeights(MBresult['X1D']) #kwargs = {'alpha': None, 'beta':None , 'lambda': None,'gamma':None , 'varscale':None } #fill_kwargs(kwargs,MBresult['X1D']) MBresult['Posterior'] = likelihood(data, options, MBresult['X1D'])[0] integral = sum(np.reshape(MBresult['Posterior'], -1) * np.reshape(MBresult['weight'], -1)) MBresult['Posterior'] /= integral borders = np.zeros([d,2]) for idx in range(0,d): (L1D,x,w) = marginalize(MBresult, np.array([idx])) x1 = x[np.max([np.where(L1D*w >= tol)[0][0] - 1, 0])] x2 = x[np.min([np.where(L1D*w >= tol)[0][-1]+1, len(x)-1])] borders[idx,:] = [x1,x2] return borders
def moveBorders(data,options): """ move parameter-boundaries to save computing power function borders=moveBorders(data, options) this function evaluates the likelihood on a much sparser, equally spaced grid definded by mbStepN and moves the borders in so that that marginals below tol are taken away from the borders. this is meant to save computing power by not evaluating the likelihood in areas where it is practically 0 everywhere. """ borders = [] tol = options.maxBorderValue d = options.borders.shape[0] MBresult = lambda: 0 ''' move borders out should our borders be to tight, e.g. the distribution does not go to zero at the borders we move them out until this is the case. TODO it was disabled in MATLAB version. What to do with it? ''' ''' move borders inwards ''' for idx in range(0,d): if (len(options.mbStepN) >= idx and options.mbStepN[idx] >= 2 and options.borders[idx,0] != options.borders[idx,1]) : MBresult.X1D[idx] = np.linspace(options.borders[idx,0], options.borders[idx,1], options.mbStepN[idx]) else: if (options.borders[idx,0] != options.borders[idx,1] and options.expType != 'equalAsymptote'): warnings.warn('MoveBorders: You set only one evaluation for moving the borders!') MBresult.X1D[idx] = 0.5*np.sum(options.borders[idx]) MBresult.weight = getWeights(MBresult.X1D) MBresult.Posterior = likelihood(data, options, MBresult.X1D) # TODO check! integral = sum(MBresult.Posterior[:] * MBresult.weight[:]) MBresult.Posterior /= integral borders = np.zeros([d,2]) for idx in range(0,d): (L1D,x,w) = marginalize(MBresult, idx) x1 = x[np.max(np.where(L1D*w >= tol)[0] - 1, 1)] x2 = x[np.min(np.where(L1D*w >= tol)[-1]+1, len(x))] borders[idx,:] = [x1,x2] return borders
def plotBayes(result, cmap = getColorMap()): plt.clf() plt.rc('text', usetex=True) plt.set_cmap(cmap) if result['options']['expType'] == 'equalAsymptote': result['X1D'][3] = 0 for ix in range(0,4): for jx in range(ix+1,5): plt.subplot(4,4,4*ix+jx) #marginalize marg, _, _ = marginalize(result,np.array([ix,jx])) e = [result['X1D'][jx][0], result['X1D'][jx][-1], \ result['X1D'][ix][0], result['X1D'][ix][-1] ] if marg.ndim == 1: marg = np.reshape(marg, [-1, 1]) if len(result['X1D'][ix]) != 1: plt.imshow(marg, extent = e) else: plt.imshow(marg.transpose(), extent = e) else: plt.imshow(marg, extent = e) # axis labels if ix == 0: plt.ylabel('threshold') elif ix == 1: plt.ylabel('width') elif ix == 2: plt.ylabel(r'$\lambda$') elif ix == 3: plt.ylabel(r'$\gamma$') if jx == 0: plt.xlabel('threshold') elif jx == 1: plt.xlabel('width') elif jx == 2: plt.xlabel(r'$\lambda$') elif jx == 3: plt.xlabel(r'$\gamma$') elif jx == 4: plt.xlabel(r'$\eta') plt.show()
def moveBorders(data,options): """ move parameter-boundaries to save computing power function borders=moveBorders(data, options) this function evaluates the likelihood on a much sparser, equally spaced grid definded by mbStepN and moves the borders in so that that marginals below tol are taken away from the borders. this is meant to save computing power by not evaluating the likelihood in areas where it is practically 0 everywhere. """ borders = [] tol = options['maxBorderValue'] d = options['borders'].shape[0] MBresult = {'X1D':[]} ''' move borders inwards ''' for idx in range(0,d): if (len(options['mbStepN']) >= idx and options['mbStepN'][idx] >= 2 and options['borders'][idx,0] != options['borders'][idx,1]) : MBresult['X1D'].append(np.linspace(options['borders'][idx,0], options['borders'][idx,1], options['mbStepN'][idx])) else: if (options['borders'][idx,0] != options['borders'][idx,1] and options['expType'] != 'equalAsymptote'): warnings.warn('MoveBorders: You set only one evaluation for moving the borders!') MBresult['X1D'].append( np.array([0.5*np.sum(options['borders'][idx])])) MBresult['weight'] = getWeights(MBresult['X1D']) #kwargs = {'alpha': None, 'beta':None , 'lambda': None,'gamma':None , 'varscale':None } #fill_kwargs(kwargs,MBresult['X1D']) MBresult['Posterior'] = likelihood(data, options, MBresult['X1D'])[0] integral = sum(np.reshape(MBresult['Posterior'], -1) * np.reshape(MBresult['weight'], -1)) MBresult['Posterior'] /= integral borders = np.zeros([d,2]) for idx in range(0,d): (L1D,x,w) = marginalize(MBresult, np.array([idx])) x1 = x[np.max([np.where(L1D*w >= tol)[0][0] - 1, 0])] x2 = x[np.min([np.where(L1D*w >= tol)[0][-1]+1, len(x)-1])] borders[idx,:] = [x1,x2] return borders
def psignifitCore(data, options): """ This is the Core processing of psignifit, call the frontend psignifit! function result=psignifitCore(data,options) Data nx3 matrix with values [x, percCorrect, NTrials] sigmoid should be a handle to a function, which accepts X,parameters as inputs and gives back a value in [0,1]. ideally parameters(1) should correspond to the threshold and parameters(2) to the width (distance containing 95% of the function. """ d = len(options['borders']) result = {'X1D': [], 'marginals': [], 'marginalsX': [], 'marginalsW': []} '''Choose grid dynamically from data''' if options['dynamicGrid']: # get seed from linear regression with logit transform Seed = getSeed(data,options) # further optimize the logliklihood to obtain a good estimate of the MAP if options['expType'] == 'YesNo': calcSeed = lambda X: -l.logLikelihood(data, options, X[0], X[1], X[2], X[3], X[4]) Seed = scipy.optimize.fmin(func=calcSeed, x0 = Seed) elif options['expType'] == 'nAFC': calcSeed = lambda X: -l.logLikelihood(data, options, X[0], X[1], X[2], 1/options['expN'], X[3]) Seed = scipy.optimize.fmin(func=calcSeed, x0 = [Seed[0:2], Seed[4]]) Seed = [Seed[0:2], 1/options['expN'], Seed[3]] #ToDo check whether row or colum vector result['X1D'] = gridSetting(data,options, Seed) else: # for types which do not need a MAP estimate if (options['gridSetType'] == 'priorlike' or options['gridSetType'] == 'STD' or options['gridSetType'] == 'exp' or options['gridSetType'] == '4power'): result['X1D'] = gridSetting(data,options) else: # Use a linear grid for idx in range(0,d): # If there is an actual Interval if options['borders'][idx, 0] < options['borders'][idx,1]: result['X1D'].append(np.linspace(options['borders'][idx,0], options['borders'][idx,1], num=options['stepN'][idx])) # if parameter was fixed else: result['X1D'].append(np.array([options['borders'][idx,0]])) '''Evaluate likelihood and form it into a posterior''' (result['Posterior'], result['logPmax']) = l.likelihood(data, options, result['X1D']) result['weight'] = getWeights(result['X1D']) integral = np.sum(np.array(result['Posterior'][:])*np.array(result['weight'][:])) result['Posterior'] = result['Posterior']/integral result['integral'] = integral '''Compute marginal distributions''' for idx in range(0,d): m, mX, mW = marginalize(result, np.array([idx])) result['marginals'].append(m) result['marginalsX'].append(mX) result['marginalsW'].append(mW) result['marginals'] = np.squeeze(result['marginals']) result['marginalsX'] = np.squeeze(result['marginalsX']) result['marginalsW'] = np.squeeze(result['marginalsW']) '''Find point estimate''' if (options['estimateType'] in ['MAP','MLE']): # get MLE estimate #start at most likely grid point index = np.where(result['Posterior'] == np.max(result['Posterior'].ravel())) Fit = np.zeros([d,1]) for idx in range(0,d): Fit[idx] = result['X1D'][idx][index[idx]] if options['expType'] == 'YesNo': fun = lambda X, f: -l.logLikelihood(data, options, [X[0],X[1],X[2],X[3],X[4]]) x0 = deepcopy(Fit) a = None elif options['expType'] == 'nAFC': #def func(X,f): # return -l.logLikelihood(data,options, [X[0], X[1], X[2], f, X[3]]) #fun = func fun = lambda X, f: -l.logLikelihood(data,options, [X[0], X[1], X[2], f, X[3]]) x0 = deepcopy(Fit[0:3]) # Fit[3] is excluded x0 = np.append(x0,deepcopy(Fit[4])) a = np.array([1/options['expN']]) elif options['expType'] == 'equalAsymptote': fun = lambda X, f: -l.logLikelihood(data,options,[X[0], X[1], X[2], f, X[3]]) x0 = deepcopy(Fit[0:3]) x0 = np.append(x0,deepcopy(Fit[4])) a = np.array([np.nan]) else: raise ValueError('unknown expType') if options['fastOptim']: Fit = scipy.optimize.fmin(fun, x0, args = (a,), xtol=0, ftol = 0, maxiter = 100, maxfun=100) warnings.warn('changed options for optimization') else: Fit = scipy.optimize.fmin(fun, x0, args = (a,), disp = True) if options['expType'] == 'YesNo': result['Fit'] = deepcopy(Fit) elif options['expType'] == 'nAFC': fit = deepcopy(Fit[0:3]) fit = np.append(fit, np.array([1/options['expN']])) fit = np.append(fit, deepcopy(Fit[3])) result['Fit'] = fit elif options['expType'] =='equalAsymptote': fit = deepcopy(Fit[0:3]) fit = np.append(fit, Fit[2]) fit = np.append(fit, Fit[3]) result['Fit'] = fit else: raise ValueError('unknown expType') par_idx = np.where(np.isnan(options['fixedPars']) == False) for idx in par_idx: result['Fit'][idx] = options['fixedPars'][idx] elif options['estimateType'] == 'mean': # get mean estimate Fit = np.zeros([d,1]) for idx in range[0:d]: Fit[idx] = np.sum(result['marginals'][idx]*result['marginalsW'][idx]*result['marginalsX'][idx]) result['Fit'] = deepcopy(Fit) Fit = np.empty(Fit.shape) '''Include input into result''' result['options'] = options # no copies here, because they are not changing result['data'] = data '''Compute confidence intervals''' if ~options['fastOptim']: result['conf_Intervals'] = getConfRegion(result) return result
import sys from marginalize import marginalize import models MAX_PHRASE_OPTIONS = 1000000 tm = models.TM('data/tm', MAX_PHRASE_OPTIONS) lm = models.LM('data/lm') spanishSentences = [tuple(line.strip().split()) for line in open('data/input', 'r')] bestSentScores = [-9999 for i in range(0,55)] bestSent = ['' for i in range(0, 55)] for fileName in sys.argv[1:]: for sentNum, enSent in enumerate(open(fileName, 'r')): score = marginalize(spanishSentences[sentNum], enSent, lm, tm) if score > bestSentScores[sentNum]: bestSentScores[sentNum] = score bestSent[sentNum] = enSent for sent in bestSent: print sent.strip()
def psignifitCore(data, options): """ This is the Core processing of psignifit, call the frontend psignifit! function result=psignifitCore(data,options) Data nx3 matrix with values [x, percCorrect, NTrials] sigmoid should be a handle to a function, which accepts X,parameters as inputs and gives back a value in [0,1]. ideally parameters(1) should correspond to the threshold and parameters(2) to the width (distance containing 95% of the function. """ d = len(options['borders']) result = {'X1D': [], 'marginals': [], 'marginalsX': [], 'marginalsW': []} '''Choose grid dynamically from data''' if options['dynamicGrid']: # get seed from linear regression with logit transform Seed = getSeed(data,options) # further optimize the logliklihood to obtain a good estimate of the MAP if options['expType'] == 'YesNo': calcSeed = lambda X: -_l.logLikelihood(data, options, X[0], X[1], X[2], X[3], X[4]) Seed = scipy.optimize.fmin(func=calcSeed, x0 = Seed) elif options['expType'] == 'nAFC': calcSeed = lambda X: -_l.logLikelihood(data, options, X[0], X[1], X[2], 1/options['expN'], X[3]) Seed = scipy.optimize.fmin(func=calcSeed, x0 = [Seed[0:2], Seed[4]]) Seed = [Seed[0:2], 1/options['expN'], Seed[3]] #ToDo check whether row or colum vector result['X1D'] = gridSetting(data,options, Seed) else: # for types which do not need a MAP estimate if (options['gridSetType'] == 'priorlike' or options['gridSetType'] == 'STD' or options['gridSetType'] == 'exp' or options['gridSetType'] == '4power'): result['X1D'] = gridSetting(data,options) else: # Use a linear grid for idx in range(0,d): # If there is an actual Interval if options['borders'][idx, 0] < options['borders'][idx,1]: result['X1D'].append(np.linspace(options['borders'][idx,0], options['borders'][idx,1], num=options['stepN'][idx])) # if parameter was fixed else: result['X1D'].append(np.array([options['borders'][idx,0]])) '''Evaluate likelihood and form it into a posterior''' (result['Posterior'], result['logPmax']) = _l.likelihood(data, options, result['X1D']) result['weight'] = getWeights(result['X1D']) integral = np.sum(np.array(result['Posterior'][:])*np.array(result['weight'][:])) result['Posterior'] = result['Posterior']/integral result['integral'] = integral '''Compute marginal distributions''' for idx in range(0,d): m, mX, mW = marginalize(result, np.array([idx])) result['marginals'].append(m) result['marginalsX'].append(mX) result['marginalsW'].append(mW) result['marginals'] = np.squeeze(result['marginals']) result['marginalsX'] = np.squeeze(result['marginalsX']) result['marginalsW'] = np.squeeze(result['marginalsW']) '''Find point estimate''' if (options['estimateType'] in ['MAP','MLE']): # get MLE estimate #start at most likely grid point index = np.where(result['Posterior'] == np.max(result['Posterior'].ravel())) Fit = np.zeros([d,1]) for idx in range(0,d): Fit[idx] = result['X1D'][idx][index[idx]] if options['expType'] == 'YesNo': fun = lambda X, f: -_l.logLikelihood(data, options, [X[0],X[1],X[2],X[3],X[4]]) x0 = _deepcopy(Fit) a = None elif options['expType'] == 'nAFC': #def func(X,f): # return -_l.logLikelihood(data,options, [X[0], X[1], X[2], f, X[3]]) #fun = func fun = lambda X, f: -_l.logLikelihood(data,options, [X[0], X[1], X[2], f, X[3]]) x0 = _deepcopy(Fit[0:3]) # Fit[3] is excluded x0 = np.append(x0,_deepcopy(Fit[4])) a = np.array([1/options['expN']]) elif options['expType'] == 'equalAsymptote': fun = lambda X, f: -_l.logLikelihood(data,options,[X[0], X[1], X[2], f, X[3]]) x0 = _deepcopy(Fit[0:3]) x0 = np.append(x0,_deepcopy(Fit[4])) a = np.array([np.nan]) else: raise ValueError('unknown expType') if options['fastOptim']: Fit = scipy.optimize.fmin(fun, x0, args = (a,), xtol=0, ftol = 0, maxiter = 100, maxfun=100) warnings.warn('changed options for optimization') else: Fit = scipy.optimize.fmin(fun, x0, args = (a,), disp = False) if options['expType'] == 'YesNo': result['Fit'] = _deepcopy(Fit) elif options['expType'] == 'nAFC': fit = _deepcopy(Fit[0:3]) fit = np.append(fit, np.array([1/options['expN']])) fit = np.append(fit, _deepcopy(Fit[3])) result['Fit'] = fit elif options['expType'] =='equalAsymptote': fit = _deepcopy(Fit[0:3]) fit = np.append(fit, Fit[2]) fit = np.append(fit, Fit[3]) result['Fit'] = fit else: raise ValueError('unknown expType') par_idx = np.where(np.isnan(options['fixedPars']) == False) for idx in par_idx[0]: result['Fit'][idx] = options['fixedPars'][idx] elif options['estimateType'] == 'mean': # get mean estimate Fit = np.zeros([d,1]) for idx in range[0:d]: Fit[idx] = np.sum(result['marginals'][idx]*result['marginalsW'][idx]*result['marginalsX'][idx]) result['Fit'] = _deepcopy(Fit) Fit = np.empty(Fit.shape) '''Include input into result''' result['options'] = options # no copies here, because they are not changing result['data'] = data '''Compute confidence intervals''' if ~options['fastOptim']: result['conf_Intervals'] = getConfRegion(result) return result
def getConfRegion(result): mode = result['options']['CImethod'] d = len(result['X1D']) ''' get confidence intervals for each parameter --> marginalize''' conf_Intervals = np.zeros((d,2, len(result['options']['confP']))) confRegion = 0 i = 0 for iConfP in result['options']['confP']: # if nargout> 1 or mode == 'project': if mode == 'project': order = np.array(result['Posterior'][:]).argsort()[::-1] Mass = result['Posterior']*result['weight'] Mass = np.cumsum(Mass[order]) confRegion = np.reshape(np.array([True]*np.size(result['Posterior']),result['Posterior'].shape)) confRegion[order[Mass >= iConfP]] = False for idx in range(0,d): confRegionM = confRegion for idx2 in range(0,d): if idx != idx2: confRegionM = np.any(confRegionM,idx2) start = result['X1D'][idx].flatten().nonzero()[0][0] stop = result['X1D'][idx].flatten().nonzero()[0][-1] conf_Intervals[idx,:,i] = [start,stop] elif mode == 'stripes': for idx in range(0,d): (margin, x, weight1D) = marginalize(result, idx) order = np.array(margin).argsort()[::-1] Mass = margin*weight1D MassSort = np.cumsum(Mass[order]) #find smallest possible percentage above confP confP1 = min(MassSort[MassSort > iConfP]) confRegionM = np.reshape(np.array([True]*np.size(margin),np.shape(margin))) confRegionM[order[MassSort>confP1]] = False ''' Now we have the confidence regions put the borders between the nearest contained and the first not contained point we move in from the outer points to collect the half of the leftover confidence from each side ''' startIndex = confRegionM.flatten().nonzero()[0][0] pleft = confP1 - iConfP if startIndex >1: start = (x[startIndex]+x[startIndex-1])/2 start += pleft/2/margin[startIndex] else: start = x[startIndex] pleft *= 2 stopIndex = confRegionM.flatten().nonzero()[0][-1] if stopIndex < len(x): stop = (x[stopIndex]+x[stopIndex+1])/2 stop -= pleft/2/margin[stopIndex] else: stop = x[stopIndex] if startIndex > 1: start += pleft/2/margin[startIndex] conf_Intervals[idx,:,i] = [start,stop] elif mode == 'percentiles': for idx in range(0,d): (margin, x, weight1D) = marginalize(result, idx) if len(x) == 1: start = x[0] stop = x[0] else: Mass = margin*weight1D cumMass = np.cumsum(Mass) confRegionM = np.logical_and(cumMass > (1-iConfP)/2,cumMass < (1-(1-iConfP)/2)) if any(confRegionM): alpha = (1-iConfP)/2 startIndex = confRegionM.flatten().nonzero()[0][0] if startIndex > 1: start = (x[startIndex-1]+x[startIndex])/2 + (alpha-cumMass[startIndex-1])/margin[startIndex] else: start = x[startIndex] + alpha/margin[startIndex] stopIndex = confRegionM.flatten().nonzero()[0][-1] if stopIndex < len(x): stop = (x[stopIndex]+x[stopIndex+1])/2 + (1-alpha-cumMass[stopIndex])/margin[stopIndex+1] else: stop = x[stopIndex] - alpha/margin[stopIndex] else: cumMass_greq_iConfP = np.array(cumMass > (1-iConfP)/2) index = cumMass_greq_iConfP.flatten().nonzero()[0][0] MMid = cumMass[index] -Mass[index]/2 start = x[index] + ((1-iConfP)/2 - MMid)/margin[index] stop = x[index] + ((1-(1-iConfP)/2)-MMid)/margin[index] conf_Intervals[idx,:,i] = np.array([start, stop]) else: raise ValueError('You specified an invalid mode') i += 1 return conf_Intervals
def getConfRegion(result): mode = result['options']['CImethod'] d = len(result['X1D']) ''' get confidence intervals for each parameter --> marginalize''' conf_Intervals = np.zeros((d, 2, len(result['options']['confP']))) confRegion = 0 i = 0 for iConfP in result['options']['confP']: if mode == 'project': order = np.array(result['Posterior'][:]).argsort()[::-1] Mass = result['Posterior'] * result['weight'] Mass = np.cumsum(Mass[order]) confRegion = np.reshape( np.array([True] * np.size(result['Posterior']), result['Posterior'].shape)) confRegion[order[Mass >= iConfP]] = False for idx in range(0, d): confRegionM = confRegion for idx2 in range(0, d): if idx != idx2: confRegionM = np.any(confRegionM, idx2) start = result['X1D'][idx].flatten().nonzero()[0][0] stop = result['X1D'][idx].flatten().nonzero()[0][-1] conf_Intervals[idx, :, i] = [start, stop] elif mode == 'stripes': for idx in range(0, d): (margin, x, weight1D) = marginalize(result, idx) order = np.array(margin).argsort()[::-1] Mass = margin * weight1D MassSort = np.cumsum(Mass[order]) #find smallest possible percentage above confP confP1 = min(MassSort[MassSort > iConfP]) confRegionM = np.reshape( np.array([True] * np.size(margin), np.shape(margin))) confRegionM[order[MassSort > confP1]] = False ''' Now we have the confidence regions put the borders between the nearest contained and the first not contained point we move in from the outer points to collect the half of the leftover confidence from each side ''' startIndex = confRegionM.flatten().nonzero()[0][0] pleft = confP1 - iConfP if startIndex > 1: start = (x[startIndex] + x[startIndex - 1]) / 2 start += pleft / 2 / margin[startIndex] else: start = x[startIndex] pleft *= 2 stopIndex = confRegionM.flatten().nonzero()[0][-1] if stopIndex < len(x): stop = (x[stopIndex] + x[stopIndex + 1]) / 2 stop -= pleft / 2 / margin[stopIndex] else: stop = x[stopIndex] if startIndex > 1: start += pleft / 2 / margin[startIndex] conf_Intervals[idx, :, i] = [start, stop] elif mode == 'percentiles': for idx in range(0, d): (margin, x, weight1D) = marginalize(result, idx) if len(x) == 1: start = x[0] stop = x[0] else: Mass = margin * weight1D cumMass = np.cumsum(Mass) confRegionM = np.logical_and( cumMass > (1 - iConfP) / 2, cumMass < (1 - (1 - iConfP) / 2)) if any(confRegionM): alpha = (1 - iConfP) / 2 startIndex = confRegionM.flatten().nonzero()[0][0] if startIndex > 0: start = (x[startIndex - 1] + x[startIndex]) / 2 + ( alpha - cumMass[startIndex - 1]) / margin[startIndex] else: start = x[startIndex] + alpha / margin[startIndex] stopIndex = confRegionM.flatten().nonzero()[0][-1] if stopIndex < len(x): stop = (x[stopIndex] + x[stopIndex + 1]) / 2 + ( 1 - alpha - cumMass[stopIndex]) / margin[stopIndex + 1] else: stop = x[stopIndex] - alpha / margin[stopIndex] else: cumMass_greq_iConfP = np.array( cumMass > (1 - iConfP) / 2) index = cumMass_greq_iConfP.flatten().nonzero()[0][0] MMid = cumMass[index] - Mass[index] / 2 start = x[index] + ( (1 - iConfP) / 2 - MMid) / margin[index] stop = x[index] + ( (1 - (1 - iConfP) / 2) - MMid) / margin[index] conf_Intervals[idx, :, i] = np.array([start, stop]) else: raise ValueError('You specified an invalid mode') i += 1 return conf_Intervals
def plot2D(result, par1, par2, colorMap=getColorMap(), labelSize=15, fontSize=10, axisHandle=None, showImediate=True): """ This function constructs a 2 dimensional marginal plot of the posterior density. This is the same plot as it is displayed in plotBayes in an unmodifyable way. The result struct is passed as result. par1 and par2 should code the two parameters to plot: 0 = threshold 1 = width 2 = lambda 3 = gamma 4 = eta Further plotting options may be passed. """ from utils import strToDim # convert strings to dimension number par1, label1 = strToDim(str(par1)) par2, label2 = strToDim(str(par2)) assert ( par1 != par2 ), 'par1 and par2 must be different numbers to code for the parameters to plot' if axisHandle == None: axisHandle = plt.gca() try: plt.axes(axisHandle) except TypeError: raise ValueError('Invalid axes handle provided to plot in.') plt.set_cmap(colorMap) marg, _, _ = marginalize(result, np.array([par1, par2])) if par1 > par2: marg = marg.T if 1 in marg.shape: if len(result['X1D'][par1]) == 1: plotMarginal(result, par2) else: plotMarginal(result, par2) else: e = [result['X1D'][par2][0], result['X1D'][par2][-1], \ result['X1D'][par1][0], result['X1D'][par1][-1]] plt.imshow(marg, extent=e) plt.ylabel(label1, fontsize=labelSize) plt.xlabel(label2, fontsize=labelSize) plt.tick_params(direction='out', right='off', top='off') for side in ['top', 'right']: axisHandle.spines[side].set_visible(False) plt.ticklabel_format(style='sci', scilimits=(-2, 4)) if (showImediate): plt.show()