示例#1
0
            internal YGen GenYLocal(double[] muWorker, double mu, double sigma)
            {
                YGen rep = YGen.GetEmptyObject();

                if (this.parentModel.ME.Any)
                {
                    //TODO non implanté
                    return(null);
                }
                else
                {
                    if (this.logNormalDistrn)
                    {
                        if (Data.AnyGT)
                        {
                            int[]    workerIds = DFRecordByMeasureType[Measure.MeasureType.GreaterThan].Select(rec => rec.WorkerOrdinal).ToArray();
                            double[] means     = muWorker.Extract(workerIds).Add(mu);
                            int      len       = this.Data.LogGT.Length;
                            rep.GT    = new double[len];
                            rep.LogGT = new double[len];
                            for (int i = 0; i < len; i++)
                            {
                                rep.LogGT[i] = RNorm4CensoredMeasures.RNormCensored(means[i], sigma, lower: this.Data.LogGT[i]);
                            }

                            rep.GT = rep.LogGT.Exp();
                        }

                        if (Data.AnyLT)
                        {
                            int[]    workerIds = DFRecordByMeasureType[Measure.MeasureType.LessThan].Select(rec => rec.WorkerOrdinal).ToArray();
                            double[] means     = muWorker.Extract(workerIds).Add(mu);
                            int      len       = this.Data.LogLT.Length;
                            rep.LT    = new double[len];
                            rep.LogLT = new double[len];
                            for (int i = 0; i < len; i++)
                            {
                                rep.LogLT[i] = RNorm4CensoredMeasures.RNormCensored(means[i], sigma, upper: this.Data.LogLT[i]);
                            }

                            rep.LT = rep.LogLT.Exp();
                        }

                        if (Data.AnyIntervalCensored)
                        {
                            int[]    workerIds = DFRecordByMeasureType[Measure.MeasureType.Interval].Select(rec => rec.WorkerOrdinal).ToArray();
                            double[] means     = muWorker.Extract(workerIds).Add(mu);
                            int      len       = this.Data.LogIntervalGT.Length;
                            rep.I    = new double[len];
                            rep.LogI = new double[len];
                            for (int i = 0; i < len; i++)
                            {
                                rep.LogI[i] = RNorm4CensoredMeasures.RNormCensored(means[i], sigma, lower: this.Data.LogIntervalGT[i], upper: this.Data.LogIntervalLT[i]);
                            }

                            rep.I = rep.LogI.Exp();
                        }
                    }
                    else
                    {
                        if (Data.AnyGT)
                        {
                            int[]    workerIds = DFRecordByMeasureType[Measure.MeasureType.GreaterThan].Select(rec => rec.WorkerOrdinal).ToArray();
                            double[] means     = muWorker.Extract(workerIds).Add(mu);
                            int      len       = this.Data.GT.Length;
                            rep.GT = new double[len];
                            for (int i = 0; i < len; i++)
                            {
                                rep.GT[i] = RNorm4CensoredMeasures.RNormCensored(means[i], sigma, lower: this.Data.GT[i]);
                            }
                        }

                        if (Data.AnyLT)
                        {
                            int[]    workerIds = DFRecordByMeasureType[Measure.MeasureType.LessThan].Select(rec => rec.WorkerOrdinal).ToArray();
                            double[] means     = muWorker.Extract(workerIds).Add(mu);
                            int      len       = this.Data.LT.Length;
                            rep.LT = new double[len];
                            for (int i = 0; i < len; i++)
                            {
                                rep.LT[i] = RNorm4CensoredMeasures.RNormCensored(means[i], sigma, upper: this.Data.LT[i]);
                            }
                        }

                        if (Data.AnyIntervalCensored)
                        {
                            int[]    workerIds = DFRecordByMeasureType[Measure.MeasureType.Interval].Select(rec => rec.WorkerOrdinal).ToArray();
                            double[] means     = muWorker.Extract(workerIds).Add(mu);
                            int      len       = this.Data.IntervalGT.Length;
                            rep.I    = new double[len];
                            rep.LogI = new double[len];
                            for (int i = 0; i < len; i++)
                            {
                                rep.I[i] = RNorm4CensoredMeasures.RNormCensored(means[i], sigma, lower: this.Data.IntervalGT[i], upper: this.Data.IntervalLT[i]);
                            }
                        }
                    }
                }
                return(rep);
            }//# end of y.gen.local
示例#2
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;
            }
        }