コード例 #1
0
ファイル: PZMath_matrix.cs プロジェクト: Josep1984/pzmath
 /// <summary>
 /// LU Det example
 /// </summary>
 public static void LUDetExample()
 {
     double[,] m = {{2, 3, 3, 5},
                    {6, 6, 8, 9},
                    {10, 11, 12, 13},
                    {14, 15, 17, 17}};
     PZMath_matrix matrix = new PZMath_matrix(m);
     double det = matrix.LUDet();
     // correct answer:
     // det(m) = -12
     System.Console.WriteLine(det);
 }
コード例 #2
0
        /// <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();
        }