Esempio n. 1
0
        internal override double LogFSecond(double mu, SGNFnAParam A)
        {
            double     z     = mu / A.S;
            LPhiObject lphio = WebExpoFunctions3.LPhi(z);

            return(A.N / A.S2 * (z * lphio.R[0] + lphio.R2[0] - 1));
        }
Esempio n. 2
0
        public SEGUninformativeModel(MeasureList measures, UninformativeModelParameters specificParams, McmcParameters mcmcParams = null)
            : base(measures, specificParams.LogNormalDstrn, /*new Range(specificParams.MuLower, specificParams.MuUpper, specificParams.InitMu),*/ mcmcParams)
        {
            this.MuLower = specificParams.MuLower;
            this.MuUpper = specificParams.MuUpper;
            this.SDRange = specificParams.SDRange;
            this.InitMu  = specificParams.InitMu;
            this.InitSD  = specificParams.InitSD;
            this.Result  = new ModelResult(this, "mu", "sd");
            bool testSD = double.IsNaN(this.InitSD) || (this.InitSD == 0);

            if (Tools.IsND(this.InitMu) || testSD)
            {
                InitialValues initVals = WebExpoFunctions3.DefaultInits(this.Data, this.OutcomeIsLogNormallyDistributed, Tools.Combine(this.MuLower, this.MuUpper), this.SDRange, includeCensoredData: false);
                if (Tools.IsND(this.InitMu))
                {
                    this.InitMu = initVals.Mu;
                }

                if (testSD)
                {
                    this.InitSD = initVals.SigmaWithin;
                }
            }

            if (this.ME.ThroughCV)
            {
                this.Result.Chains.Add("cv");
            }
        }// end constructor
Esempio n. 3
0
        internal override double LogFPrime(double mu, SGNFnAParam A)
        {
            double     z     = mu / A.S;
            LPhiObject lphio = WebExpoFunctions3.LPhi(z);

            return(-A.N * (mu - A.MuMean) / A.S2 - A.N * lphio.R[0] / A.S);
        }
Esempio n. 4
0
        internal override double LogFSecond(double s, SGNFnAParam A)
        {
            double     z = A.Mu / s;
            LPhiObject l = WebExpoFunctions3.LPhi(z);

            return(A.N / Math.Pow(s, 2) - 6.0 * A.B / Math.Pow(s, 4) + A.N * z * ((Math.Pow(z, 2) - 2.0) * l.R[0] + z * l.R2[0]) / Math.Pow(s, 2));
        }
Esempio n. 5
0
        internal override double LogFPrime(double s, SGNFnAParam A)
        {
            double     z = A.Mu / s;
            LPhiObject l = WebExpoFunctions3.LPhi(z);

            return(-A.N / s + 2.0 * A.B / Math.Pow(s, 3) + A.N * z * l.R[0] / s);
        }
Esempio n. 6
0
        internal override double LogFPrime(double v, SGNFnAParam A)
        {
            LPhiObject l = WebExpoFunctions3.LPhi(1 / v);

            return
                (-A.N / v
                 + 2.0 * A.B / Math.Pow(v, 3)
                 + A.N * l.R[0] / Math.Pow(v, 2));
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        internal override double LogFSecond(double s, SGNFnAParam A)
        {
            //    log.f.prime <- function(s, A){z <- exp(s)/A$ksi; l <- lphi(z); -l$r*z + A$y*z/A$ksi - z^2 - (s-A$mu)/A$sigma2}
            double[]   z     = new double[] { Math.Exp(s) / A.Ksi };
            LPhiObject lphio = WebExpoFunctions3.LPhi(z);
            double     r0    = lphio.R[0];
            double     z0    = z[0];

            return(r0 * (Math.Pow(z0, 3) - z[0]) + Math.Pow(r0 * z0, 2) + (A.Y * Math.Exp(s) - 2.0 * Math.Exp(2 * s)) / A.Ksi2 - 1.0 / A.Sigma2);
        }
Esempio n. 9
0
        internal override double LogFSecond(double v, SGNFnAParam A)
        {
            LPhiObject l  = WebExpoFunctions3.LPhi(1 / v);
            double     v2 = Math.Pow(v, 2);
            double     v4 = Math.Pow(v2, 2);

            return
                (A.N / v2
                 - 6.0 * A.B / v4
                 + A.N / v4 * (l.R[0] * (1 / v - 2.0 * v) + l.R2[0]));
        }
Esempio n. 10
0
        internal override double LogFPrime(double s, SGNFnAParam A)
        {
            double     z = A.Mu / s;
            LPhiObject l = WebExpoFunctions3.LPhi(z);

            return
                (-(A.N + 1.0) / s
                 + 2.0 * A.B / Math.Pow(s, 3)
                 - (Math.Log(s) - A.LM) / A.LS2 / s
                 + A.N * z * l.R[0] / s);
        }
Esempio n. 11
0
        internal override double LogFPrime(double s, SGNFnAParam A)
        {
            //    log.f.prime <- function(s, A){z <- exp(s)/A$ksi; l <- lphi(z); -l$r*z + A$y*z/A$ksi - z^2 - (s-A$mu)/A$sigma2}
            double     z     = Math.Exp(s) / A.Ksi;
            LPhiObject lphio = WebExpoFunctions3.LPhi(z);

            return
                (-lphio.R[0] * z
                 + A.Y * z / A.Ksi
                 - Math.Pow(z, 2) - (s - A.Mu) / A.Sigma2);
        }
Esempio n. 12
0
        public double Bidon()
        {
            ReferencePoints rp         = new ReferencePoints(this.Ob01, this.A, this.Start.Copy(), this.Range, this.InestimableLowerLimit, epsilon: this.Precision);
            LModeExt        refPtsMode = rp.GetPoints();
            bool            cntn       = false;
            double          uMode;
            double          x;
            double          target;
            double          areaMode;
            bool            distrnLeftSide = false;
            double          mathLowerLimit = this.Ob01.MathLowerLimit;
            bool            converged      = false;
            double          areaX;
            double          precision = this.Precision;
            Func <double, SGNFnAParam, double> area;

            if (refPtsMode.Found)
            {
                // A$f < -o$f
                this.A.F          = this.Ob01.F;
                this.A.LowerLimit = refPtsMode.Remote[0];
                this.A.M          = refPtsMode.H; // # (kind of a) standardizing constant
                area = this.A.AreaFnFromALLimToX;
                double areaTot = WebExpoFunctions3.SmoothedAreaEstimate(
                    area,
                    refPtsMode.Remote[1],
                    this.A,
                    mathLowerLimit,
                    refPtsMode.Remote,
                    this.InestimableLowerLimit,
                    remoteRight: true);

                target    = this.U * areaTot;
                precision = this.Precision * areaTot;
                x         = refPtsMode.X;
                areaX     = 0;
                areaMode  = WebExpoFunctions3.SmoothedAreaEstimate(area, x, this.A, mathLowerLimit, refPtsMode.Remote, this.InestimableLowerLimit);
                uMode     = areaMode / areaTot;
                converged = Math.Abs(areaMode - target) < precision;
                cntn      = !converged;
            }
            else
            {
                throw new WEException("Mode not found");
            }

            if (cntn)
            {
                distrnLeftSide = this.U < uMode;
                if (distrnLeftSide)
                {
                    area = this.A.AreaFn_FromXToAULim;
                    this.A.UpperLimit = refPtsMode.X;
                    this.Range        = Tools.Combine(refPtsMode.Remote[0], refPtsMode.X);
                }
                else
                {
                    this.A.LowerLimit = refPtsMode.X;
                    this.Range        = Tools.Combine(refPtsMode.X, refPtsMode.Remote[1]);
                }
            }

            //converged = true;
            if (cntn && this.Ob01.PotentiallyBimodal)
            {
                // Slower but safer algorithm for posterior distrns that are potentially bimodal
                double fMode = A.F(refPtsMode.X, A);
                double start = refPtsMode.X + (target - areaMode) / fMode;
                cntn      = false;
                converged = true; // voir l'impact !!!

                double highestPoint;
                double hMax;
                if (distrnLeftSide)
                {
                    highestPoint = refPtsMode.Remote[0];
                    hMax         = areaMode;
                    target       = (uMode - this.U) / uMode * hMax;
                }
                else
                {
                    highestPoint = refPtsMode.Remote[1];
                    hMax         = WebExpoFunctions3.SmoothedAreaEstimate(area, refPtsMode.Remote[1], this.A, mathLowerLimit, this.Range, this.InestimableLowerLimit, remoteRight: true);
                    target       = (this.U - uMode) / (1 - uMode) * hMax;
                }

                SecuredNRA      oCum = new SecuredNRA(area, this.Ob01.F);
                SecuredNRSearch nr   = new SecuredNRSearch(oCum, this.A, start, precision, this.Range,
                                                           maxPoint: new LMode(highestPoint, hMax), target: target, inestimableLowerLimit: this.InestimableLowerLimit);
                // le dernier paramètre a une valeur par défaut: expectedHpSign = -1
                if (nr.Converged)
                {
                    x = nr.X;
                }
                else
                {
                    //# it seems we were caught in an infinite loop, had we not limited the number of iterations;
                    //# see if we can rule that problem out
                    x = nr.Bounds.X[0];
                    double h = nr.Bounds.H[0];
                    x = ICDFFunctions.PingpongTieBreaker(area, this.A, x, h, target, mathLowerLimit, this.Range, this.InestimableLowerLimit, precision, distrnLeftSide);
                }
            }

            int direction = 0;
            int count     = 0;

            if (cntn)
            {
                direction = distrnLeftSide ? -1 : 1;
                if (distrnLeftSide)
                {
                    target = (uMode - this.U) / uMode * areaMode;
                }
                else
                {
                    double areaRemoteRight = WebExpoFunctions3.SmoothedAreaEstimate(
                        area,
                        refPtsMode.Remote[1],
                        this.A,
                        mathLowerLimit,
                        this.Range,
                        this.InestimableLowerLimit,
                        remoteRight: true);
                    target = (this.U - uMode) / (1 - uMode) * areaRemoteRight;
                }
            }

            while (cntn)
            {
                double hp     = this.Ob01.F(x, A);
                double change = (target - areaX) / hp * direction;
                x         = x + change;
                areaX     = area(x, A);
                count     = count + 1;
                converged = Math.Abs(areaX - target) < precision;
                cntn      = !converged && count <= this.NRMaxIter;
            }

            if (!converged)
            {
                if (x > refPtsMode.Remote[1] || x < refPtsMode.Remote[0])
                {
                    throw new WEException("Stepped out of bounds -- due to numerical imprecision?");
                }
                else
                {
                    //# it seems we were caught in an infinite loop, had we not limited the number of iterations;
                    //# see if we can rule that problem out
                    x = ICDFFunctions.PingpongTieBreaker(area, this.A, x, areaX, target, mathLowerLimit, this.Range, this.InestimableLowerLimit, precision, distrnLeftSide);
                }
            }
            return(x);
        }
Esempio n. 13
0
        }// 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
Esempio n. 14
0
        internal static double PingpongTieBreaker(
            Func <double, SGNFnAParam, double> area,
            SGNFnAParam A,
            double start,
            double areaX,
            double target,
            double mathLowerLimit,
            double[] refRange,
            bool inestimableLowerLimit,
            double epsilon,
            bool distrnLeftSide = false,
            int maxCount        = 100)
        {
            int    hpMult   = distrnLeftSide ? -1 : 1;
            bool   cntn     = true;
            int    count    = 0;
            double x        = start;
            Visits visitedX = new Visits(maxCount);

            visitedX.Add(x);
            bool converged    = false;
            bool caughtInLoop = false;

            while (cntn)
            {
                double hp     = A.F(x, A) * hpMult;
                double change = (target - areaX) / hp;
                x = x + change;
                // new_0.11
                if (x < refRange[0])
                {
                    x = (refRange[0] == mathLowerLimit) && inestimableLowerLimit ? (x - change + mathLowerLimit) / 2.0 : refRange[0];
                }

                areaX = WebExpoFunctions3.SmoothedAreaEstimate(area, x, A, mathLowerLimit, refRange, inestimableLowerLimit, hpMult: hpMult);
                count++;
                converged = Math.Abs(areaX - target) < epsilon;
                visitedX.Add(x);
                caughtInLoop = visitedX.CaughtInLoop; // La propiété CaughtInLoop est mise à jour à chaque ajout d'un x.
                                                      // x doit être fini, et avoir déjà été visité.
                cntn = !converged && (visitedX.Count <= maxCount) && !caughtInLoop;
            }

            if (!converged && caughtInLoop)
            {
                //  We have found the series of points that are repeatedly visited:
                // recalibrate and try Newton-Raphson again
                double[] tail = visitedX.GetFromTail(x);

                x     = (distrnLeftSide) ? x = tail.Max() : tail.Min();
                areaX = WebExpoFunctions3.SmoothedAreaEstimate(area, x, A, mathLowerLimit, refRange, inestimableLowerLimit, hpMult: hpMult);

                if (distrnLeftSide)
                {
                    A.UpperLimit = x;
                }
                else
                {
                    A.LowerLimit = x;
                }

                target = target - areaX;
                areaX  = 0.0;
                count  = 0;
                cntn   = true;
                while (cntn)
                {
                    double hp     = A.F(x, A) * hpMult;
                    double change = (target - areaX) / hp;
                    x         = x + change;
                    areaX     = area(x, A);
                    count     = count + 1;
                    converged = Math.Abs(areaX - target) < epsilon;
                    cntn      = !converged && (count <= maxCount);
                }
            }

            if (!converged)
            {
                //TODO Exception
                throw new WEException("Newton-Raphson algorithm did not converge. Sorry.\n");
            }

            return(x);
        } // end of ping.pong.tie.breaker
Esempio n. 15
0
        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;
            }
        }