Bayesian fit of cosine wave taking longer than expected

旧巷老猫 提交于 2019-12-11 00:33:53

问题


In a recent homework, I was asked to perform a Bayesian fit over a set of data a and b using a Metropolis algorithm. The relationship between a and b is given:

e(t) = e_0*cos(w*t)

w = 2 * pi

The Metropolis algorithm is (it works fine with other fit):

def metropolis(logP, args, v0, Nsteps, stepSize):

    vCur = v0
    logPcur = logP(vCur, *args)
    v = []
    Nattempts = 0

    for i in range(Nsteps):
        while(True):
            #Propose step:
            vNext = vCur + stepSize*np.random.randn(*vCur.shape)
            logPnext = logP(vNext, *args)
            Nattempts += 1

            #Accept/reject step:
            Pratio = (1. if logPnext>logPcur else np.exp(logPnext-logPcur))

            if np.random.rand() < Pratio: 
                vCur = vNext
                logPcur = logPnext
                v.append(vCur)
                break

    acceptRatio = Nsteps*(1./Nattempts)
    return np.array(v), acceptRatio

I have tried to Bayesian fit the cosine wave and used the Metropolis algorithm above:

e_0 = -0.00155 

def strain_t(e_0,t):
    return e_0*np.cos(2*np.pi*t)

data = pd.read_csv('stressStrain.csv')
t = np.array(data['t'])
e = strain_t(e_0,t)

def logfitstrain_t(params,t,e):
    e_0 = params[0]
    sigmaR = params[1]
    strainModel = strain_t(e_0,t)
    return np.sum(-0.5*((e-strainModel)/sigmaR)**2 - np.log(sigmaR))


params0 = np.array([-0.00155,np.std(t)]) 
params, accRatio = metropolis(logfitstrain_t, (t,e), params0, 1000, 0.042)
print('Acceptance ratio:', accRatio)

e0 = np.mean(params[0])
print('e0=',e0)

e_t = e0*np.cos(2*np.pi*t)
sns.jointplot(t, e_t, kind='hex',color='purple')

The data in .csv looks like

There isn't any error message showing after I hit run, but it takes forever for python to give me an output. What did I do wrong here?


回答1:


Why it might "take forever"

Your algorithm is designed to run until it accepts a given number of proposals (1000 in the example). Thus, if it's running for a long time, you're likely rejecting a bunch of proposals. This can happen when the step size is too large, leading new proposals to end up in distant, low probability regions of the likelihood space. Try reducing your step size. This may require you to also increase the number of samples to ensure the posterior space becomes adequately explored.

A more serious concern

Because you only append accepted proposals to the chain v, you haven't actually implemented the Metropolis algorithm, and instead obtain a biased set of samples that will tend to overrepresent less likely regions of the posterior space. A true Metropolis implementation re-appends the previous proposal whenever the new proposal is rejected. You can still enforce a minimum number of accepted proposals, but you really must append something every time.



来源:https://stackoverflow.com/questions/52580995/bayesian-fit-of-cosine-wave-taking-longer-than-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!