Пример #1
0
 public SimpleSequentialGaussianProposal(double[] sigma)
 {
     this.sigma = sigma;
     gen        = new Troschuetz.Random.Generators.MT19937Generator();
     gDist      = new Troschuetz.Random.Distributions.Continuous.NormalDistribution(gen, 0, 1);
     dim        = sigma.Length;
     duDist     = new Troschuetz.Random.Distributions.Discrete.DiscreteUniformDistribution(gen, 0, dim - 1);
 }
Пример #2
0
        public ParallelHierarchicalSampler(int _total, int _burnIn, int _numSubSpaces, int _dim)
        {
            OutputDirectory = Directory.GetCurrentDirectory();

            numAuxiliary = _numSubSpaces;
            dim          = _dim;
            total        = _total;
            burnIn       = _burnIn;
            mean         = new double[dim];
            if (!SkipCovarianceComputation)
            {
                covariance = new double[dim, dim];
            }
            CI = new double[dim];

            mcmc = new AuxiliaryChain[numAuxiliary];
            for (int i = 0; i < numAuxiliary; i++)
            {
                mcmc[i] = new AuxiliaryChain();
            }

            gen = new Troschuetz.Random.Generators.MT19937Generator();
            discreteUniformDist = new Troschuetz.Random.Distributions.Discrete.DiscreteUniformDistribution(gen, 0, numAuxiliary - 1);
        }
Пример #3
0
        public static Tuple <List <Ball <T> >, bool, int> Cluster2FixedNumberOfBalls(List <T> elements, Metric metric, int nBalls, int maxNumSteps)
        {
            if (elements.Count <= nBalls)
            {
                // nothing to do
                return(new Tuple <List <Ball <T> >, bool, int>(null, false, 0));
            }

            List <Ball <T> > balls = new List <Ball <T> >();

            for (int i = 0; i < nBalls; i++)
            {
                balls.Add(new Ball <T>(metric));
            }
            Troschuetz.Random.Generators.MT19937Generator gen = new Troschuetz.Random.Generators.MT19937Generator();
            Troschuetz.Random.Distributions.Discrete.DiscreteUniformDistribution dud = new Troschuetz.Random.Distributions.Discrete.DiscreteUniformDistribution(gen);
            dud.Alpha = 0;
            dud.Beta  = nBalls - 1;

            // start by assigning elements to balls at random
            Dictionary <T, Ball <T> > assigmnent = new Dictionary <T, Ball <T> >();

            foreach (T element in elements)
            {
                balls[dud.Next()].AddElement(element);
            }

            // check for empty balls
            for (int i = 0; i < nBalls; i++)
            {
                if (balls[i].Elements.Count == 0)
                {
                    // select another ball at random
                    bool done = false;
                    while (!done)
                    {
                        int iBall = dud.Next();
                        if (balls[iBall].Elements.Count > 1)
                        {
                            T elementToMove = balls[iBall].Elements[0];
                            balls[iBall].RemoveElement(elementToMove);
                            balls[i].AddElement(elementToMove);
                            done = true;
                        }
                    }
                }
            }

            foreach (Ball <T> ball in balls)
            {
                foreach (T element in ball.elements)
                {
                    assigmnent.Add(element, ball);
                }
            }

            int  nStepsTaken = maxNumSteps;
            bool converged   = false;

            for (int iStep = 0; iStep < maxNumSteps; iStep++)
            {
                int nChanged = 0;
                foreach (T element in elements)
                {
                    double   smallestDistance = double.MaxValue;
                    Ball <T> nearestBall      = null;
                    foreach (Ball <T> ball in balls)
                    {
                        double dist = metric(element, ball.Center);
                        if (dist < smallestDistance)
                        {
                            smallestDistance = dist;
                            nearestBall      = ball;
                        }
                    }
                    if (nearestBall.Elements.Contains(element))
                    {
                        continue;
                    }

                    nearestBall.AddElement(element);
                    assigmnent[element].RemoveElement(element);
                    assigmnent.Remove(element);
                    assigmnent.Add(element, nearestBall);
                    nChanged++;
                }
                if (nChanged == 0)
                {
                    nStepsTaken = iStep;
                    converged   = true;
                    break;
                }
            }
            return(new Tuple <List <Ball <T> >, bool, int>(balls, converged, nStepsTaken));
        }
Пример #4
0
        public static Tuple <List <Ball <T> >, bool, int> ClusterFixedNumberOfBalls(List <T> elements, Metric metric, int nBalls, int maxNumSteps)
        {
            if (elements.Count <= nBalls)
            {
                // nothing to do
                return(new Tuple <List <Ball <T> >, bool, int>(null, false, 0));
            }

            List <Ball <T> > balls = new List <Ball <T> >();

            for (int i = 0; i < nBalls; i++)
            {
                balls.Add(new Ball <T>(metric));
            }
            Troschuetz.Random.Generators.MT19937Generator gen = new Troschuetz.Random.Generators.MT19937Generator();
            Troschuetz.Random.Distributions.Discrete.DiscreteUniformDistribution dud = new Troschuetz.Random.Distributions.Discrete.DiscreteUniformDistribution(gen);
            dud.Alpha = 0;
            dud.Beta  = nBalls - 1;

            // start by assigning elements to balls at random
            Dictionary <T, Ball <T> > assigmnent = new Dictionary <T, Ball <T> >();

            foreach (T element in elements)
            {
                balls[dud.Next()].AddElement(element);
            }

            // check for empty balls
            for (int i = 0; i < nBalls; i++)
            {
                if (balls[i].Elements.Count == 0)
                {
                    // select another ball at random
                    bool done = false;
                    while (!done)
                    {
                        int iBall = dud.Next();
                        if (balls[iBall].Elements.Count > 1)
                        {
                            T elementToMove = balls[iBall].Elements[0];
                            balls[iBall].RemoveElement(elementToMove);
                            balls[i].AddElement(elementToMove);
                            done = true;
                        }
                    }
                }
            }

            foreach (Ball <T> ball in balls)
            {
                foreach (T element in ball.elements)
                {
                    assigmnent.Add(element, ball);
                }
            }

            Dictionary <T, double> distanceToCenter = new Dictionary <T, double>();
            double   greatestRadius         = double.MinValue;
            Ball <T> ballWithGreatestRadius = null;

            foreach (Ball <T> ball in balls)
            {
                if (ball.radius > greatestRadius)
                {
                    greatestRadius         = ball.radius;
                    ballWithGreatestRadius = ball;
                }
            }

            int  nStepsTaken = maxNumSteps;
            bool converged   = false;

            for (int iStep = 0; iStep < maxNumSteps; iStep++)
            {
                T        outlier          = ballWithGreatestRadius.MostDistantElement;
                double   smallestDistance = double.MaxValue;
                Ball <T> nearestBall      = null;

                foreach (Ball <T> ball in balls)
                {
                    double dist = metric(outlier, ball.Center);
                    if (dist < smallestDistance)
                    {
                        smallestDistance = dist;
                        nearestBall      = ball;
                    }
                }

                // if the closest center is closer than current center, move it. Else end.
                if (smallestDistance < ballWithGreatestRadius.radius)
                {
                    // move outlier
                    ballWithGreatestRadius.RemoveElement(outlier);
                    nearestBall.AddElement(outlier);
                }
                else
                {
                    converged   = true;
                    nStepsTaken = iStep;
                    break;
                }
                // find the next outlier
                greatestRadius = double.MinValue;
                foreach (Ball <T> ball in balls)
                {
                    if (ball.radius > greatestRadius)
                    {
                        greatestRadius         = ball.radius;
                        ballWithGreatestRadius = ball;
                    }
                }
            }
            return(new Tuple <List <Ball <T> >, bool, int>(balls, converged, nStepsTaken));
        }