コード例 #1
0
        public DynamicBin1D SampleBootstrap(DynamicBin1D other, int resamples, Cern.Jet.Random.Engine.RandomEngine randomGenerator, BinBinFunction1D function)
        {
            if (randomGenerator == null)
            {
                randomGenerator = Cern.Jet.Random.Uniform.MakeDefaultGenerator();
            }

            // since "resamples" can be quite large, we care about performance and memory
            int MaxCapacity = 1000;
            int s1          = Size;
            int s2          = other.Size;

            // prepare auxiliary bins and buffers
            DynamicBin1D sample1 = new DynamicBin1D();

            Cern.Colt.Buffer.DoubleBuffer buffer1 = sample1.Buffered(System.Math.Min(MaxCapacity, s1));

            DynamicBin1D sample2 = new DynamicBin1D();

            Cern.Colt.Buffer.DoubleBuffer buffer2 = sample2.Buffered(System.Math.Min(MaxCapacity, s2));

            DynamicBin1D bootstrap = new DynamicBin1D();

            Cern.Colt.Buffer.DoubleBuffer bootBuffer = bootstrap.Buffered(System.Math.Min(MaxCapacity, resamples));

            // resampling steps
            for (int i = resamples; --i >= 0;)
            {
                sample1.Clear();
                sample2.Clear();

                this.Sample(s1, true, randomGenerator, buffer1);
                other.Sample(s2, true, randomGenerator, buffer2);

                bootBuffer.Add(function(sample1, sample2));
            }
            bootBuffer.Flush();
            return(bootstrap);
        }
コード例 #2
0
        public void Sample(int n, Boolean withReplacement, RandomEngine randomGenerator, Cern.Colt.Buffer.DoubleBuffer buffer)
        {
            if (randomGenerator == null)
            {
                randomGenerator = Cern.Jet.Random.Uniform.MakeDefaultGenerator();
            }
            buffer.Clear();

            if (!withReplacement)
            { // without
                if (n > Size)
                {
                    throw new ArgumentException("n must be less than or equal to Size()");
                }
                Cern.Jet.Random.Sampling.RandomSamplingAssistant sampler = new Cern.Jet.Random.Sampling.RandomSamplingAssistant(n, Size, randomGenerator);
                for (int i = n; --i >= 0;)
                {
                    if (sampler.SampleNextElement())
                    {
                        buffer.Add(this.Elements[i]);
                    }
                }
            }
            else
            { // with
                Cern.Jet.Random.Uniform uniform = new Cern.Jet.Random.Uniform(randomGenerator);
                int s = Size;
                for (int i = n; --i >= 0;)
                {
                    buffer.Add(this.Elements[uniform.NextIntFromTo(0, s - 1)]);
                }
                buffer.Flush();
            }
        }