本文主要是介绍C#,数值计算——数据建模Proposal的计算方法与源程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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;}}
}
这篇关于C#,数值计算——数据建模Proposal的计算方法与源程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!