Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13025 for trunk


Ignore:
Timestamp:
10/16/15 14:37:31 (9 years ago)
Author:
gkronber
Message:

#2488 implemented quantile extension method in HeuristicLab.Common

Location:
trunk/sources
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Common/3.3/EnumerableStatisticExtensions.cs

    r12012 r13025  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Diagnostics.Contracts;
    2425using System.Linq;
    2526
     
    4445      } else {
    4546        return (valuesArr[(n / 2) - 1] + valuesArr[n / 2]) / 2.0;
     47      }
     48    }
     49
     50    /// <summary>
     51    /// Calculates the alpha-quantile element of the enumeration.
     52    /// </summary>
     53    /// <param name="values"></param>
     54    /// <returns></returns>
     55    public static double Quantile(this IEnumerable<double> values, double alpha) {
     56      Contract.Assert(alpha > 0 && alpha < 1);
     57      // iterate only once
     58      double[] valuesArr = values.ToArray();
     59      int n = valuesArr.Length;
     60      if (n == 0) throw new InvalidOperationException("Enumeration contains no elements.");
     61
     62      Array.Sort(valuesArr);
     63      //  starts at 0
     64
     65      // return the element at Math.Ceiling (if n*alpha is fractional) or the average of two elements if n*alpha is integer.
     66      var pos = n * alpha;
     67      Contract.Assert(pos >= 0);
     68      Contract.Assert(pos < n);
     69      bool isInteger = Math.Round(pos).IsAlmost(pos);
     70      if (isInteger) {
     71        return 0.5 * (valuesArr[(int)pos - 1] + valuesArr[(int)pos]);
     72      } else {
     73        return valuesArr[(int)Math.Ceiling(pos) - 1];
    4674      }
    4775    }
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Tests.csproj

    r12817 r13025  
    468468    <Compile Include="HeuristicLab.Collections-3.3\BidirectionalLookupTest.cs" />
    469469    <Compile Include="HeuristicLab.Collections-3.3\ObservableKeyedListTest.cs" />
     470    <Compile Include="HeuristicLab.Common-3.3\EnumerableStatisticExtensions.cs" />
    470471    <Compile Include="HeuristicLab.Encodings.BinaryVectorEncoding-3.3\Auxiliary.cs" />
    471472    <Compile Include="HeuristicLab.Encodings.BinaryVectorEncoding-3.3\NPointCrossoverTest.cs" />
     
    536537    <Compile Include="HeuristicLab.Persistence-3.3\StorableAttributeTests.cs" />
    537538    <Compile Include="HeuristicLab.Persistence-3.3\UseCases.cs" />
    538     <Compile Include="HeuristicLab.PluginInfraStructure-3.3\InstallationManagerTest.cs" />
    539     <Compile Include="HeuristicLab.PluginInfraStructure-3.3\TypeExtensionsTest.cs" />
    540539    <Compile Include="HeuristicLab.Problems.DataAnalysis-3.4\ThresholdCalculatorsTest.cs" />
    541540    <Compile Include="HeuristicLab.Problems.DataAnalysis-3.4\OnlineCalculatorPerformanceTest.cs" />
     
    629628    </None>
    630629    <Shadow Include="Test References\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.accessor" />
    631     <Shadow Include="Test References\HeuristicLab.PluginInfrastructure-3.3.accessor" />
    632630    <Shadow Include="Test References\HeuristicLab.MainForm.WindowsForms-3.3.accessor" />
    633631    <Shadow Include="Test References\HeuristicLab.Encodings.IntegerVectorEncoding-3.3.accessor" />
Note: See TracChangeset for help on using the changeset viewer.