示例#1
0
        public void ParallelNSampler()
        {
            var prior = from a in Normal(0, 100)
                        from b in Normal(0, 100)
                        select new Param(a, b);

            var smc = LinearRegression.CreateLinearRegression(prior, LinearRegression.BeachSandData).SmcStandard(5000);

            var sTimer = new Stopwatch();

            sTimer.Start();
            var serial = from s1 in smc
                         from s2 in smc
                         from s3 in smc
                         select new Tuple <Samples <Param>, Samples <Param>, Samples <Param> >(s1, s2, s3);
            var result = serial.SampleN(2).ToList();

            sTimer.Stop();

            var pTimer = new Stopwatch();

            pTimer.Start();
            var parallel = from s1 in Independent(smc)
                           from s2 in Independent(smc)
                           from s3 in Independent(smc)
                           from triple in RunIndependent(s1, s2, s3)
                           select triple;
            var thing = parallel.SampleNParallel(2).ToList();

            pTimer.Stop();


            Trace.WriteLine($"parallel: {pTimer.ElapsedMilliseconds}ms, serial: {sTimer.ElapsedMilliseconds}ms");
            Assert.IsTrue(pTimer.ElapsedMilliseconds < sTimer.ElapsedMilliseconds);
        }
        public void Metropolis_LinReg()
        {
            // Define a prior with heavy tails
            var prior = from a in Normal(0, 100)
                        from b in Normal(0, 100)
                        select new Param(a, b);

            var linReg  = LinearRegression.CreateLinearRegression(prior, LinearRegression.BeachSandData);
            var mh      = MetropolisHastings.MHPrior(linReg, 1000);
            var samples = mh.SampleNParallel(100);

            var paramA = from outer in samples
                         from inner in outer
                         select inner.a;

            var paramB = from outer in samples
                         from inner in outer
                         select inner.b;

            Debug.WriteLine(Histogram.Unweighted(paramA));
            Debug.WriteLine(Histogram.Unweighted(paramB));
        }
示例#3
0
        public void LinReg()
        {
            // Define a prior with heavy tails
            var prior = from a in Normal(5, 10)
                        from b in Normal(1, 10)
                        select new Param(a, b);

            // Create the linear regression, but don't do any inference yet
            var linReg = LinearRegression.CreateLinearRegression(prior, LinearRegression.LinearData);

            // Basically do importance sampling using the prior
            var samples = linReg.WeightedPrior().SampleNParallel(1000);

            var posteriorA = samples.Select(sample => ItemProb(sample.Item.a, sample.Prob));
            var posteriorB = samples.Select(sample => ItemProb(sample.Item.b, sample.Prob));

            // Graph the results
            Debug.WriteLine("Posterior distribution of a:");
            Debug.WriteLine(Histogram.Weighted(posteriorA, 20, 2));

            Debug.WriteLine("Posterior distribution of b:");
            Debug.WriteLine(Histogram.Weighted(posteriorB, 20, 2));
        }