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 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); }
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 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