Esempio n. 1
0
        internal void SoftmaxGWAS()
        {
            var v  = Vector.Zero(1);
            var x1 = Variable.VectorGaussianFromMeanAndPrecision(Vector.Zero(1), PositiveDefiniteMatrix.Identity(1));

            x1.ObservedValue = v;
            var x2 = Variable.VectorGaussianFromMeanAndPrecision(Vector.Zero(1), PositiveDefiniteMatrix.Identity(1));

            v[0]             = 1;
            x2.ObservedValue = v;
            Console.WriteLine(x1.ObservedValue);
            Console.WriteLine(x2.ObservedValue);

            int numExposures = 10;
            int numClasses   = 5;
            int numSamples   = 1000;
            var c            = new Range(numClasses);
            var n            = new Range(numSamples);
            // model
            var Bexposures = Variable.Array <Vector>(c);

            Bexposures[c] = Variable.VectorGaussianFromMeanAndPrecision(
                Vector.Zero(numExposures),
                PositiveDefiniteMatrix.Identity(numExposures))
                            .ForEach(c);
            var exposures = Variable.Array <Vector>(n);
            var Bgenotype = Variable.Array <double>(c);

            Bgenotype[c] = Variable.GaussianFromMeanAndPrecision(0, 1).ForEach(c);
            var genotypes = Variable.Array <double>(n);
            var mean      = Variable.Array <double>(c);

            mean[c] = Variable.GaussianFromMeanAndPrecision(0, 1).ForEach(c);
            var g = Variable.Array(Variable.Array <double>(c), n);

            g[n][c] = Bgenotype[c] * genotypes[n] + Variable.InnerProduct(Bexposures[c], exposures[n]) + mean[c];
            var classProbs = Variable.Array <Vector>(n);

            classProbs[n] = Variable.Softmax(g[n]);
            var classAssignments = Variable.Array <int>(n);

            classAssignments[n] = Variable.Discrete(classProbs[n]);
            // generate data
            var exposuresObs   = new Vector[numSamples];
            var genotypeObs    = new double[numSamples];
            var classObs       = new int[numSamples];
            var trueBexposures = new Vector[numClasses];
            var trueBgenotype  = new double[numClasses];

            Rand.Restart(123);
            for (int i = 0; i < numClasses; i++)
            {
                trueBexposures[i] = VectorGaussian.SampleFromMeanAndVariance(Vector.Zero(numExposures), PositiveDefiniteMatrix.Identity(numExposures));
                trueBgenotype[i]  = Rand.Normal();
            }
            for (int i = 0; i < numSamples; i++)
            {
                exposuresObs[i] = VectorGaussian.SampleFromMeanAndVariance(Vector.Zero(numExposures), PositiveDefiniteMatrix.Identity(numExposures));
                genotypeObs[i]  = Rand.Double() < .5 ? 0.0 : 1.0;
                var gt = new double[numClasses];
                for (int j = 0; j < numClasses; j++)
                {
                    gt[j] = genotypeObs[i] * trueBgenotype[j] + exposuresObs[i].Inner(trueBexposures[j]);
                }
                var p = MMath.Softmax(gt);
                classObs[i] = Discrete.Sample(p);
            }
            Rand.Restart(DateTime.Now.Millisecond);
            exposures.ObservedValue        = exposuresObs;
            genotypes.ObservedValue        = genotypeObs;
            classAssignments.ObservedValue = classObs;
            // inference
            var ie    = new InferenceEngine(new VariationalMessagePassing());
            var bPost = ie.Infer <Gaussian[]>(Bgenotype);

            for (int i = 0; i < numClasses; i++)
            {
                Console.WriteLine("Inferred: " + bPost[i] + " truth: " + trueBgenotype[i]);
            }
        }