/// <summary>
        /// A-R example
        /// S Chib and E Greenberg, Understanding the Metropolis-Hastings Algorithm, The American Statistician, Vol. 49, No. 4. (Nov., 1995), pp. 327-335
        /// samples are tesitfied by SigmaPlot
        /// </summary>
        public static void Example()
        {
            int N = 6000;
            int n0 = 0;
            string filename1 = "v1.txt";
            FileStream fs1 = new FileStream(filename1, FileMode.Create, FileAccess.Write);
            StreamWriter w1 = new StreamWriter(fs1);

            // target distribution
            //double[,] S = {{1, 0.9},{0.9, 1}};
            double[,] S = { { 2, 0 }, { 0, 1 } };
            double[] m = { 1, 2 };
            PZMath_matrix targetSigma = new PZMath_matrix(S);
            PZMath_vector targetMu = new PZMath_vector(m);

            // instrumental distribution
            double[,] D = {{2, 0},{0, 1}};
            PZMath_matrix instrumentalSigma = new PZMath_matrix(D);
            PZMath_vector instrumentalMu = new PZMath_vector(m);

            double c = System.Math.Sqrt(System.Math.Abs(instrumentalSigma.LUDet()) / System.Math.Abs(targetSigma.LUDet()));

            // prepare workspace
            AcceptRejectionSampler ARSample = new AcceptRejectionSampler(c, N);

            // prepare instrumental distribution
            InstrumentalDistribution instrumentalDistribution = new InstrumentalDistribution(instrumentalMu, instrumentalSigma);

            // prepare target distribution
            TargetDistribution targetDistribution = new TargetDistribution();
            targetDistribution.TargetDistributionFunction = new targetDistribution(AcceptRejectionSampler.ExampleTargetDistributionFunction);

            // assign target distribution and instrumental distribution to the work space
            ARSample.ARSInstrumentalDistribution = instrumentalDistribution;
            ARSample.ARSTargetDistribution = targetDistribution;

            // A-R sample
            List<double[]> samples = ARSample.Sample();

            for (int i = 0; i < N - n0; i++)
            {
                w1.Write(samples[i][0]);
                w1.Write("          ");
                w1.WriteLine(samples[i][1]);
            }

            w1.Close();
            fs1.Close();
        }
示例#2
0
        /// <summary>
        /// M-H example
        /// S Chib and E Greenberg, Understanding the Metropolis-Hastings Algorithm, The American Statistician, Vol. 49, No. 4. (Nov., 1995), pp. 327-335
        /// 1. random walk - uniform
        /// 2. random walk - normal
        /// 3. 1st order autogressive - uniform (change MH.Sample() samle method)
        /// samples are tesitfied by SigmaPlot
        /// </summary>
        public static void Example()
        {
            int N = 10000;
            int n0 = 1000;

            // file IO
            string filename1 = "v1.txt";
            FileStream fs1 = new FileStream(filename1, FileMode.Create, FileAccess.Write);
            StreamWriter w1 = new StreamWriter(fs1);

            // target distribution
            double lamda = 20;
            //double r = 0.3;
            double width = 1.0;
            double height = 1.0;
            //double theta1 = 2;
            //double theta2 = -1.0;

            // prepare workspace
            GeyerMollerSampler GMSampler = new GeyerMollerSampler(N, n0, lamda, width, height);

            // prepare target distribution
            TargetDistribution targetDistribution = new TargetDistribution();
            targetDistribution.TargetPointProcessDistributionFunction = new targetPointProcessDistribution(GeyerMollerSampler.ExampleTargetDistributionFunction);

            // assign target distribution and instrumental distribution to the work space
            GMSampler.GMTargetDistribution = targetDistribution;

            // M-H sample
            GMSampler.Init();

            List<List<PZPoint>> samples = GMSampler.Sample();

            // output samples
            int sampleLength = samples.Count;
            for (int i = 0; i < sampleLength; i++)
            {
                int xLength = samples[i].Count;
                for (int j = 0; j < xLength; j++)
                {
                    w1.Write(String.Format("{0:f}",samples[i][j].x) + "  " + String.Format("{0:f}",samples[i][j].y) + " | ");
                }
                w1.WriteLine();
            }

            w1.Close();
            fs1.Close();
        }
        /// <summary>
        /// M-H example
        /// S Chib and E Greenberg, Understanding the Metropolis-Hastings Algorithm, The American Statistician, Vol. 49, No. 4. (Nov., 1995), pp. 327-335
        /// 1. random walk - uniform
        /// 2. random walk - normal
        /// 3. 1st order autogressive - uniform (change MH.Sample() samle method)
        /// samples are tesitfied by SigmaPlot
        /// </summary>
        public static void Example()
        {
            int N = 10000;
            int n0 = 1000;

            string filename1 = "v1.txt";
            FileStream fs1 = new FileStream(filename1, FileMode.Create, FileAccess.Write);
            StreamWriter w1 = new StreamWriter(fs1);

            // target distribution
            //double[,] S = {{1, 0.9},{0.9, 1}};
            double[,] S = { { 2, 0 }, { 0, 1 } };
            double[] m = { 1, 2 };
            PZMath_matrix targetSigma = new PZMath_matrix(S);
            PZMath_vector targetMu = new PZMath_vector(m);

            // candidate generating distribution
            // normal
            double[,] D = {{2.5, 0},{0, 1.5}};
            double[] mm = { 0, 0 };
            PZMath_matrix cgdSigma = new PZMath_matrix(D);
            PZMath_vector cgdMu = new PZMath_vector(mm);
            // uniform
            double[] d = { 0.75, 1 };     // for random walk
            //double[] d = { 1, 1 };      // for 1st order autogressive

            // prepare workspace
            MetropolisHastingsSampler MHSample = new MetropolisHastingsSampler(N, n0);

            // prepare instrumental distribution
            // multivariate normal candidate generating distribution
            //CandidateGeneratingDistribution cgDistribution = new CandidateGeneratingDistribution(cgdMu, cgdSigma);
            // multivariate-t candidate generating distribution
            CandidateGeneratingDistribution cgDistribution = new CandidateGeneratingDistribution(d);

            // prepare target distribution
            TargetDistribution targetDistribution = new TargetDistribution();
            targetDistribution.TargetDistributionFunction = new targetDistribution(MetropolisHastingsSampler.ExampleTargetDistributionFunction);

            // assign target distribution and instrumental distribution to the work space
            MHSample.MHCandidateGeneratingDistribution = cgDistribution;
            MHSample.MHTargetDistribution = targetDistribution;

            // M-H sample
            double[] initX = { 1, 2 };
            MHSample.Init(initX);
            List<double[]> samples = MHSample.Sample();

            for (int i = 0; i < N - n0; i++)
            {
                w1.Write(samples[i][0]);
                w1.Write("          ");
                w1.WriteLine(samples[i][1]);
            }

            w1.Close();
            fs1.Close();
        }