コード例 #1
0
ファイル: MCMC.cs プロジェクト: BULQI/TBKMath-Release
        public void Step()
        {
            lastAuxChain = discreteUniformDist.Next();
            Mother       = mcmc[lastAuxChain].SwapStates(Mother);

            for (int i = 0; i < numAuxiliary; i++)
            {
                if (i == lastAuxChain)
                {
                    continue;                     // skip the swapped chain
                }
                mcmc[i].Step();
            }
        }
コード例 #2
0
ファイル: MCMC.cs プロジェクト: BULQI/TBKMath-Release
        public override MCMCProposal Proposal(double[] theta)
        {
            if (theta.Length != dim)
            {
                throw new ArgumentException("The length of the parameter vector is inconsistent with previous lengths.");
            }
            double[] newTheta = theta.Clone() as double[];
            int      i        = duDist.Next();

            newTheta[i] = theta[i] + sigma[i] * gDist.NextDouble();

            return(new MCMCProposal()
            {
                Theta = newTheta, LogRatio = 0
            });
        }
コード例 #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));
        }