1 文本格式
using System;
namespace Legalsoft.Truffer
{
public class Proposal
{
public Normaldev gau { get; set; } = null;
private double logstep { get; set; }
public Proposal(int ranseed, double lstep)
{
this.gau = new Normaldev(0.0, 1.0, (ulong)ranseed);
this.logstep = lstep;
}
public void functorMethod(State s1, State s2, ref double qratio)
{
double r = gau.doub();
if (r < 0.9)
{
s2.lam1 = s1.lam1 * Math.Exp(logstep * gau.dev());
s2.lam2 = s1.lam2 * Math.Exp(logstep * gau.dev());
s2.tc = s1.tc * Math.Exp(logstep * gau.dev());
s2.k1 = s1.k1;
s2.k2 = s1.k2;
qratio = (s2.lam1 / s1.lam1) * (s2.lam2 / s1.lam2) * (s2.tc / s1.tc);
}
else
{
r = gau.doub();
if (s1.k1 > 1)
{
if (r < 0.5)
{
s2.k1 = s1.k1;
}
else if (r < 0.75)
{
s2.k1 = s1.k1 + 1;
}
else
{
s2.k1 = s1.k1 - 1;
}
}
else
{
if (r < 0.75)
{
s2.k1 = s1.k1;
}
else
{
s2.k1 = s1.k1 + 1;
}
}
s2.lam1 = s2.k1 * s1.lam1 / s1.k1;
r = gau.doub();
if (s1.k2 > 1)
{
if (r < 0.5)
{
s2.k2 = s1.k2;
}
else if (r < 0.75)
{
s2.k2 = s1.k2 + 1;
}
else
{
s2.k2 = s1.k2 - 1;
}
}
else
{
if (r < 0.75)
{
s2.k2 = s1.k2;
}
else
{
s2.k2 = s1.k2 + 1;
}
}
s2.lam2 = s2.k2 * s1.lam2 / s1.k2;
s2.tc = s1.tc;
qratio = 1.0;
}
}
public static double mcmcstep(int m, State s, Plog plog, Proposal propose)
{
State sprop = new State();
double qratio = 0.0;
int accept = 0;
plog.get(s);
for (int i = 0; i < m; i++)
{
propose.functorMethod(s, sprop, ref qratio);
double alph = Math.Min(1.0, qratio * Math.Exp(plog.get(sprop) - s.plog));
double ran = propose.gau.doub();
if (ran < alph)
{
s = sprop;
plog.get(s);
accept++;
}
}
return accept / (double)m;
}
}
}
2 代码格式
using System;
namespace Legalsoft.Truffer
{
public class Proposal
{
public Normaldev gau { get; set; } = null;
private double logstep { get; set; }
public Proposal(int ranseed, double lstep)
{
this.gau = new Normaldev(0.0, 1.0, (ulong)ranseed);
this.logstep = lstep;
}
public void functorMethod(State s1, State s2, ref double qratio)
{
double r = gau.doub();
if (r < 0.9)
{
s2.lam1 = s1.lam1 * Math.Exp(logstep * gau.dev());
s2.lam2 = s1.lam2 * Math.Exp(logstep * gau.dev());
s2.tc = s1.tc * Math.Exp(logstep * gau.dev());
s2.k1 = s1.k1;
s2.k2 = s1.k2;
qratio = (s2.lam1 / s1.lam1) * (s2.lam2 / s1.lam2) * (s2.tc / s1.tc);
}
else
{
r = gau.doub();
if (s1.k1 > 1)
{
if (r < 0.5)
{
s2.k1 = s1.k1;
}
else if (r < 0.75)
{
s2.k1 = s1.k1 + 1;
}
else
{
s2.k1 = s1.k1 - 1;
}
}
else
{
if (r < 0.75)
{
s2.k1 = s1.k1;
}
else
{
s2.k1 = s1.k1 + 1;
}
}
s2.lam1 = s2.k1 * s1.lam1 / s1.k1;
r = gau.doub();
if (s1.k2 > 1)
{
if (r < 0.5)
{
s2.k2 = s1.k2;
}
else if (r < 0.75)
{
s2.k2 = s1.k2 + 1;
}
else
{
s2.k2 = s1.k2 - 1;
}
}
else
{
if (r < 0.75)
{
s2.k2 = s1.k2;
}
else
{
s2.k2 = s1.k2 + 1;
}
}
s2.lam2 = s2.k2 * s1.lam2 / s1.k2;
s2.tc = s1.tc;
qratio = 1.0;
}
}
public static double mcmcstep(int m, State s, Plog plog, Proposal propose)
{
State sprop = new State();
double qratio = 0.0;
int accept = 0;
plog.get(s);
for (int i = 0; i < m; i++)
{
propose.functorMethod(s, sprop, ref qratio);
double alph = Math.Min(1.0, qratio * Math.Exp(plog.get(sprop) - s.plog));
double ran = propose.gau.doub();
if (ran < alph)
{
s = sprop;
plog.get(s);
accept++;
}
}
return accept / (double)m;
}
}
}