Unusual numerical errors

I implement a simple two-particle particle filter. If you don’t know what a particle filter is, that’s for sure - the short version is that I need to calculate weighted means with weights between 0 and 1 and values ​​between 0 and 1. Each particle has a value and weight.

C # gives me completely bizarre numerical problems though.

When trying to debug this code, it looks like this:

            ConcurrentBag<Particle> particles; //this is inputted as an argument to my function
            double mean = 0.0;
            double totalWeight = 0.0;
            foreach (Particle p in particles)
            {
                mean += p.Value * p.Weight;
                totalWeight += p.Weight;

                if (p.Value > 1.01 || p.Weight > 1.01)
                {
                    Console.WriteLine("Value " + p.Value);
                    Console.WriteLine("Weight " + p.Weight);
                    Console.WriteLine("wtf");
                }
            }

            if (totalWeight == 0.0)
            {
                //in this case, everything has miniscule weight, so let just return 0.0 to avoid this precision corner case.
                return new Bernoulli(0.0);
            }
            double oldMean = mean;
            mean /= totalWeight;
            return mean;

This if statement with "wtf" exists for debugging purposes and runs. But listing:

Value 1.0 Weight 0.01

This if statement should not be true at all! What's happening?

Edit: A small update for debugging. This is my current whole function:

public override IDistribution createDistribution(ConcurrentBag<Particle> particles)
        {
            if (particles.Count == 0)
            {
                throw new Exception("Cannot create Distribution from empty particle collection");
            }
            if (!particles.ToArray()[0].GetType().IsAssignableFrom(typeof(BinaryParticle)))
            {
                throw new Exception("Cannot create Bernoulli Distribution from non-Binary Particle");
            }

            decimal mean = 0.0m;
            decimal totalWeight = 0.0m;
            foreach (Particle p in particles)
            {
                mean += (decimal)(p.Value * p.Weight);
                totalWeight += (decimal)p.Weight;


                    if ((p.Weight > 1.01))
                    {
                        {
                            Console.WriteLine("Value " + p.Value);
                            Console.WriteLine("Weight " + p.Weight);
                            Console.WriteLine("Value " + p.Value.ToString("0.0000000"));
                            Console.WriteLine("wtf");
                        }
                    }


            if (totalWeight == 0.0m)
            {
                //in this case, everything has miniscule weight, so let just return 0.0 to avoid this precision corner case.
                return new Bernoulli(0.0);
            }
            decimal oldMean = mean;
            mean /= totalWeight;

            try
            {
                return new Bernoulli((double)mean);
            }
            catch (Exception e)
            {
                decimal testMean = 0.0m;
                decimal testTotalWeight = 0.0m;
                Console.WriteLine(e);
                foreach (Particle p in particles)
                {
                    testMean += (decimal)(p.Value * p.Weight);
                    testTotalWeight += (decimal)p.Weight;
                    Console.WriteLine("weight is " + p.Weight);
                    Console.WriteLine("value is " + p.Value);
                    Console.WriteLine("Total mean is " + testMean);
                    Console.WriteLine("Total weight is " + testTotalWeight);
                }


                Console.WriteLine(testMean / testTotalWeight);
                throw new Exception();
            }
        }

"mean" , catch. . , , s > 1,01, , 0,01.

+4
1

, , , , , : -)

, , #. , , . .

^ _ ^;;

+2

Source: https://habr.com/ru/post/1599136/


All Articles