示例#1
0
        private YGen(MeasurementError me, TrueValuesGen genTV, DataSummary data, double mu, double sigma, bool logNormalDistrn = true)
        {
            if (me.Any)
            {
                // Sample y (censored) values | true values
                if (data.AnyGT)
                {
                    double[] tmpMean = genTV.GT;
                    double[] tmpSD;
                    if (me.ThroughCV)
                    {
                        tmpSD = tmpMean.Multiply(me.Parm);
                    }
                    else
                    {
                        tmpSD = Tools.Rep(me.Parm, tmpMean.Length);
                    }

                    this.GT = RNorm4CensoredMeasures.RNormCensored(tmpMean, tmpSD, lower: data.GT);
                    if (logNormalDistrn)
                    {
                        this.LogGT = this.GT.Log();
                    }
                }

                if (data.AnyLT)
                {
                    double[] tmpMean = genTV.LT;
                    double[] tmpSD;
                    if (me.ThroughCV)
                    {
                        tmpSD = tmpMean.Multiply(me.Parm);
                    }
                    else
                    {
                        tmpSD = Tools.Rep(me.Parm, tmpMean.Length);
                    }

                    this.I = RNorm4CensoredMeasures.RNormCensored(tmpMean, tmpSD, upper: data.LT, negativeDisallowed: logNormalDistrn || me.ThroughCV);
                    if (logNormalDistrn)
                    {
                        this.LogLT = this.LT.Log();
                    }
                }

                if (data.AnyIntervalCensored)
                {
                    double[] tmpMean = genTV.I;
                    double[] tmpSD;
                    if (me.ThroughCV)
                    {
                        tmpSD = tmpMean.Multiply(me.Parm);
                    }
                    else
                    {
                        tmpSD = Tools.Rep(me.Parm, tmpMean.Length);
                    }

                    this.I = RNorm4CensoredMeasures.RNormCensored(tmpMean, tmpSD, lower: data.IntervalGT, upper: data.IntervalLT);
                    if (logNormalDistrn)
                    {
                        this.LogI = this.I.Log();
                    }
                }
            }
            else
            {
                if (logNormalDistrn)
                {
                    if (data.AnyGT)
                    {
                        this.LogGT = RNorm4CensoredMeasures.RNormCensored(mu, sigma, lower: data.LogGT);
                        this.GT    = this.LogGT.Exp();
                    }

                    if (data.AnyLT)
                    {
                        this.LogLT = RNorm4CensoredMeasures.RNormCensored(mu, sigma, upper: data.LogLT);
                        this.LT    = this.LogLT.Exp();
                    }

                    if (data.AnyIntervalCensored)
                    {
                        this.LogI = RNorm4CensoredMeasures.RNormCensored(mu, sigma, lower: data.LogIntervalGT, upper: data.LogIntervalLT);
                        this.I    = this.LogI.Exp();
                    }
                }
                else
                {
                    if (data.AnyGT)
                    {
                        this.GT = RNorm4CensoredMeasures.RNormCensored(mu, sigma, lower: data.GT);
                    }

                    if (data.AnyLT)
                    {
                        this.LT = RNorm4CensoredMeasures.RNormCensored(mu, sigma, upper: data.LT);
                    }

                    if (data.AnyIntervalCensored)
                    {
                        this.I = RNorm4CensoredMeasures.RNormCensored(mu, sigma, lower: data.IntervalGT, upper: data.IntervalLT);
                    }
                }
            }
        }// internal YGen (constructor)
示例#2
0
        public static YGen GetInstance(MeasurementError me, TrueValuesGen genTV, DataSummary data, double mu, double sigma, bool logNormalDistrn = true)
        {
            if (me.Any)
            {
                YGen newYGen = new YGen();
                // Sample y (censored) values | true values
                if (data.AnyGT)
                {
                    double[] tmpMean = genTV.GT;
                    double[] tmpSD;
                    if (me.ThroughCV)
                    {
                        tmpSD = tmpMean.Multiply(me.Parm);
                    }
                    else
                    {
                        tmpSD = Tools.Rep(me.Parm, tmpMean.Length);
                    }

                    newYGen.GT = RNorm4CensoredMeasures.RNormCensored(tmpMean, tmpSD, lower: data.GT);
                    if (logNormalDistrn)
                    {
                        newYGen.LogGT = newYGen.GT.Log();
                    }
                }

                if (data.AnyLT)
                {
                    double[] tmpMean = genTV.LT;
                    double[] tmpSD;
                    if (me.ThroughCV)
                    {
                        tmpSD = tmpMean.Multiply(me.Parm);
                    }
                    else
                    {
                        tmpSD = Tools.Rep(me.Parm, tmpMean.Length);
                    }

                    newYGen.LT = RNorm4CensoredMeasures.RNormCensored(tmpMean, tmpSD, upper: data.LT, negativeDisallowed: logNormalDistrn || me.ThroughCV);
                    if (logNormalDistrn)
                    {
                        newYGen.LogLT = newYGen.LT.Log();
                    }
                }

                if (data.AnyIntervalCensored)
                {
                    double[] tmpMean = genTV.I;
                    double[] tmpSD;
                    if (me.ThroughCV)
                    {
                        tmpSD = tmpMean.Multiply(me.Parm);
                    }
                    else
                    {
                        tmpSD = Tools.Rep(me.Parm, tmpMean.Length);
                    }

                    newYGen.I = RNorm4CensoredMeasures.RNormCensored(tmpMean, tmpSD, lower: data.IntervalGT, upper: data.IntervalLT);
                    if (logNormalDistrn)
                    {
                        newYGen.LogI = newYGen.I.Log();
                    }
                }
                return(newYGen);
            }
            else
            {
                return(YGen.Inits(data, mu, sigma, meThroughCV: data.METhroughCV, logNormalDistrn: logNormalDistrn));
            }
        }
示例#3
0
        }// 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
示例#4
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
示例#5
0
        public static OutLogoutMoments Get(bool anyME, bool logNormalDistrn, DataSummary data, YGen genY, TrueValuesGen genTV)
        {
            OutLogoutMoments olm = new OutLogoutMoments();

            if (anyME)
            {
                double[] y;
                if (logNormalDistrn)
                {
                    y = Tools.Combine(genTV.LogY, genTV.LogGT, genTV.LogLT, genTV.LogI);
                }
                else
                {
                    y = Tools.Combine(genTV.Y, genTV.GT, genTV.LT, genTV.I);
                }

                olm.Sum  = y.Sum();
                olm.Sum2 = y.Sqr().Sum();
                return(olm);
            }
            else if (logNormalDistrn)
            {
                olm.Sum   = data.LogUncensoredSum;
                olm.Sum2  = data.LogUncensoredSum2;
                olm.Sum  += genY.LogGT.Sum() + genY.LogLT.Sum() + genY.LogI.Sum();
                olm.Sum2 += genY.LogGT.Sqr().Sum() + genY.LogLT.Sqr().Sum() + genY.LogI.Sqr().Sum();
            }
            else
            {
                olm.Sum   = data.UncensoredSum;
                olm.Sum2  = data.UncensoredSum2;
                olm.Sum  += genY.GT.Sum() + genY.LT.Sum() + genY.I.Sum();
                olm.Sum2 += genY.GT.Sqr().Sum() + genY.LT.Sqr().Sum() + genY.I.Sqr().Sum();
            }

            return(olm);
        }
示例#6
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);
        }