[Quantproject-developers] QuantProject/b1_ADT/Statistics BasicFunctions.cs,1.4,1.5
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2005-08-21 10:09:33
|
Update of /cvsroot/quantproject/QuantProject/b1_ADT/Statistics In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25089/b1_ADT/Statistics Modified Files: BasicFunctions.cs Log Message: Added statistic methods for getting Percentile, Median and DecileAverage (even though these methods are not used by the project, at the moment). Index: BasicFunctions.cs =================================================================== RCS file: /cvsroot/quantproject/QuantProject/b1_ADT/Statistics/BasicFunctions.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** BasicFunctions.cs 22 Aug 2004 16:52:40 -0000 1.4 --- BasicFunctions.cs 21 Aug 2005 10:09:23 -0000 1.5 *************** *** 22,25 **** --- 22,26 ---- using System; + using QuantProject.ADT.Statistics; namespace QuantProject.ADT.Statistics *************** *** 30,34 **** public class BasicFunctions { ! static public double Sum( double[] data ) { double sum = 0; --- 31,44 ---- public class BasicFunctions { ! private static double[] orderedData; ! ! static private void setOrderedData(double[] data) ! { ! orderedData = new double[data.Length]; ! data.CopyTo(orderedData,0); ! Array.Sort(orderedData); ! } ! ! static public double Sum( double[] data ) { double sum = 0; *************** *** 185,190 **** throw new Exception("The two variables haven't the same length!"); } } } ! \ No newline at end of file --- 195,276 ---- throw new Exception("The two variables haven't the same length!"); } + + /// <summary> + /// Returns the t-th percentile for a given set of data + /// </summary> + /// <param name="data">Data for which t-th percentile is to be computed </param> + /// <param name="percentileNumber">A number between 0 and 100, corresponding + /// to the requested t-th percentile </param> + static public double Percentile(double[] data, + int percentileNumber) + { + if(percentileNumber<0 || percentileNumber >100) + throw new Exception("percentileNumber has to be a number between 0 and 100!"); + setOrderedData(data); + int n = orderedData.Length; + double np = ((double)n+1) * (double)percentileNumber/100.0; + if(np<=1.0) + return orderedData[0]; + if(np>=n) + return orderedData[n-1]; + + int integerPart_np = (int)Math.Floor(np); + double fractionalPart_np = np - integerPart_np; + //if np is not a whole number, it has to be returned a + // number between two data points + // In this implementation the method used to return such number + // is based on a simple interpolation + return (orderedData[integerPart_np-1] + + fractionalPart_np* (orderedData[integerPart_np] - orderedData[integerPart_np-1])); + + } + + /// <summary> + /// Returns the median for a given set of data + /// (Median is the value, belonging or not to the given set of data, such that + /// number of data points below that value is equal to the number of data points + /// above that value) + /// </summary> + /// <param name="data">Data for which median is to be computed</param> + static public double Median(double[] data) + { + return Percentile(data, 50); + } + + /// <summary> + /// Returns the average value for the given decile of + /// a given set of data + /// </summary> + /// <param name="data">The set of data</param> + /// <param name="decileNumber">The decile for which the average has to be computed</param> + static public double GetDecileAverage(double[] data, + int decileNumber) + { + if(decileNumber<1 || decileNumber >9) + throw new Exception("decileNumber has to be a number between 1 and 9!"); + + double firstCorrespondingPercentile = + Percentile(data, decileNumber * 10 - 10); + double secondCorrespondingPercentile = + Percentile(data, decileNumber * 10); + double totalBetweenPercentiles = 0.0; + int numberOfDataPointsBetweenPercentiles = 0; + for(int i = 0; i<orderedData.Length; i++) + { + if(orderedData[i]>= firstCorrespondingPercentile && + orderedData[i]< secondCorrespondingPercentile) + { + numberOfDataPointsBetweenPercentiles++; + totalBetweenPercentiles += orderedData[i]; + } + + } + + return totalBetweenPercentiles/numberOfDataPointsBetweenPercentiles; + } + + } } ! |