Пример #1
0
        /** <summary>Compute the sample (unbiased estimator) standard deviation following:</summary>
         *
         *  <remarks>
         *  Computing Deviations: Standard Accuracy
         *  Tony F. Chan and John Gregg Lewis
         *  Stanford University
         *  Communications of ACM September 1979 of Volume 22 the ACM Number 9
         *
         *  The "two-pass" method from the paper; supposed to have better
         *  numerical properties than the textbook summation/sqrt.  To me
         *  this looks like the textbook method, but I ain't no numerical
         *  methods guy.
         *  </remarks>
         */
        public static double Stddev(int[] X)
        {
            int m = X.Length;

            if (m <= 1)
            {
                return(0);
            }
            double xbar = EnumerableExtensions.Average(X);
            double s2   = 0.0;

            for (int i = 0; i < m; i++)
            {
                s2 += (X[i] - xbar) * (X[i] - xbar);
            }
            s2 = s2 / (m - 1);
            return(Math.Sqrt(s2));
        }
                public void ShouldReturnValidResult()
                {
                    // arrange
                    var source = Fixture.CreateMany <Length>(3).Select(e => new TestObject <Length>(e));
                    Func <TestObject <Length>, Length> selector = e => e.Property;

                    number expectedResultInMetres = source.Average(e => e.Property.Metres);
                    var    expectedResultUnit     = source.First().Property.Unit;
                    var    expectedResult         = new Length(expectedResultInMetres).Convert(expectedResultUnit);

                    // act
                    var result = EnumerableExtensions.Average(source, selector);

                    // assert
                    result.Metres.Should().BeApproximately(expectedResult.Metres);
                    result.Unit.Should().Be(expectedResult.Unit);
                    result.Value.Should().BeApproximately(expectedResult.Value);
                }
Пример #3
0
            public void ShouldReturnValidResult()
            {
                // arrange
                var source = Fixture.CreateMany <SomeQuantity>(3).Select(e => new TestObject <SomeQuantity>(e));
                Func <number, SomeUnit, SomeQuantity>          factory  = SomeQuantityFactory.Create;
                Func <TestObject <SomeQuantity>, SomeQuantity> selector = e => e.Property;

                number expectedResultInWatts = source.Average(e => e.Property.Value * e.Property.Unit.ValueInUnits);
                var    expectedResultUnit    = source.First().Property.Unit;
                var    expectedResult        = new SomeQuantity(expectedResultInWatts / expectedResultUnit.ValueInUnits, expectedResultUnit);

                // act
                var result = EnumerableExtensions.Average <TestObject <SomeQuantity>, SomeQuantity, SomeUnit>(source, factory, selector);

                // assert
                result.Value.Should().BeApproximately(expectedResult.Value);
                result.Unit.Should().Be(expectedResult.Unit);
            }
Пример #4
0
 public static double avg(int[] X)
 {
     return(EnumerableExtensions.Average(EnumerableExtensions.DefaultIfEmpty(X, 0)));
 }
Пример #5
0
        public virtual string ToNotifyString()
        {
            ITokenStream input = _parser.TokenStream;

            for (int i = 0; i < input.Count && _lastTokenConsumed != null && i <= _lastTokenConsumed.TokenIndex; i++)
            {
                IToken t = input.Get(i);
                if (t.Channel != TokenChannels.Default)
                {
                    numHiddenTokens++;
                    numHiddenCharsMatched += t.Text.Length;
                }
            }
            numCharsMatched             = _lastTokenConsumed.StopIndex + 1;
            decisionMaxFixedLookaheads  = Trim(decisionMaxFixedLookaheads, numFixedDecisions);
            decisionMaxCyclicLookaheads = Trim(decisionMaxCyclicLookaheads, numCyclicDecisions);
            StringBuilder buf = new StringBuilder();

            buf.Append(Version);
            buf.Append('\t');
            buf.Append(_parser.GetType().Name);
            buf.Append('\t');
            buf.Append(numRuleInvocations);
            buf.Append('\t');
            buf.Append(maxRuleInvocationDepth);
            buf.Append('\t');
            buf.Append(numFixedDecisions);
            buf.Append('\t');
            buf.Append(EnumerableExtensions.Min(EnumerableExtensions.DefaultIfEmpty(decisionMaxFixedLookaheads, int.MaxValue)));
            buf.Append('\t');
            buf.Append(EnumerableExtensions.Max(EnumerableExtensions.DefaultIfEmpty(decisionMaxFixedLookaheads, int.MinValue)));
            buf.Append('\t');
            buf.Append(EnumerableExtensions.Average(EnumerableExtensions.DefaultIfEmpty(decisionMaxFixedLookaheads, 0)));
            buf.Append('\t');
            buf.Append(Stats.Stddev(decisionMaxFixedLookaheads));
            buf.Append('\t');
            buf.Append(numCyclicDecisions);
            buf.Append('\t');
            buf.Append(EnumerableExtensions.Min(EnumerableExtensions.DefaultIfEmpty(decisionMaxCyclicLookaheads, int.MaxValue)));
            buf.Append('\t');
            buf.Append(EnumerableExtensions.Max(EnumerableExtensions.DefaultIfEmpty(decisionMaxCyclicLookaheads, int.MinValue)));
            buf.Append('\t');
            buf.Append(EnumerableExtensions.Average(EnumerableExtensions.DefaultIfEmpty(decisionMaxCyclicLookaheads, 0)));
            buf.Append('\t');
            buf.Append(Stats.Stddev(decisionMaxCyclicLookaheads));
            buf.Append('\t');
            buf.Append(numBacktrackDecisions);
            buf.Append('\t');
            buf.Append(EnumerableExtensions.Min(EnumerableExtensions.DefaultIfEmpty(decisionMaxSynPredLookaheads, int.MaxValue)));
            buf.Append('\t');
            buf.Append(EnumerableExtensions.Max(EnumerableExtensions.DefaultIfEmpty(decisionMaxSynPredLookaheads, int.MinValue)));
            buf.Append('\t');
            buf.Append(EnumerableExtensions.Average(EnumerableExtensions.DefaultIfEmpty(decisionMaxSynPredLookaheads, 0)));
            buf.Append('\t');
            buf.Append(Stats.Stddev(decisionMaxSynPredLookaheads));
            buf.Append('\t');
            buf.Append(numSemanticPredicates);
            buf.Append('\t');
            buf.Append(_parser.TokenStream.Count);
            buf.Append('\t');
            buf.Append(numHiddenTokens);
            buf.Append('\t');
            buf.Append(numCharsMatched);
            buf.Append('\t');
            buf.Append(numHiddenCharsMatched);
            buf.Append('\t');
            buf.Append(numberReportedErrors);
            buf.Append('\t');
            buf.Append(numMemoizationCacheHits);
            buf.Append('\t');
            buf.Append(numMemoizationCacheMisses);
            buf.Append('\t');
            buf.Append(numGuessingRuleInvocations);
            buf.Append('\t');
            buf.Append(numMemoizationCacheEntries);
            return(buf.ToString());
        }