private double TrueValueGen(GenObject genO, MeasurementError me, double y, double mu, double logY) { SGNFnAParam localA = genO.A.Clone(); localA.Mu = mu; if (genO.LogNormalDistrn) { localA.LogY = logY; } localA.Y = y; localA.Y2 = y * y; if (genO.ThroughSD) { localA.Ksi = me.Parm; localA.Ksi2 = me.Parm * me.Parm; } else { localA.CV2 = me.Parm * me.Parm; } double[] start = genO.Start(localA); //start.Any(zz => !zz.IsFinite()); Icdf icdf = new Icdf(genO, localA, range: genO.Range); double x = icdf.Bidon(start, inestLowerLim: !genO.LogNormalDistrn); if (genO.LogNormalDistrn) { x = Math.Exp(x);// # new_0.11 } return(x); } //# end of truevalue.gen
internal Icdf(GenObject o, SGNFnAParam a, double[] range, double precision = 1E-6, int NRMaxIteration = 200) { this.Ob01 = o; this.A = a == null ? o.A : a; this.Range = (range == null) ? new double[0] : range; this.Start = Tools.Combine(Range.Mean()); // donne NaN si range est de longueur 0. this.Precision = precision; this.NRMaxIter = NRMaxIteration; }
internal static MEParmGen GetInstance(GenObject o, MeasurementError me, DataSummary data, YGen genY, TrueValuesGen genTV) { MEParmGen instance = new MEParmGen(); double b; double[] tmpY, tmpT; if (me.ThroughCV) { if (o.LogNormalDistrn) { tmpY = Tools.Combine(data.LogY, genY.LogGT, genY.LogLT, genY.LogI); tmpT = Tools.Combine(genTV.LogY, genTV.LogGT, genTV.LogLT, genTV.LogI); b = tmpY.Substract(tmpT).Exp().Substract(1.0).Sqr().Sum(); b /= 2.0; } else { tmpY = Tools.Combine(data.Y, genY.GT, genY.LT, genY.I); tmpT = Tools.Combine(genTV.Y, genTV.GT, genTV.LT, genTV.I); b = tmpY.Divide(tmpT).Substract(1.0).Sqr().Reverse().Sum(); b /= 2.0; } SGNFnAParam localA = o.A.Clone(); localA.B = b; localA.Range = me.GetRange(); double[] range = me.GetRange(); Icdf icdf = new Icdf(o, localA, range); instance.Parm = icdf.Bidon(o.Start(localA), inestLowerLim: range[0] == 0); } else { tmpY = Tools.Combine(data.Y, genY.GT, genY.LT, genY.I); tmpT = Tools.Combine(genTV.Y, genTV.GT, genTV.LT, genTV.I); b = tmpY.Substract(tmpT).Sqr().Sum(); b /= 2.0; if (o.LogNormalDistrn) { SGNFnAParam localA = o.A.Clone(); localA.B = b; localA.Range = me.GetRange(); localA.Truevalues = Tools.Copy(tmpT); //me.parm <- dens.gen.icdf(o, A, range=me$range, inestimable.lower.limit=me$range[1]==0) double[] range = me.GetRange(); Icdf icdf = new Icdf(o, localA, range); instance.Parm = icdf.Bidon(inestLowerLim: range[0] == 0.0); } else { instance.Parm = WebExpoFunctions3.SqrtInvertedGammaGen(data.N, b, me.GetRange(), o); } } return(instance); }
public static SigmaTruncatedDataGen GetInstance(GenObject o, double[] range, double b, double mu, double currentSigma) { SigmaTruncatedDataGen rep = new SigmaTruncatedDataGen(); SGNFnAParam localA = o.A.Clone(); localA.B = b; localA.Mu = mu; double[] start = o.Start(localA); if (start.Length == 0) { start = Tools.Combine(currentSigma); } Icdf icdf = new Icdf(o, localA, range); rep.Sigma = icdf.Bidon(start, inestLowerLim: range[0] == 0); return(rep); }
} //# end of higher.local.max //# new_0.11 private double[] SplitStartingValuesLeftRight(GenObject o, double target, SGNFnAParam A, double[] range, double[] startValues) { double[] start = new double[2] { Tools.NA, Tools.NA }; double[] lsStart = startValues.Where(number => (number < A.Mode) && (number > range[0])).ToArray(); double[] rsStart = startValues.Where(number => (number > A.Mode) && (number < range[1])).ToArray(); if (lsStart.Length == 1) { start[0] = lsStart[0]; } else if (lsStart.Length > 1) { double[] f = new double[lsStart.Length]; for (int i = 0; i < lsStart.Length; i++) { f[i] = o.LogF(lsStart[i], A); } double[] d = f.Substract(target).Abs(); int w = d.WhichMin(); start[0] = lsStart[w]; } if (rsStart.Length == 1) { start[1] = rsStart[0]; } else if (rsStart.Length > 1) { double[] f = new double[rsStart.Length]; for (int i = 0; i < rsStart.Length; i++) { f[i] = o.LogF(rsStart[i], A); } double[] d = f.Substract(target).Abs(); int w = d.WhichMin(); start[1] = rsStart[w]; } return(start); } //# end of split.starting.values.leftRight
internal ReferencePoints( GenObject o, SGNFnAParam a, double[] start, double[] range, bool inestimableLowerLimit = false, double fRatioRemote = 1e-8, double epsilon = 1e-6, int maxNIter = 200) { this.A = a; this.O = o; this.Start = start; this.Range = range; this.InestimableLowerLimit = inestimableLowerLimit; this.FRatioRemote = fRatioRemote; this.Epsilon = epsilon; this.MaxNIter = 200; }
private double HigherLocalMax(GenObject o, SGNFnAParam A, double dipX, double[] range, double epsilon) { //# Find a starting point on each side of dip.x double[] start = o.Start(A); double tmp = start.Substract(dipX).Abs().Max(); double startLeft = start.Min(); if (startLeft > dipX) { startLeft = dipX - tmp; } double startRight = start.Max(); if (startRight < dipX) { startRight = dipX + tmp; } //# Look for starting points with negative values for h'' //# i) on left side bool cntn = o.LogFSecond(startLeft, A) > 0; while (cntn) { startLeft = startLeft - tmp; if (startLeft < range[0]) { startLeft = (range[0] + startLeft + tmp) / 2; } cntn = o.LogFSecond(startLeft, A) > 0; } //# ii) on right side cntn = o.LogFSecond(startRight, A) > 0; while (cntn) { startRight = startRight + tmp; cntn = o.LogFSecond(startRight, A) > 0; } //# Run pure Newton-Raphson to find local max on each side //# i) left side double x = startLeft; cntn = true; double hp = o.LogFPrime(x, A); while (cntn) { double change = hp / o.LogFSecond(x, A); x = x - change; hp = o.LogFPrime(x, A); cntn = Math.Abs(hp) > epsilon; } double localModeLeft = x; //# ii) right-side x = startRight; cntn = true; hp = o.LogFPrime(x, A); while (cntn) { double change = hp / o.LogFSecond(x, A); x = x - change; hp = o.LogFPrime(x, A); cntn = Math.Abs(hp) > epsilon; } double localModeRight = x; double hLeft = o.LogF(localModeLeft, A); double hRight = o.LogF(localModeRight, A); x = hLeft > hRight ? localModeLeft : localModeRight; return(x); } //# end of higher.local.max
} //# end of split.starting.values.leftRight //# new_0.11 private MaxFastTrackObject MaxFastTrack(GenObject o, SGNFnAParam A, double lowerLimit, double rightsideX, double rightsideHp, double epsilon = 1e-4, double epsilonX = 1e-20) { double x = rightsideX; double h = o.LogF(x, A); double hp = rightsideHp; //b = list(x = c(NA, x), h = c(NA, h), hp = c(NA, hp)) double[] bX = new double[2] { x, Tools.NA }; double[] bH = new double[2] { h, Tools.NA }; double[] bHp = new double[2] { hp, Tools.NA }; x = (x + lowerLimit) / 2; bool cntn = true; //# Find a point with positive slope (that is, to the left of the mode) while (cntn) { hp = o.LogFPrime(x, A); if (hp > 0) { cntn = double.IsInfinity(hp); if (cntn) { double nextX = (3 * x - lowerLimit) / 2.0; lowerLimit = x; cntn = Math.Abs(nextX - x) > epsilonX; if (cntn) { x = nextX; } } } else { x = (x + lowerLimit) / 2.0; } } hp = o.LogFPrime(x, A); bool converged = Tools.IsFinite(hp) & (hp > 0); cntn = converged; while (cntn) { int side = hp > 0 ? 0 : 1; bX[side] = x; bH[side] = o.LogF(x, A); bHp[side] = hp; double bDiff = bHp.Diff()[0]; if (bDiff == 0) { x = bX.Mean(); } else { x = (bHp.Multiply(bX).Diff()[0] - bH.Diff()[0]) / bDiff; if ((x <= bX[0]) || (x >= bX[1])) { x = bX.Mean(); } hp = o.LogFPrime(x, A); cntn = Math.Abs(hp) > epsilon; } } h = o.LogF(x, A); return(new MaxFastTrackObject(x, h, converged)); } //# end of max.fastTrack
private TrueValuesGen(YGen genY, DataSummary data, double mu, double sigma, MeasurementError me, bool logNormalDistrn = true, GenObject o = null) { if (me.ThroughSD && !logNormalDistrn) { double meSD = me.Parm; double tauStar = 1 / Math.Pow(sigma, 2) + 1 / Math.Pow(meSD, 2); double sdStar = 1 / Math.Sqrt(tauStar); if (data.YLength > 0) { double[] tmpMean = (data.Y.Divide(Math.Pow(meSD, 2)).Add(mu / Math.Pow(sigma, 2))).Divide(tauStar); this.Y = NormalDistribution.RNorm(data.YLength, tmpMean, Tools.Rep(sdStar, tmpMean.Length)); } if (data.GTLength > 0) { double[] tmpMean = (genY.GT.Divide(Math.Pow(meSD, 2)).Add(mu / Math.Pow(sigma, 2))).Divide(tauStar); this.Y = NormalDistribution.RNorm(data.GTLength, tmpMean, Tools.Rep(sdStar, tmpMean.Length)); } if (data.LTLength > 0) { double[] tmpMean = (genY.LT.Divide(Math.Pow(meSD, 2)).Add(mu / Math.Pow(sigma, 2))).Divide(tauStar); this.Y = NormalDistribution.RNorm(data.GTLength, tmpMean, Tools.Rep(sdStar, tmpMean.Length)); } if (data.IntervalLength > 0) { double[] tmpMean = (genY.I.Divide(Math.Pow(meSD, 2)).Add(mu / Math.Pow(sigma, 2))).Divide(tauStar); this.Y = NormalDistribution.RNorm(data.GTLength, tmpMean, Tools.Rep(sdStar, tmpMean.Length)); } } else { o.A.Sigma2 = sigma * sigma; this.Y = new double[data.YLength]; this.GT = new double[data.GTLength]; this.LT = new double[data.LTLength]; this.I = new double[data.IntervalLength]; for (int j = 0; j < data.YLength; j++) { this.Y[j] = TrueValueGen(o, me, data.Y[j], mu, logY: logNormalDistrn?data.LogY[j] : Tools.ND); } for (int j = 0; j < data.GTLength; j++) { this.GT[j] = TrueValueGen(o, me, genY.GT[j], mu, logY: logNormalDistrn?genY.LogGT[j] : Tools.ND); } for (int j = 0; j < data.LTLength; j++) { this.LT[j] = TrueValueGen(o, me, genY.LT[j], mu, logY: logNormalDistrn?genY.LogLT[j] : Tools.ND); } for (int j = 0; j < data.IntervalLength; j++) { this.I[j] = TrueValueGen(o, me, genY.I[j], mu, logY: logNormalDistrn?genY.LogI[j] : Tools.ND); } if (logNormalDistrn) { this.LogY = this.Y.Log(); this.LogGT = this.GT.Log(); this.LogLT = this.LT.Log(); this.LogI = this.I.Log(); } } } //# end of truevalues.gen
} //# end of truevalues.gen internal static TrueValuesGen GetInstance(YGen genY, DataSummary data, double mu, double sigma, MeasurementError me, bool logNormalDistrn = true, GenObject o = null) { return(new TrueValuesGen(genY, data, mu, sigma, me, logNormalDistrn, o)); }
//sqrt.invertedGamma.gen <- function(n, beta, xrange, o=list(), beta.min=1e-8) internal static double SqrtInvertedGammaGen(int n, double beta, double[] xRange, GenObject o, double betaMin = 1E-8) { //# Sample a value from the distrn //# //# f(x) = 1 //# ------- . exp(-beta/x^2) //# x^n double x = Tools.ND; if (n <= 1) { SGNFnAParam A = o.A.Clone(); A.B = beta; Icdf icdf = new Icdf(o, A, xRange); x = icdf.Bidon(start: Math.Sqrt(2.0 * beta), inestLowerLim: xRange[0] == 0); } else if (beta < betaMin) { if (xRange[0] == 0) { xRange[0] = 0.0001; } x = RandomPow(n, xRange); } else { double alpha = (n - 1) / 2.0; double[] invX2Range = xRange.Sqr().Reverse().ToArray().Reciprocal(); double invX2 = RGammaTruncated(alpha, beta, invX2Range); x = 1 / Math.Sqrt(invX2); } return(x); } //# end of sqrt.invertedGamma.gen
}// end constructor /* * La méthode étant internal, elle peut être invoquée d'un programme externe à la librairie. * La seule méthode qui peut invoquer Run, c'est la méthode Compute de Model qui ne le fera que si le modèle est * jugé valide. */ internal override void Run() { SGNFnAParam localA = null; GenObject oTV = null; GenObject oMu = null; GenObject oSigma = null; GenObject oME = null; YGen genY = YGen.EmptyInstance; TrueValuesGen genTV = null; double[] burninMu; double[] burninSigma; double[] burninCV = null; double[] sampleMu; double[] sampleSigma; double[] sampleCV = null; double mu; double sigma; int iter = -1, savedIter; double muCondMean; double yBar; double muCondSD; double[] pLim = new double[2]; double p; double[] muLim = new double[] { this.MuLower, this.MuUpper }; double logSigmaSD; try { logSigmaSD = 1 / Math.Sqrt(this.LogSigmaPrec); if (ME.Any) { if (ME.ThroughCV) { if (OutcomeIsLogNormallyDistributed) { oTV = new TrueValue_CV_LogN_GenObject(); } else { oTV = new TrueValue_CV_Norm_GenObject(); } } else { //oTV = new TrueValue_SD_GenObject(); } } //# modif_0.12 int combinedN = this.Data.N + (this.PastData.Defined ? PastData.N : 0); if (ME.ThroughCV && !OutcomeIsLogNormallyDistributed) { oMu = new MuTruncatedData_GenObject(combinedN); //# modif_0.12 oSigma = GenObject.GetSigmaTruncatedDataLNormGenObject(combinedN, this.LogSigmaMu, logSigmaSD); //# modif_0.12 } else { oSigma = GenObject.GetSigmaGenObject(combinedN, this.LogSigmaMu, logSigmaSD); //# modif_0.12 } localA = oSigma.A.Clone(); if (ME.Any && !ME.Known) { oME = GenObject.GetMeGenObject(this.ME, this.OutcomeIsLogNormallyDistributed, this.Data.N); } int nIterations = NBurnin + NIter * NThin; //les tableaux pour les chaines sampleMu = Result.Chains.GetChain("muSample"); sampleSigma = Result.Chains.GetChain("sdSample"); burninMu = Result.Chains.GetChain("muBurnin"); burninSigma = Result.Chains.GetChain("sdBurnin"); if (ME.ThroughCV) { sampleCV = Result.Chains.GetChain("cvSample"); burninCV = Result.Chains.GetChain("cvBurnin"); } bool inestimableLowerLimit = false; //Initial values for mu and sigma mu = InitMu; sigma = InitSigma; savedIter = 0; // pour les échantillons if (this.Data.AnyCensored) { genY = YGen.Inits(this.Data, mu, sigma, meThroughCV: this.ME.ThroughCV, logNormalDistrn: OutcomeIsLogNormallyDistributed); } if (ME.Any) { ME.Parm = ME.InitialValue; } //Boucle principale for (iter = 0; iter < nIterations; iter++) { if (ME.Any) { genTV = TrueValuesGen.GetInstance(genY, this.Data, mu, sigma, this.ME, logNormalDistrn: OutcomeIsLogNormallyDistributed, o: oTV); } if (this.Data.AnyCensored) { //y.gen(true.values, data, sigma, me, outcome.is.logNormally.distributed, mu=mu) //On ne tient pas compte de true.values, ni de me ... genY = YGen.GetInstance(this.ME, genTV, this.Data, mu, sigma, OutcomeIsLogNormallyDistributed); } OutLogoutMoments moments = OutLogoutMoments.Get(this.ME.Any, this.OutcomeIsLogNormallyDistributed, this.Data, genY, genTV); double sigmaBeta = (moments.Sum2 - 2 * mu * moments.Sum + this.Data.N * mu * mu) / 2.0; if (PastData.Defined) { sigmaBeta = sigmaBeta + PastData.N / 2.0 * Math.Pow(PastData.Mean - mu, 2) + PastData.NS2 / 2.0; } double[] start = new double[0]; if (this.ME.ThroughCV && !OutcomeIsLogNormallyDistributed) { //ici // A <- c(o.sigma$A, list(b=sigma.beta, mu=mu)) localA = oSigma.A.Clone(); localA.B = sigmaBeta; localA.Mu = mu; start = Tools.Combine(sigma); inestimableLowerLimit = false; } else { localA.B = sigmaBeta; start = oSigma.Start(localA); inestimableLowerLimit = true; } Icdf icdf = new Icdf(oSigma, localA, Tools.Combine(0, double.PositiveInfinity)); sigma = icdf.Bidon(start, inestimableLowerLimit); yBar = moments.Sum / this.Data.N; muCondMean = this.PastData.Defined ? (moments.Sum + PastData.N * PastData.Mean) / combinedN : yBar; // # new_0.12 if (this.ME.ThroughCV && !this.OutcomeIsLogNormallyDistributed) { mu = MuTruncatedGen.GetInstance(oMu, muLim, muCondMean, sigma).Mu; } else { muCondSD = sigma / Math.Sqrt(combinedN); pLim = NormalDistribution.PNorm(muLim.Substract(muCondMean).Divide(muCondSD)); p = UniformDistribution.RUnif(1, pLim[0], pLim[1])[0]; mu = NormalDistribution.QNorm(p, mu: muCondMean, sigma: muCondSD); } //# Sample Measurement Error from its posterior density if (this.ME.Any && !this.ME.Known) { this.ME.Parm = MEParmGen.GetInstance(oME, this.ME, this.Data, genY, genTV).Parm; } if (iter < NBurnin) { if (MonitorBurnin) { burninMu[iter] = mu; burninSigma[iter] = sigma; if (this.ME.Any && !this.ME.Known) { burninCV[iter] = ME.Parm; } } } else if ((iter - NBurnin) % NThin == 0) { sampleMu[savedIter] = mu; sampleSigma[savedIter] = sigma; if (this.ME.Any && !this.ME.Known) { sampleCV[savedIter] = ME.Parm; } savedIter++; } }// for( int iter = 1 ... } catch (Exception ex) { this.Result.Messages.AddError(WEException.GetStandardMessage(ex, iter, Result.PRNGSeed), this.ClassName); return; } } //end Run
}// end constructor internal override void Run() { YGen genY = YGen.EmptyInstance; // Pourra probablement prendre plusieurs formes. TrueValuesGen genTV = null; GenObject oSigma = EmptyGenObject.Instance; GenObject oMu = EmptyGenObject.Instance; GenObject oME = null; GenObject oTV = null; double[] burninMu; double[] burninSigma; double[] burninCV = null; double[] sampleMu; double[] sampleSigma; double[] sampleCV = null; double mu; double sigma; int iter = -1, savedIter; double muCondMean; double muCondSD; try { //# Prepare dens.gen.icdf objects if (ME.Any) { if (ME.ThroughCV) { if (OutcomeIsLogNormallyDistributed) { oTV = new TrueValue_CV_LogN_GenObject(); } else { oTV = new TrueValue_CV_Norm_GenObject(); } } else { //oTV = new TrueValue_SD_GenObject(); } } if (ME.ThroughCV && !OutcomeIsLogNormallyDistributed) { oMu = new MuTruncatedData_GenObject(this.Data.N); //# modif_0.12 oSigma = GenObject.GetSigmaTruncatedDataGenObject(this.Data.N); //# modif_0.12 } else { oSigma = EmptyGenObject.Instance; } if (ME.Any && !ME.Known) { oME = GenObject.GetMeGenObject(this.ME, this.OutcomeIsLogNormallyDistributed, this.Data.N); } int nIterations = NBurnin + NIter * NThin; //les tableaux pour les chaines sampleMu = Result.Chains.GetChain("muSample"); sampleSigma = Result.Chains.GetChain("sdSample"); burninMu = Result.Chains.GetChain("muBurnin"); burninSigma = Result.Chains.GetChain("sdBurnin"); if (ME.ThroughCV) { sampleCV = Result.Chains.GetChain("cvSample"); burninCV = Result.Chains.GetChain("cvBurnin"); } //Initial values for mu and sigma mu = this.InitMu; sigma = this.InitSD; //# Initialize measured values for subjects with censored values [modif_0.10] if (this.Data.AnyCensored) { genY = YGen.Inits(data: this.Data, mu: mu, sigma: sigma, meThroughCV: true, logNormalDistrn: OutcomeIsLogNormallyDistributed); } if (ME.Any) { ME.Parm = ME.InitialValue; } //# Start MCMC savedIter = 0; // pour les échantillons for (iter = 0; iter < nIterations; iter++) { //# Sample true values (in presence of measurement error) [new_0.10] if (ME.Any) { genTV = TrueValuesGen.GetInstance(genY, this.Data, mu, sigma, this.ME, OutcomeIsLogNormallyDistributed, o: oTV); } //# Sample y values for censored observations if (this.Data.AnyCensored) { //y.gen(true.values, data, sigma, me, outcome.is.logNormally.distributed, mu=mu) //On ne tient pas compte de true.values, ni de me ... genY = YGen.GetInstance(this.ME, genTV, this.Data, mu, sigma, OutcomeIsLogNormallyDistributed); } //# Compute data points sum and square sum OutLogoutMoments moments = OutLogoutMoments.Get(this.ME.Any, this.OutcomeIsLogNormallyDistributed, this.Data, genY, genTV); //# Sample from f(sigma | mu) //# modif_0.10 double sigmaBeta = (moments.Sum2 - 2 * mu * moments.Sum + this.Data.N * mu * mu) / 2.0; if (sigmaBeta < 1e-6) { sigmaBeta = 0; // # protection against numeric imprecision } if (this.ME.ThroughCV && !OutcomeIsLogNormallyDistributed) { sigma = SigmaTruncatedDataGen.GetInstance(oSigma, SDRange, sigmaBeta, mu, sigma).Sigma; } else { sigma = WebExpoFunctions3.SqrtInvertedGammaGen(Data.N, sigmaBeta, this.SDRange, oSigma); } muCondMean = moments.Sum / this.Data.N; if (this.ME.ThroughCV && !this.OutcomeIsLogNormallyDistributed) { mu = MuTruncatedGen.GetInstance(oMu, Tools.Combine(MuLower, MuUpper), muCondMean, sigma).Mu; } else { muCondSD = sigma / Math.Sqrt(this.Data.N); mu = RNorm4CensoredMeasures.RNormCensored(muCondMean, muCondSD, lower: this.MuLower, upper: this.MuUpper); } //# Sample Measurement Error from its posterior density if (this.ME.Any && !ME.Known) { this.ME.Parm = MEParmGen.GetInstance(oME, this.ME, this.Data, genY, genTV).Parm; } if (iter < NBurnin) { if (MonitorBurnin) { burninMu[iter] = mu; burninSigma[iter] = sigma; if (this.ME.Any && !this.ME.Known) { burninCV[iter] = ME.Parm; } } } else if ((iter - NBurnin) % NThin == 0) { sampleMu[savedIter] = mu; sampleSigma[savedIter] = sigma; if (this.ME.Any && !this.ME.Known) { sampleCV[savedIter] = ME.Parm; } savedIter++; } }// for( int iter = 1 ... } catch (Exception ex) { this.Result.Messages.AddError(WEException.GetStandardMessage(ex, iter, Result.PRNGSeed), this.ClassName); return; } }//compute
internal override void Run() { ChainNamePair b_s; b_s = Mcmc.GetChainNames("muOverall"); double[] muOverallBurnin = Result.Chains.GetChain(b_s.Burnin); double[] muOverallSample = Result.Chains.GetChain(b_s.Sample); b_s = Mcmc.GetChainNames("sigmaWithin"); double[] sigmaWithinBurnin = Result.Chains.GetChain(b_s.Burnin); double[] sigmaWithinSample = Result.Chains.GetChain(b_s.Sample); b_s = Mcmc.GetChainNames("sigmaBetween"); double[] sigmaBetweenBurnin = Result.Chains.GetChain(b_s.Burnin); double[] sigmaBetweenSample = Result.Chains.GetChain(b_s.Sample); double[][] workerBurnin = new double[Data.NWorkers][]; double[][] workerSample = new double[Data.NWorkers][]; int iTag = 0; foreach (string tag in Data.WorkersByTag.Keys) { b_s = Mcmc.GetWorkerChainNames(tag); workerBurnin[iTag] = Result.Chains.GetChain(b_s.Burnin); workerSample[iTag] = Result.Chains.GetChain(b_s.Sample); iTag++; } int iter = -1, savedIter = 0; try { double logSigmaWithinSD = 1 / Math.Sqrt(LogSigmaWithinPrec); double logSigmaBetweenSD = 1 / Math.Sqrt(LogSigmaBetweenPrec); //# Prepare dens.gen.icdf objects if (this.ME.Any) { //o.tv < -truevalue.gen.object(me, outcome.is.logNormally.distributed) } if (this.ME.ThroughCV && !this.OutcomeIsLogNormallyDistributed) { //o.mu.overall < -mu.truncatedData.gen.local.object(data$N, data$worker$count) //o.mu.worker < -mu.worker.truncatedData.gen.object(data$worker$count) } GenObject oSB = null, oSW = null; if (this.UseUniformPriorOnSds) { if (Data.NWorkers <= 1) { oSB = new Sigma_woLM_GenObject(Data.NWorkers); } else { oSB = null; } } else { oSB = new Sigma_LM_GenObject(Data.NWorkers, this.LogSigmaBetweenMu, logSigmaBetweenSD); } if (this.ME.ThroughCV && !this.OutcomeIsLogNormallyDistributed) { //if (use.uniform.prior.on.sds) //{ // o.sw < -sigma.within.truncatedData.gen.object(data$N, data$worker$count, T, range = sigma.within.range) //} //else //{ // o.sw < -sigma.within.truncatedData.gen.object(data$N, data$worker$count, F, lnorm.mu = log.sigma.within.mu, lnorm.sd = log.sigma.within.sd) //} } else { if (this.UseUniformPriorOnSds) { if (Data.N <= 1) { oSW = new Sigma_woLM_GenObject(Data.N); } else { oSW = null; } } else { oSW = new Sigma_LM_GenObject(this.Data.N, lNormMu: this.LogSigmaWithinMu, lNormSd: logSigmaWithinSD); } } if (this.ME.Any && !this.ME.Known) { //o.me < -me.gen.object(me, outcome.is.logNormally.distributed, data$N) } double muOverall = this.InitMuOverall; double sigmaWithin = InitSigmaWithin; //# Initialize measured values for subjects with censored values [new_0.10] YGen genY = YGen.GetEmptyObject(); WorkerData workerData = new WorkerData(this); this.ResultParams = new Object[1]; this.ResultParams[0] = this.Data.WorkersByTag.Keys; if (this.Data.AnyCensored) { genY = YGen.Inits(data: this.Data, mu: muOverall, sigma: sigmaWithin, meThroughCV: false, logNormalDistrn: OutcomeIsLogNormallyDistributed); workerData.UpdateGeneratedValues(genY); } double[] muWorker = workerData.MuWorker; workerData.AdjustMuOverall(this.MuOverallLower, this.MuOverallUpper); muOverall = workerData.MuOverall; muWorker = muWorker.Substract(muOverall); // # center mu.worker double[] predicted = workerData.GetPredictedMeans(muOverall); sigmaWithin = workerData.GetSigmaWithin(); int nIterations = NBurnin + NIter * NThin; for (iter = 0; iter < nIterations; iter++) { if (this.ME.Any) { //true.values < -truevalues.gen.local(gen.y, data, me, mu.worker, o = o.tv) } //# Sample y values for censored observations if (this.Data.AnyCensored) { //function(true.values, data, me, mu.worker, mu=mu.overall, sigma=sigma.within, logNormal.distrn=outcome.is.logNormally.distributed) genY = workerData.GenYLocal(muWorker, muOverall, sigmaWithin); workerData.UpdateGeneratedValues(genY); } double[] yWorkerAvg = workerData.MuWorker; double yAvg = workerData.Average; //# Sample from f(sigma.within | other parms) double[] residuals = workerData.GetGenValues().Substract(muWorker.Extract(workerData.WorkerIds)).Substract(muOverall); double b = residuals.Sqr().ToArray().Sum() / 2.0; SGNFnAParam localA = null; if (this.ME.ThroughCV && !this.OutcomeIsLogNormallyDistributed) { //A < -c(o.sw$A, list(b = b, mu = mu.overall, muk = mu.worker)) //sigma.within < -dens.gen.icdf(o.sw, A, range = o.sw$range, start = sigma.within, inestimable.lower.limit = o.sw$inestimable.lower.limit) } else { if (this.UseUniformPriorOnSds) { sigmaWithin = WebExpoFunctions3.SqrtInvertedGammaGen(Data.N, b, SigmaWithinRange.Copy(), oSW); } else { localA = oSW.A.Clone(); localA.B = b; localA.Mu = muOverall; localA.Muk = muWorker; Icdf icdf = new Icdf(oSW, localA, range: Tools.Combine(0, double.PositiveInfinity)); sigmaWithin = icdf.Bidon(start: oSW.Start(localA), inestLowerLim: true); } } //# Sample from f(sigma.between | other parms) b = muWorker.Sqr().Sum() / 2.0; double sigmaBetween = 0; if (UseUniformPriorOnSds) { sigmaBetween = WebExpoFunctions3.SqrtInvertedGammaGen(Data.NWorkers, b, this.SigmaBetweenRange.Copy(), oSB); } else { localA = oSB.A.Clone(); localA.B = b; Icdf icdf = new Icdf(oSB, localA, range: Tools.Combine(0, double.PositiveInfinity)); sigmaBetween = icdf.Bidon(start: oSB.Start(localA), inestLowerLim: true); } //# Sample from f(mu.overall | other parms) //# modif_0.10 double tmpMean = yAvg - (muWorker.Multiply(this.Data.WorkerCounts).Sum() / this.Data.N); if (this.ME.ThroughCV && !OutcomeIsLogNormallyDistributed) { //muOverall = mu.truncatedData.gen.local(o.mu.overall, tmp.mean, sigma.within, mu.worker, mu.overall.range, current.value = mu.overall) } else { double tmpSD = sigmaWithin / Math.Sqrt(this.Data.N); muOverall = RNorm4CensoredMeasures.RNormCensored(tmpMean, tmpSD, lower: this.MuOverallLower, upper: this.MuOverallUpper); } //# Sample from f(mu.worker's | other parms) //# modif_0.10 double[] sigma2A = Tools.Rep(Math.Pow(sigmaWithin, 2.0), this.Data.NWorkers).Divide(this.Data.WorkerCounts); // # vector of length '# of workers' double sigma2B = Math.Pow(sigmaBetween, 2); // # scalar double[] muA = yWorkerAvg.Substract(muOverall); // # vector of length '# of workers' double[] mukStar = muA.Multiply(sigma2B).Divide(sigma2A.Add(sigma2B)); // # vector of length '# of workers' double[] s2kStar = sigma2A.Multiply(sigma2B).Divide(sigma2A.Add(sigma2B)); // # vector of length '# of workers' if (this.ME.ThroughCV && !OutcomeIsLogNormallyDistributed) { //muWorker = mu.worker.truncatedData.gen(o.mu.worker, muk.star, s2k.star, mu.overall, sigma.within, mu.worker) } else { muWorker = NormalDistribution.RNorm(this.Data.NWorkers, mu: mukStar, sigma: s2kStar.Sqrt()); } if (iter < NBurnin) { if (MonitorBurnin) { muOverallBurnin[iter] = muOverall; sigmaBetweenBurnin[iter] = sigmaBetween; sigmaWithinBurnin[iter] = sigmaWithin; SaveWorkerChainData(iter, muWorker, workerBurnin); } } else if ((iter - NBurnin) % NThin == 0) { muOverallSample[savedIter] = muOverall; sigmaBetweenSample[savedIter] = sigmaBetween; sigmaWithinSample[savedIter] = sigmaWithin; SaveWorkerChainData(savedIter, muWorker, workerSample); savedIter++; } } //for ... } catch (Exception ex) { this.Result.Messages.AddError(WEException.GetStandardMessage(ex, iter, Result.PRNGSeed), this.ClassName); return; } }