Exemplo n.º 1
0
        public void TestBlockSelecter(int freqency, int iFrequency)
        {
            BlockSelecter selecter = new BlockSelecter(freqency, iFrequency);

            var sResult = selecter.Sample();

            sResult.Should().NotBeNull();
            selecter.BlockState.Should().NotBeEmpty();
        }
Exemplo n.º 2
0
        public void GenerateBlock(int frequency)
        {
            var block = BlockSelecter.GenerateBlock(frequency);

            block.Should().HaveCount(2 * 5 * frequency);

            var numSamples = block.Where(x => x == true).Count();

            numSamples.Should().Be(10);
        }
Exemplo n.º 3
0
        // run the sampler so that number of samples lines up with
        // block size so there is no margin for error
        public void BlockSelecter_Sample_exact_with_iTrees()
        {
            int freqency   = 15;
            int numSamples = freqency * 2 * 5 * 100;
            int iFrequency = 2;

            BlockSelecter selecter = new BlockSelecter(freqency, iFrequency);

            ValidateBlockSelecter(selecter, freqency, iFrequency, numSamples, 0);
        }
Exemplo n.º 4
0
        public void BlockSelecter_Rehidrate(int freqency, int iFrequency)
        {
            BlockSelecter selecter = new BlockSelecter(freqency, iFrequency);

            var selecterAgian = new BlockSelecter(selecter.Frequency,
                                                  selecter.ITreeFrequency,
                                                  selecter.BlockState,
                                                  selecter.Count,
                                                  selecter.InsuranceIndex,
                                                  selecter.InsuranceCounter);
        }
Exemplo n.º 5
0
        protected void ValidateBlockSelecter(BlockSelecter selecter, int freq, int iFreq, int numSamples, int blockMarginOfError = BLOCK_SAMPLING_MARGIN_OF_ERROR)
        {
            int[] results       = new int[numSamples];
            int   totalSamples  = 0;
            int   totalISamples = 0;

            for (int i = 0; i < numSamples; i++)
            {
                var result = selecter.Sample();

                if (result == SampleResult.M)
                {
                    results[i] = 1;
                    totalSamples++;
                }
                else if (result == SampleResult.I)
                {
                    results[i] = 2;
                    totalISamples++;
                }
                else
                {
                    results[i] = 0;
                }
            }

            selecter.ITreeFrequency.ShouldBeEquivalentTo(iFreq);

            //this.TestContext.WriteLine(" numsamples  = {0}", numSamples.ToString());
            //this.TestContext.WriteLine("total samples  = {0}", totalSamples.ToString());
            //this.TestContext.WriteLine("total Isamples = {0}", totalISamples.ToString());

            decimal observedFreq  = (totalSamples / (decimal)numSamples);
            decimal observediFreq = (totalISamples / (decimal)numSamples);

            var expectedSamples = numSamples / freq;

            // if we are sampleing insurance trees then some regualr samples will get converted
            // into insurance samples, so we need to ajust expected samples to account for insuance trees
            var expectedITrees = (iFreq > 0) ? numSamples / (iFreq * freq) : 0;

            Math.Abs(expectedSamples - totalSamples)
            .Should().BeLessOrEqualTo(blockMarginOfError, $"expected samples:{expectedSamples} actual samples {totalSamples}");     // difference between actual samples and expected should be less than 1, allowing for rounding errors


            if (iFreq > 0)
            {
                var iSampleMarginOfError = Math.Max(blockMarginOfError / iFreq, 1);

                Math.Abs(expectedITrees - totalISamples)
                .Should().BeLessOrEqualTo(iSampleMarginOfError, $"expected i samples:{expectedITrees} actual i samples:{totalISamples}");
            }
        }
        public void GetSamplerBySampleGroupCode_STR_MockedDS(string method)
        {
            var stCode     = "00";
            var sgCode     = "01";
            var freq       = 5;
            var iFreq      = 2;
            var blockState = new String(BlockSelecter.GenerateBlock(freq).Select(x => x ? '-' : 'x').ToArray());

            var sInfo = new SamplerInfo()
            {
                SamplingFrequency  = freq,
                InsuranceFrequency = iFreq,
                SampleGroupCode    = sgCode,
                StratumCode        = stCode,
                Method             = method,
            };

            var sState = new SamplerState()
            {
                SampleGroupCode    = sgCode,
                StratumCode        = stCode,
                SampleSelectorType = nameof(BlockSelecter),
                BlockState         = blockState,
            };

            var mockSids = new Mock <ISamplerInfoDataservice>();

            mockSids.Setup(x => x.GetSamplerInfo(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(sInfo);
            mockSids.Setup(x => x.GetSamplerState(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(sState);

            var ssRepo = new SampleSelectorRepository(mockSids.Object);

            var sampler = ssRepo.GetSamplerBySampleGroupCode(stCode, sgCode);

            sampler.Should().NotBeNull();

            sampler.StratumCode.Should().Be(stCode);
            sampler.SampleGroupCode.Should().Be(sgCode);

            sampler.Should().BeAssignableTo <IFrequencyBasedSelecter>();

            var freqSampler = sampler as IFrequencyBasedSelecter;

            freqSampler.Frequency.Should().Be(freq);
            freqSampler.Sample().Should().NotBeNull();

            var samplerAgain = ssRepo.GetSamplerBySampleGroupCode(stCode, sgCode);

            samplerAgain.Should().BeSameAs(sampler);
        }
Exemplo n.º 7
0
        public ISampleSelector MakeBlockSampleSelector(SamplerInfo samplerInfo)
        {
            if (samplerInfo is null)
            {
                throw new ArgumentNullException(nameof(samplerInfo));
            }

            var state = Dataservice.GetSamplerState(samplerInfo.StratumCode, samplerInfo.SampleGroupCode);

            var freq = samplerInfo.SamplingFrequency;

            if (freq == 0)
            {
                // frequency shouldn't be 0,
                // but I don't want it to break the program if it is, so for now lets just track it
                Analytics.TrackEvent("ZeroFrequencySelecter Created", new Dictionary <string, string> {
                    { "Method", "MakeBlockSampleSelector" },
                });
                return(new ZeroFrequencySelecter(samplerInfo.StratumCode, samplerInfo.SampleGroupCode));
            }

            // we need to gruard against a empty block state
            // if a block selector is initialized with a frequency of 0
            // then the block state will be empty
            if (state != null && string.IsNullOrWhiteSpace(state.BlockState) == false)
            {
                var selector = new BlockSelecter(freq,
                                                 samplerInfo.InsuranceFrequency,
                                                 state.BlockState,
                                                 state.Counter,
                                                 state.InsuranceIndex,
                                                 state.InsuranceCounter);
                selector.StratumCode     = samplerInfo.StratumCode;
                selector.SampleGroupCode = samplerInfo.SampleGroupCode;
                return(selector);
            }
            else
            {
                var selector = new BlockSelecter(freq, samplerInfo.InsuranceFrequency);
                selector.StratumCode     = samplerInfo.StratumCode;
                selector.SampleGroupCode = samplerInfo.SampleGroupCode;
                return(selector);
            }
        }
Exemplo n.º 8
0
 public SampleResult Sample()
 {
     return(BlockSelecter.Sample());
 }
Exemplo n.º 9
0
 public S3PSelector(int freq, int kz, int count, string blockState)
 {
     _blockSelecter  = new BlockSelecter(freq, 0, blockState, count, 0, 0);
     _threePSelecter = new ThreePSelecter(kz, 0);
 }
Exemplo n.º 10
0
 public S3PSelector(int freq, int kz)
 {
     _blockSelecter  = new BlockSelecter(freq, 0);
     _threePSelecter = new ThreePSelecter(kz, 0);
 }
Exemplo n.º 11
0
        public void TestBlockSelector_2(int frequency, int iFrequency)
        {
            var block = new String(BlockSelecter.GenerateBlock(frequency).Select(x => x ? 'x' : '-').ToArray());

            BlockSelecter selecter = new BlockSelecter(frequency, iFrequency, block, 3, 0, 0);
        }
Exemplo n.º 12
0
        public void BlockSelector_Sample_Test(int freq, int iFreq, int numSamples)
        {
            BlockSelecter selecter = new BlockSelecter(freq, iFreq);

            ValidateBlockSelecter(selecter, freq, iFreq, numSamples);
        }