12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- //
- // Purpose: This class is used for Statistical Analysis
- //
- using System;
- using System.Collections;
- namespace FastReport.DataVisualization.Charting.Formulas
- {
- /// <summary>
- ///
- /// </summary>
- internal class StatisticalAnalysis : IFormula
- {
- #region Error strings
- // Error strings
- //internal string inputArrayStart = "Formula requires";
- //internal string inputArrayEnd = "arrays";
-
- #endregion
- #region Parameters
- /// <summary>
- /// Formula Module name
- /// </summary>
- virtual public string Name { get { return SR.FormulaNameStatisticalAnalysis; } }
- #endregion // Parameters
- #region Methods
- /// <summary>
- /// Default constructor
- /// </summary>
- public StatisticalAnalysis()
- {
- }
- /// <summary>
- /// The first method in the module, which converts a formula
- /// name to the corresponding private method.
- /// </summary>
- /// <param name="formulaName">String which represent a formula name</param>
- /// <param name="inputValues">Arrays of doubles - Input values</param>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Formula parameters</param>
- /// <param name="extraParameterList">Array of strings - Extra Formula parameters from DataManipulator object</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- virtual public void Formula( string formulaName, double [][] inputValues, out double [][] outputValues, string [] parameterList, string [] extraParameterList, out string [][] outLabels )
- {
- string name;
-
- outLabels = null;
- name = formulaName.ToUpper(System.Globalization.CultureInfo.InvariantCulture);
- try
- {
- switch( name )
- {
- case "TTESTEQUALVARIANCES":
- TTest( inputValues, out outputValues, parameterList, out outLabels, true );
- break;
- case "TTESTUNEQUALVARIANCES":
- TTest( inputValues, out outputValues, parameterList, out outLabels, false );
- break;
- case "TTESTPAIRED":
- TTestPaired( inputValues, out outputValues, parameterList, out outLabels );
- break;
- case "ZTEST":
- ZTest( inputValues, out outputValues, parameterList, out outLabels );
- break;
- case "FTEST":
- FTest( inputValues, out outputValues, parameterList, out outLabels );
- break;
- case "COVARIANCE":
- Covariance( inputValues, out outputValues, out outLabels );
- break;
- case "CORRELATION":
- Correlation( inputValues, out outputValues, out outLabels );
- break;
- case "ANOVA":
- Anova( inputValues, out outputValues, parameterList, out outLabels );
- break;
- case "TDISTRIBUTION":
- TDistribution( out outputValues, parameterList, out outLabels );
- break;
- case "FDISTRIBUTION":
- FDistribution( out outputValues, parameterList, out outLabels );
- break;
- case "NORMALDISTRIBUTION":
- NormalDistribution( out outputValues, parameterList, out outLabels );
- break;
- case "INVERSETDISTRIBUTION":
- TDistributionInverse( out outputValues, parameterList, out outLabels );
- break;
- case "INVERSEFDISTRIBUTION":
- FDistributionInverse( out outputValues, parameterList, out outLabels );
- break;
- case "INVERSENORMALDISTRIBUTION":
- NormalDistributionInverse( out outputValues, parameterList, out outLabels );
- break;
- case "MEAN":
- Average( inputValues, out outputValues, out outLabels );
- break;
- case "VARIANCE":
- Variance( inputValues, out outputValues, parameterList, out outLabels );
- break;
- case "MEDIAN":
- Median( inputValues, out outputValues, out outLabels );
- break;
- case "BETAFUNCTION":
- BetaFunction( out outputValues, parameterList, out outLabels );
- break;
- case "GAMMAFUNCTION":
- GammaFunction( out outputValues, parameterList, out outLabels );
- break;
- default:
- outputValues = null;
- break;
- }
- }
- catch( IndexOutOfRangeException )
- {
- throw new InvalidOperationException( SR.ExceptionFormulaInvalidPeriod(name) );
- }
- catch( OverflowException )
- {
- throw new InvalidOperationException( SR.ExceptionFormulaNotEnoughDataPoints(name) );
- }
- }
- #endregion // Methods
- #region Statistical Tests
- /// <summary>
- /// Anova test
- /// </summary>
- /// <param name="inputValues">Arrays of doubles - Input values</param>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Parameters</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void Anova(double [][] inputValues, out double [][] outputValues, string [] parameterList, out string [][] outLabels )
- {
- // There is no enough input series
- if( inputValues.Length < 3 )
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesNotEnoughInputSeries);
-
- outLabels = null;
- for( int index = 0; index < inputValues.Length - 1; index++ )
- {
- if( inputValues[index].Length != inputValues[index+1].Length )
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidAnovaTest);
- }
- // Alpha value
- double alpha;
- try
- {
- alpha = double.Parse( parameterList[0], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidAlphaValue);
- }
- if( alpha < 0 || alpha > 1 )
- {
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesInvalidAlphaValue);
- }
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [10];
- // X
- outputValues[0] = new double [10];
- // Y
- outputValues[1] = new double [10];
- int m = inputValues.Length - 1;
- int n = inputValues[0].Length;
- double [] average = new double[ m ];
- double [] variance = new double[ m ];
-
- // Find averages
- for( int group = 0; group < m; group++ )
- {
- average[group] = Mean( inputValues[group+1] );
- }
- // Find variances
- for( int group = 0; group < m; group++ )
- {
- variance[group] = Variance( inputValues[group+1], true );
- }
- // Total Average ( for all groups )
- double averageTotal = Mean( average );
- // Total Sample Variance
- double totalS = 0;
- foreach( double avr in average )
- {
- totalS += ( avr - averageTotal ) * ( avr - averageTotal );
- }
- totalS /= ( m - 1 );
- // Group Sample Variance
- double groupS = Mean( variance );
- // F Statistica
- double f = totalS * ( n ) / groupS;
- // ****************************************
- // Sum of Squares
- // ****************************************
- // Grend Total Average
- double grandTotalAverage = 0;
- for( int group = 0; group < m; group++ )
- {
- foreach( double point in inputValues[group+1] )
- {
- grandTotalAverage += point;
- }
- }
- grandTotalAverage /= ( m * n );
- // Treatment Sum of Squares
- double trss = 0;
- for( int group = 0; group < m; group++ )
- {
- trss += ( average[group] - grandTotalAverage ) * ( average[group] - grandTotalAverage );
- }
- trss *= n;
-
- // Error Sum of Squares
- double erss = 0;
- for( int group = 0; group < m; group++ )
- {
- foreach( double point in inputValues[group+1] )
- {
- erss += ( point - average[group] ) * ( point - average[group] );
- }
- }
- outLabels[0][0] = SR.LabelStatisticalSumOfSquaresBetweenGroups;
- outputValues[0][0] = 1;
- outputValues[1][0] = trss;
- outLabels[0][1] = SR.LabelStatisticalSumOfSquaresWithinGroups;
- outputValues[0][1] = 2;
- outputValues[1][1] = erss;
- outLabels[0][2] = SR.LabelStatisticalSumOfSquaresTotal;
- outputValues[0][2] = 3;
- outputValues[1][2] = trss + erss;
- outLabels[0][3] = SR.LabelStatisticalDegreesOfFreedomBetweenGroups;
- outputValues[0][3] = 4;
- outputValues[1][3] = m - 1;
- outLabels[0][4] = SR.LabelStatisticalDegreesOfFreedomWithinGroups;
- outputValues[0][4] = 5;
- outputValues[1][4] = m * ( n - 1 );
- outLabels[0][5] = SR.LabelStatisticalDegreesOfFreedomTotal;
- outputValues[0][5] = 6;
- outputValues[1][5] = m * n - 1;
- outLabels[0][6] = SR.LabelStatisticalMeanSquareVarianceBetweenGroups;
- outputValues[0][6] = 7;
- outputValues[1][6] = trss / ( m - 1 );
- outLabels[0][7] = SR.LabelStatisticalMeanSquareVarianceWithinGroups;
- outputValues[0][7] = 8;
- outputValues[1][7] = erss / ( m * ( n - 1 ) );
- outLabels[0][8] = SR.LabelStatisticalFRatio;
- outputValues[0][8] = 9;
- outputValues[1][8] = f;
- outLabels[0][9] = SR.LabelStatisticalFCriteria;
- outputValues[0][9] = 10;
- outputValues[1][9] = FDistributionInverse( alpha, m - 1, m * ( n - 1 ) );
-
- }
- /// <summary>
- /// Correlation measure the relationship between two data sets that
- /// are scaled to be independent of the unit of measurement. The
- /// population correlation calculation returns the covariance
- /// of two data sets divided by the product of their standard
- /// deviations: You can use the Correlation to determine whether two
- /// ranges of data move together — that is, whether large values of
- /// one set are associated with large values of the other
- /// (positive correlation), whether small values of one set are
- /// associated with large values of the other (negative correlation),
- /// or whether values in both sets are unrelated (correlation
- /// near zero).
- /// </summary>
- /// <param name="inputValues">Arrays of doubles - Input values</param>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void Correlation(double [][] inputValues, out double [][] outputValues, out string [][] outLabels )
- {
- // There is no enough input series
- if( inputValues.Length != 3 )
- throw new ArgumentException( SR.ExceptionPriceIndicatorsFormulaRequiresTwoArrays);
-
- outLabels = null;
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [1];
- // X
- outputValues[0] = new double [1];
- // Y
- outputValues[1] = new double [1];
- // Find Covariance.
- double covar = Covar( inputValues[1], inputValues[2] );
- double varianceX = Variance( inputValues[1], false );
- double varianceY = Variance( inputValues[2], false );
- // Correlation
- double correl = covar / Math.Sqrt( varianceX * varianceY );
- outLabels[0][0] = SR.LabelStatisticalCorrelation;
- outputValues[0][0] = 1;
- outputValues[1][0] = correl;
- }
- /// <summary>
- /// Returns covariance, the average of the products of deviations
- /// for each data point pair. Use covariance to determine the
- /// relationship between two data sets. For example, you can
- /// examine whether greater income accompanies greater
- /// levels of education.
- /// </summary>
- /// <param name="inputValues">Arrays of doubles - Input values</param>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void Covariance(double [][] inputValues, out double [][] outputValues, out string [][] outLabels )
- {
- // There is no enough input series
- if( inputValues.Length != 3 )
- throw new ArgumentException( SR.ExceptionPriceIndicatorsFormulaRequiresTwoArrays);
-
- outLabels = null;
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [1];
- // X
- outputValues[0] = new double [1];
- // Y
- outputValues[1] = new double [1];
- // Find Covariance.
- double covar = Covar( inputValues[1], inputValues[2] );
- outLabels[0][0] = SR.LabelStatisticalCovariance;
- outputValues[0][0] = 1;
- outputValues[1][0] = covar;
- }
- /// <summary>
- /// Returns the result of an F-test. An F-test returns the one-tailed
- /// probability that the variances in array1 and array2 are not
- /// significantly different. Use this function to determine
- /// whether two samples have different variances. For example,
- /// given test scores from public and private schools, you can
- /// test whether these schools have different levels of diversity.
- /// </summary>
- /// <param name="inputValues">Arrays of doubles - Input values</param>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Parameters</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void FTest(double [][] inputValues, out double [][] outputValues, string [] parameterList, out string [][] outLabels )
- {
- // There is no enough input series
- if( inputValues.Length != 3 )
- throw new ArgumentException( SR.ExceptionPriceIndicatorsFormulaRequiresTwoArrays);
-
- outLabels = null;
- double alpha;
- // The number of data points has to be > 1.
- CheckNumOfPoints( inputValues );
-
- // Alpha value
- try
- {
- alpha = double.Parse( parameterList[0], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidAlphaValue);
- }
- if( alpha < 0 || alpha > 1 )
- {
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesInvalidAlphaValue);
- }
-
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [7];
- // X
- outputValues[0] = new double [7];
- // Y
- outputValues[1] = new double [7];
- // Find Variance of the first group
- double variance1 = Variance( inputValues[1], true );
- // Find Variance of the second group
- double variance2 = Variance( inputValues[2], true );
- // Find Mean of the first group
- double mean1 = Mean( inputValues[1] );
- // Find Mean of the second group
- double mean2 = Mean( inputValues[2] );
-
- // F Value
- double valueF = variance1 / variance2;
- if( variance2 == 0 )
- {
- throw new InvalidOperationException(SR.ExceptionStatisticalAnalysesZeroVariance);
- }
- // The way to find a left critical value is to reversed the degrees of freedom,
- // look up the right critical value, and then take the reciprocal of this value.
- // For example, the critical value with 0.05 on the left with 12 numerator and 15
- // denominator degrees of freedom is found of taking the reciprocal of the critical
- // value with 0.05 on the right with 15 numerator and 12 denominator degrees of freedom.
- // Avoiding Left Critical Values. Since the left critical values are a pain to calculate,
- // they are often avoided altogether. This is the procedure followed in the textbook.
- // You can force the F test into a right tail test by placing the sample with the large
- // variance in the numerator and the smaller variance in the denominator. It does not
- // matter which sample has the larger sample size, only which sample has the larger
- // variance. The numerator degrees of freedom will be the degrees of freedom for
- // whichever sample has the larger variance (since it is in the numerator) and the
- // denominator degrees of freedom will be the degrees of freedom for whichever sample
- // has the smaller variance (since it is in the denominator).
- bool lessOneF = valueF <= 1;
- double fDistInv;
- double fDist;
- if( lessOneF )
- {
- fDistInv = FDistributionInverse( 1 - alpha, inputValues[1].Length - 1, inputValues[2].Length - 1 );
- fDist = 1 - FDistribution( valueF, inputValues[1].Length - 1, inputValues[2].Length - 1 );
- }
- else
- {
- fDistInv = FDistributionInverse( alpha, inputValues[1].Length - 1, inputValues[2].Length - 1 );
- fDist = FDistribution( valueF, inputValues[1].Length - 1, inputValues[2].Length - 1 );
- }
- outLabels[0][0] = SR.LabelStatisticalTheFirstGroupMean;
- outputValues[0][0] = 1;
- outputValues[1][0] = mean1;
- outLabels[0][1] = SR.LabelStatisticalTheSecondGroupMean;
- outputValues[0][1] = 2;
- outputValues[1][1] = mean2;
- outLabels[0][2] = SR.LabelStatisticalTheFirstGroupVariance;
- outputValues[0][2] = 3;
- outputValues[1][2] = variance1;
- outLabels[0][3] = SR.LabelStatisticalTheSecondGroupVariance;
- outputValues[0][3] = 4;
- outputValues[1][3] = variance2;
- outLabels[0][4] = SR.LabelStatisticalFValue;
- outputValues[0][4] = 5;
- outputValues[1][4] = valueF;
- outLabels[0][5] = SR.LabelStatisticalPFLessEqualSmallFOneTail;
- outputValues[0][5] = 6;
- outputValues[1][5] = fDist;
- outLabels[0][6] = SR.LabelStatisticalFCriticalValueOneTail;
- outputValues[0][6] = 7;
- outputValues[1][6] = fDistInv;
- }
- /// <summary>
- /// Returns the two-tailed P-value of a z-test. The z-test
- /// generates a standard score for x with respect to the data set,
- /// array, and returns the two-tailed probability for the
- /// normal distribution. You can use this function to assess
- /// the likelihood that a particular observation is drawn
- /// from a particular population.
- /// </summary>
- /// <param name="inputValues">Arrays of doubles - Input values</param>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Parameters</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void ZTest(double [][] inputValues, out double [][] outputValues, string [] parameterList, out string [][] outLabels )
- {
- // There is no enough input series
- if( inputValues.Length != 3 )
- throw new ArgumentException( SR.ExceptionPriceIndicatorsFormulaRequiresTwoArrays);
-
- // The number of data points has to be > 1.
- CheckNumOfPoints( inputValues );
- outLabels = null;
- double variance1;
- double variance2;
- double alpha;
- double HypothesizedMeanDifference;
- // Find Hypothesized Mean Difference parameter
- try
- {
- HypothesizedMeanDifference = double.Parse( parameterList[0], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidMeanDifference);
- }
- if( HypothesizedMeanDifference < 0.0 )
- {
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesNegativeMeanDifference);
- }
- // Find variance of the first group
- try
- {
- variance1 = double.Parse( parameterList[1], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidVariance);
- }
- // Find variance of the second group
- try
- {
- variance2 = double.Parse( parameterList[2], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidVariance);
- }
- // Alpha value
- try
- {
- alpha = double.Parse( parameterList[3], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidAlphaValue);
- }
- if( alpha < 0 || alpha > 1 )
- {
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesInvalidAlphaValue);
- }
-
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [9];
- // X
- outputValues[0] = new double [9];
- // Y
- outputValues[1] = new double [9];
- // Find Mean of the first group
- double mean1 = Mean( inputValues[1] );
- // Find Mean of the second group
- double mean2 = Mean( inputValues[2] );
-
- double dev = Math.Sqrt( variance1 / inputValues[1].Length + variance2 / inputValues[2].Length );
- // Z Value
- double valueZ = ( mean1 - mean2 - HypothesizedMeanDifference ) / dev;
- double normalDistTwoInv = NormalDistributionInverse( 1 - alpha / 2 );
- double normalDistOneInv = NormalDistributionInverse( 1 - alpha);
- double normalDistOne;
- double normalDistTwo;
- if( valueZ < 0.0 )
- {
- normalDistOne = NormalDistribution( valueZ );
- }
- else
- {
- normalDistOne = 1.0 - NormalDistribution( valueZ );
- }
- normalDistTwo = 2.0 * normalDistOne;
- outLabels[0][0] = SR.LabelStatisticalTheFirstGroupMean;
- outputValues[0][0] = 1;
- outputValues[1][0] = mean1;
- outLabels[0][1] = SR.LabelStatisticalTheSecondGroupMean;
- outputValues[0][1] = 2;
- outputValues[1][1] = mean2;
- outLabels[0][2] = SR.LabelStatisticalTheFirstGroupVariance;
- outputValues[0][2] = 3;
- outputValues[1][2] = variance1;
- outLabels[0][3] = SR.LabelStatisticalTheSecondGroupVariance;
- outputValues[0][3] = 4;
- outputValues[1][3] = variance2;
- outLabels[0][4] = SR.LabelStatisticalZValue;
- outputValues[0][4] = 5;
- outputValues[1][4] = valueZ;
- outLabels[0][5] = SR.LabelStatisticalPZLessEqualSmallZOneTail;
- outputValues[0][5] = 6;
- outputValues[1][5] = normalDistOne;
- outLabels[0][6] = SR.LabelStatisticalZCriticalValueOneTail;
- outputValues[0][6] = 7;
- outputValues[1][6] = normalDistOneInv;
- outLabels[0][7] = SR.LabelStatisticalPZLessEqualSmallZTwoTail;
- outputValues[0][7] = 8;
- outputValues[1][7] = normalDistTwo;
- outLabels[0][8] = SR.LabelStatisticalZCriticalValueTwoTail;
- outputValues[0][8] = 9;
- outputValues[1][8] = normalDistTwoInv;
- }
- /// <summary>
- /// Returns the two-tailed P-value of a z-test. The z-test
- /// generates a standard score for x with respect to the data set,
- /// array, and returns the two-tailed probability for the
- /// normal distribution. You can use this function to assess
- /// the likelihood that a particular observation is drawn
- /// from a particular population.
- /// </summary>
- /// <param name="inputValues">Arrays of doubles - Input values</param>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Parameters</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- /// <param name="equalVariances">True if Variances are equal.</param>
- private void TTest(double [][] inputValues, out double [][] outputValues, string [] parameterList, out string [][] outLabels, bool equalVariances )
- {
- // There is no enough input series
- if( inputValues.Length != 3 )
- throw new ArgumentException( SR.ExceptionPriceIndicatorsFormulaRequiresTwoArrays);
- outLabels = null;
- double variance1;
- double variance2;
- double alpha;
- double HypothesizedMeanDifference;
- // Find Hypothesized Mean Difference parameter
- try
- {
- HypothesizedMeanDifference = double.Parse( parameterList[0], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidMeanDifference);
- }
- if( HypothesizedMeanDifference < 0.0 )
- {
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesNegativeMeanDifference);
- }
- // Alpha value
- try
- {
- alpha = double.Parse( parameterList[1], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidAlphaValue);
- }
- if( alpha < 0 || alpha > 1 )
- {
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesInvalidAlphaValue);
- }
- // The number of data points has to be > 1.
- CheckNumOfPoints( inputValues );
-
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [10];
- // X
- outputValues[0] = new double [10];
- // Y
- outputValues[1] = new double [10];
- // Find Mean of the first group
- double mean1 = Mean( inputValues[1] );
- // Find Mean of the second group
- double mean2 = Mean( inputValues[2] );
- variance1 = Variance( inputValues[1], true );
- variance2 = Variance( inputValues[2], true );
- double s;
- double T;
- int freedom;
- if( equalVariances )
- {
- freedom = inputValues[1].Length + inputValues[2].Length - 2;
- // S value
- s = ( ( inputValues[1].Length - 1 ) * variance1 + ( inputValues[2].Length - 1 ) * variance2 ) / ( inputValues[1].Length + inputValues[2].Length - 2 );
- // T value
- T = ( mean1 - mean2 - HypothesizedMeanDifference ) / ( Math.Sqrt( s * ( 1.0 / inputValues[1].Length + 1.0 / inputValues[2].Length ) ) );
-
- }
- else
- {
- double m = inputValues[1].Length;
- double n = inputValues[2].Length;
- double s1 = variance1;
- double s2 = variance2;
- double f = ( s1 / m + s2 / n ) * ( s1 / m + s2 / n ) / ( ( s1 / m ) * ( s1 / m ) / ( m - 1 ) + ( s2 / n ) * ( s2 / n ) / ( n - 1 ) );
- freedom = (int)Math.Round(f);
- s = Math.Sqrt( variance1 / inputValues[1].Length + variance2 / inputValues[2].Length );
- // Z Value
- T = ( mean1 - mean2 - HypothesizedMeanDifference ) / s;
- }
-
- double TDistTwoInv = StudentsDistributionInverse( alpha , freedom );
- bool more50 = alpha > 0.5;
- if( more50 )
- {
- alpha = 1 - alpha;
- }
- double TDistOneInv = StudentsDistributionInverse( alpha * 2.0, freedom );
- if( more50 )
- {
- TDistOneInv *= -1.0;
- }
-
- double TDistTwo = StudentsDistribution( T, freedom, false );
- double TDistOne = StudentsDistribution( T, freedom, true );
- outLabels[0][0] = SR.LabelStatisticalTheFirstGroupMean;
- outputValues[0][0] = 1;
- outputValues[1][0] = mean1;
- outLabels[0][1] = SR.LabelStatisticalTheSecondGroupMean;
- outputValues[0][1] = 2;
- outputValues[1][1] = mean2;
- outLabels[0][2] = SR.LabelStatisticalTheFirstGroupVariance;
- outputValues[0][2] = 3;
- outputValues[1][2] = variance1;
- outLabels[0][3] = SR.LabelStatisticalTheSecondGroupVariance;
- outputValues[0][3] = 4;
- outputValues[1][3] = variance2;
- outLabels[0][4] = SR.LabelStatisticalTValue;
- outputValues[0][4] = 5;
- outputValues[1][4] = T;
- outLabels[0][5] = SR.LabelStatisticalDegreeOfFreedom;
- outputValues[0][5] = 6;
- outputValues[1][5] = freedom;
- outLabels[0][6] = SR.LabelStatisticalPTLessEqualSmallTOneTail;
- outputValues[0][6] = 7;
- outputValues[1][6] = TDistOne;
- outLabels[0][7] = SR.LabelStatisticalSmallTCrititcalOneTail;
- outputValues[0][7] = 8;
- outputValues[1][7] = TDistOneInv;
- outLabels[0][8] = SR.LabelStatisticalPTLessEqualSmallTTwoTail;
- outputValues[0][8] = 9;
- outputValues[1][8] = TDistTwo;
- outLabels[0][9] = SR.LabelStatisticalSmallTCrititcalTwoTail;
- outputValues[0][9] = 10;
- outputValues[1][9] = TDistTwoInv;
- }
- /// <summary>
- /// Returns the two-tailed P-value of a z-test. The z-test
- /// generates a standard score for x with respect to the data set,
- /// array, and returns the two-tailed probability for the
- /// normal distribution. You can use this function to assess
- /// the likelihood that a particular observation is drawn
- /// from a particular population.
- /// </summary>
- /// <param name="inputValues">Arrays of doubles - Input values</param>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Parameters</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void TTestPaired(double [][] inputValues, out double [][] outputValues, string [] parameterList, out string [][] outLabels )
- {
- // There is no enough input series
- if( inputValues.Length != 3 )
- throw new ArgumentException( SR.ExceptionPriceIndicatorsFormulaRequiresTwoArrays);
-
- if( inputValues[1].Length != inputValues[2].Length )
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidVariableRanges);
- outLabels = null;
- double variance;
- double alpha;
- double HypothesizedMeanDifference;
- int freedom;
- // Find Hypothesized Mean Difference parameter
- try
- {
- HypothesizedMeanDifference = double.Parse( parameterList[0], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidMeanDifference);
- }
- if( HypothesizedMeanDifference < 0.0 )
- {
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesNegativeMeanDifference);
- }
-
- // Alpha value
- try
- {
- alpha = double.Parse( parameterList[1], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidAlphaValue);
- }
- if( alpha < 0 || alpha > 1 )
- {
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesInvalidAlphaValue);
- }
- // The number of data points has to be > 1.
- CheckNumOfPoints( inputValues );
-
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [10];
- // X
- outputValues[0] = new double [10];
- // Y
- outputValues[1] = new double [10];
- double [] difference = new double[inputValues[1].Length];
-
- for( int item = 0; item < inputValues[1].Length; item++ )
- {
- difference[item] = inputValues[1][item] - inputValues[2][item];
- }
- // Find Mean of the second group
- double mean = Mean( difference );
- variance = Math.Sqrt( Variance( difference, true ) );
- double T = ( Math.Sqrt( inputValues[1].Length ) * ( mean - HypothesizedMeanDifference ) ) / variance;
- freedom = inputValues[1].Length - 1;
-
- double TDistTwoInv = StudentsDistributionInverse( alpha , freedom );
- double TDistOneInv = alpha <= 0.5 ? StudentsDistributionInverse(2 * alpha, freedom) : double.NaN;
- double TDistTwo = StudentsDistribution( T, freedom, false );
- double TDistOne = StudentsDistribution( T, freedom, true );
- outLabels[0][0] = SR.LabelStatisticalTheFirstGroupMean;
- outputValues[0][0] = 1;
- outputValues[1][0] = Mean(inputValues[1]);
- outLabels[0][1] = SR.LabelStatisticalTheSecondGroupMean;
- outputValues[0][1] = 2;
- outputValues[1][1] = Mean(inputValues[2]);
- outLabels[0][2] = SR.LabelStatisticalTheFirstGroupVariance;
- outputValues[0][2] = 3;
- outputValues[1][2] = Variance(inputValues[1],true);
- outLabels[0][3] = SR.LabelStatisticalTheSecondGroupVariance;
- outputValues[0][3] = 4;
- outputValues[1][3] = Variance(inputValues[2],true);
- outLabels[0][4] = SR.LabelStatisticalTValue;
- outputValues[0][4] = 5;
- outputValues[1][4] = T;
- outLabels[0][5] = SR.LabelStatisticalDegreeOfFreedom;
- outputValues[0][5] = 6;
- outputValues[1][5] = freedom;
- outLabels[0][6] = SR.LabelStatisticalPTLessEqualSmallTOneTail;
- outputValues[0][6] = 7;
- outputValues[1][6] = TDistOne;
- outLabels[0][7] = SR.LabelStatisticalSmallTCrititcalOneTail;
- outputValues[0][7] = 8;
- outputValues[1][7] = TDistOneInv;
- outLabels[0][8] = SR.LabelStatisticalPTLessEqualSmallTTwoTail;
- outputValues[0][8] = 9;
- outputValues[1][8] = TDistTwo;
- outLabels[0][9] = SR.LabelStatisticalSmallTCrititcalTwoTail;
- outputValues[0][9] = 10;
- outputValues[1][9] = TDistTwoInv;
- }
- #endregion // Statistical Tests
- #region Public distributions
- /// <summary>
- /// Returns the Percentage Points (probability) for the Student
- /// t-distribution. The t-distribution is used in the hypothesis
- /// testing of small sample data sets. Use this function in place
- /// of a table of critical values for the t-distribution.
- /// </summary>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Parameters</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void TDistribution(out double [][] outputValues, string [] parameterList, out string [][] outLabels )
- {
- // T value value
- double tValue;
- try
- {
- tValue = double.Parse( parameterList[0], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidTValue);
- }
- // DegreeOfFreedom
- int freedom;
- try
- {
- freedom = int.Parse( parameterList[1], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidDegreeOfFreedom);
- }
- // One Tailed distribution
- int oneTailed;
- try
- {
- oneTailed = int.Parse( parameterList[2], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidTailedParameter);
- }
-
- outLabels = null;
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [1];
- // X
- outputValues[0] = new double [1];
- // Y
- outputValues[1] = new double [1];
- outLabels[0][0] = SR.LabelStatisticalProbability;
- outputValues[0][0] = 1;
- outputValues[1][0] = StudentsDistribution( tValue, freedom, oneTailed == 1 );
- }
- /// <summary>
- /// Returns the F probability distribution. You can use
- /// this function to determine whether two data sets have
- /// different degrees of diversity. For example, you can
- /// examine test scores given to men and women entering
- /// high school and determine if the variability in the
- /// females is different from that found in the males.
- /// </summary>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Parameters</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void FDistribution(out double [][] outputValues, string [] parameterList, out string [][] outLabels )
- {
- // F value value
- double fValue;
- try
- {
- fValue = double.Parse( parameterList[0], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidTValue);
- }
- // Degree Of Freedom 1
- int freedom1;
- try
- {
- freedom1 = int.Parse( parameterList[1], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidDegreeOfFreedom);
- }
- // Degree Of Freedom 2
- int freedom2;
- try
- {
- freedom2 = int.Parse( parameterList[2], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidDegreeOfFreedom);
- }
-
- outLabels = null;
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [1];
- // X
- outputValues[0] = new double [1];
- // Y
- outputValues[1] = new double [1];
- outLabels[0][0] = SR.LabelStatisticalProbability;
- outputValues[0][0] = 1;
- outputValues[1][0] = FDistribution( fValue, freedom1, freedom2 );
- }
- /// <summary></summary>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Parameters</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void NormalDistribution(out double [][] outputValues, string [] parameterList, out string [][] outLabels )
- {
- // F value value
- double zValue;
- try
- {
- zValue = double.Parse( parameterList[0], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidZValue);
- }
-
- outLabels = null;
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [1];
- // X
- outputValues[0] = new double [1];
- // Y
- outputValues[1] = new double [1];
- outLabels[0][0] = SR.LabelStatisticalProbability;
- outputValues[0][0] = 1;
- outputValues[1][0] = this.NormalDistribution( zValue );
- }
- /// <summary>
- /// Returns the t-value of the Student's t-distribution
- /// as a function of the probability and the degrees
- /// of freedom.
- /// </summary>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Parameters</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void TDistributionInverse(out double [][] outputValues, string [] parameterList, out string [][] outLabels )
- {
- // T value value
- double probability;
- try
- {
- probability = double.Parse( parameterList[0], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidProbabilityValue);
- }
- // DegreeOfFreedom
- int freedom;
- try
- {
- freedom = int.Parse( parameterList[1], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidDegreeOfFreedom);
- }
-
- outLabels = null;
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [1];
- // X
- outputValues[0] = new double [1];
- // Y
- outputValues[1] = new double [1];
- outLabels[0][0] = SR.LabelStatisticalProbability;
- outputValues[0][0] = 1;
- outputValues[1][0] = StudentsDistributionInverse( probability, freedom );
- }
- /// <summary>
- /// Returns the inverse of the F probability distribution.
- /// If p = FDIST(x,...), then FINV(p,...) = x. The F distribution
- /// can be used in an F-test that compares the degree of
- /// variability in two data sets. For example, you can analyze
- /// income distributions in the United States and Canada to
- /// determine whether the two countries have a similar degree
- /// of diversity.
- /// </summary>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Parameters</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void FDistributionInverse(out double [][] outputValues, string [] parameterList, out string [][] outLabels )
- {
- // Probability value value
- double probability;
- try
- {
- probability = double.Parse( parameterList[0], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidProbabilityValue);
- }
- // Degree Of Freedom 1
- int freedom1;
- try
- {
- freedom1 = int.Parse( parameterList[1], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidDegreeOfFreedom);
- }
- // Degree Of Freedom 2
- int freedom2;
- try
- {
- freedom2 = int.Parse( parameterList[2], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidDegreeOfFreedom);
- }
-
- outLabels = null;
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [1];
- // X
- outputValues[0] = new double [1];
- // Y
- outputValues[1] = new double [1];
- outLabels[0][0] = SR.LabelStatisticalProbability;
- outputValues[0][0] = 1;
- outputValues[1][0] = FDistributionInverse( probability, freedom1, freedom2 );
- }
- /// <summary>
- /// Returns the inverse of the standard normal
- /// cumulative distribution. The distribution
- /// has a mean of zero and a standard deviation
- /// of one.
- /// </summary>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Parameters</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void NormalDistributionInverse(out double [][] outputValues, string [] parameterList, out string [][] outLabels )
- {
- // Alpha value value
- double alpha;
- try
- {
- alpha = double.Parse( parameterList[0], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidAlphaValue);
- }
-
- outLabels = null;
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [1];
- // X
- outputValues[0] = new double [1];
- // Y
- outputValues[1] = new double [1];
- outLabels[0][0] = SR.LabelStatisticalProbability;
- outputValues[0][0] = 1;
- outputValues[1][0] = this.NormalDistributionInverse( alpha );
- }
- #endregion
- #region Utility Statistical Functions
- /// <summary>
- /// Check number of data points. The number should be greater then 1.
- /// </summary>
- /// <param name="inputValues">Input series</param>
- private void CheckNumOfPoints( double [][] inputValues )
- {
- if( inputValues[1].Length < 2 )
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesNotEnoughDataPoints);
- }
- if( inputValues.Length > 2 )
- {
- if( inputValues[2].Length < 2 )
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesNotEnoughDataPoints);
- }
- }
- }
-
- /// <summary>
- /// Returns covariance, the average of the products of deviations
- /// for each data point pair. Use covariance to determine the
- /// relationship between two data sets. For example, you can
- /// examine whether greater income accompanies greater
- /// levels of education.
- /// </summary>
- /// <param name="arrayX">First data set from X random variable.</param>
- /// <param name="arrayY">Second data set from Y random variable.</param>
- /// <returns>Returns covariance</returns>
- private double Covar( double [] arrayX, double [] arrayY )
- {
- // Check the number of data points
- if( arrayX.Length != arrayY.Length )
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesCovariance);
- }
- double [] arrayXY = new double[arrayX.Length];
- // Find XY
- for( int index = 0; index < arrayX.Length; index++ )
- {
- arrayXY[index] = arrayX[index] * arrayY[index];
- }
- // Find means
- double meanXY = Mean( arrayXY );
- double meanX = Mean( arrayX );
- double meanY = Mean( arrayY );
- // return covariance
- return meanXY - meanX * meanY;
- }
- /// <summary>
- /// Returns the natural logarithm of the gamma function, G(x).
- /// </summary>
- /// <param name="n">The value for which you want to calculate gamma function.</param>
- /// <returns>Returns the natural logarithm of the gamma function.</returns>
- private double GammLn( double n )
- {
- double x;
- double y;
- double tmp;
- double sum;
- double [] cof = {76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.1208650973866179e-2, -0.5395239384953e-5};
- if( n < 0 )
- {
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesGammaBetaNegativeParameters);
- }
- // Iterative method for Gamma function
- y = x = n;
- tmp = x + 5.5;
- tmp -= ( x + 0.5 ) * Math.Log( tmp );
- sum = 1.000000000190015;
- for( int item = 0; item <=5; item++ )
- {
- sum += cof[item] / ++y;
- }
- return -tmp + Math.Log( 2.5066282746310005 * sum / x );
- }
- /// <summary>
- /// Calculates Beta function
- /// </summary>
- /// <param name="m">First parameter for beta function</param>
- /// <param name="n">Second parameter for beta function</param>
- /// <returns>returns beta function</returns>
- private double BetaFunction( double m, double n )
- {
- return Math.Exp( GammLn( m ) + GammLn( n ) - GammLn( m + n ) );
- }
- /// <summary>
- /// Used by betai: Evaluates continued fraction for
- /// incomplete beta function by modified Lentz’s
- /// </summary>
- /// <param name="a">Beta incomplete parameter</param>
- /// <param name="b">Beta incomplete parameter</param>
- /// <param name="x">Beta incomplete parameter</param>
- /// <returns>Value used for Beta incomplete function</returns>
- private double BetaCF( double a, double b, double x )
- {
- int MAXIT = 100;
- double EPS = 3.0e-7;
- double FPMIN = 1.0e-30;
- int m,m2;
- double aa,c,d,del,h,qab,qam,qap;
- qab = a + b;
- qap= a + 1.0;
- qam = a - 1.0;
- c = 1.0;
- d = 1.0 - qab * x / qap;
- if ( Math.Abs(d) < FPMIN ) d=FPMIN;
- d = 1.0 / d;
- h = d;
- // Numerical approximation for Beta incomplete function
- for( m=1; m<=MAXIT; m++ )
- {
- m2 = 2*m;
- aa = m*(b-m)*x/((qam+m2)*(a+m2));
- // Find d coeficient
- d = 1.0 + aa*d;
- if( Math.Abs(d) < FPMIN ) d=FPMIN;
- // Find c coeficient
- c = 1.0 + aa / c;
- if( Math.Abs(c) < FPMIN ) c = FPMIN;
- // Find d coeficient
- d = 1.0 / d;
- // Find h coeficient
- h *= d*c;
- aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2));
- // Recalc d coeficient
- d=1.0+aa*d;
- if (Math.Abs(d) < FPMIN) d=FPMIN;
- // Recalc c coeficient
- c=1.0+aa/c;
- if (Math.Abs(c) < FPMIN) c=FPMIN;
- // Recalc d coeficient
- d=1.0/d;
- del=d*c;
- // Recalc h coeficient
- h *= del;
- if (Math.Abs(del-1.0) < EPS)
- {
- break;
- }
- }
- if (m > MAXIT)
- {
- throw new InvalidOperationException(SR.ExceptionStatisticalAnalysesIncompleteBetaFunction);
- }
- return h;
- }
- /// <summary>
- /// Standard normal density function
- /// </summary>
- /// <param name="t">T Value</param>
- /// <returns>Standard normal density</returns>
- private double NormalDistributionFunction(double t)
- {
- return 0.398942280401433 * Math.Exp( -t * t / 2 );
- }
- /// <summary>
- /// Returns the incomplete beta function Ix(a, b).
- /// </summary>
- /// <param name="a">Beta incomplete parameter</param>
- /// <param name="b">Beta incomplete parameter</param>
- /// <param name="x">Beta incomplete parameter</param>
- /// <returns>Beta Incomplete value</returns>
- private double BetaIncomplete( double a, double b, double x )
- {
- double bt;
- if (x < 0.0 || x > 1.0)
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesInvalidInputParameter);
- if (x == 0.0 || x == 1.0)
- {
- bt = 0.0;
- }
- else
- { // Factors in front of the continued fraction.
- bt = Math.Exp(GammLn(a + b) - GammLn(a) - GammLn(b) + a * Math.Log(x) + b * Math.Log(1.0 - x));
- }
- if (x < (a + 1.0) / (a + b + 2.0))
- { //Use continued fraction directly.
- return bt * BetaCF(a, b, x) / a;
- }
- else
- { // Use continued fraction after making the symmetry transformation.
- return 1.0 - bt * BetaCF(b, a, 1.0 - x) / b;
- }
- }
-
- #endregion // Utility Statistical Functions
-
- #region Statistical Parameters
- /// <summary>
- /// Returns the average (arithmetic mean) of the arguments.
- /// </summary>
- /// <param name="inputValues">Arrays of doubles - Input values</param>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void Average(double [][] inputValues, out double [][] outputValues, out string [][] outLabels )
- {
-
- outLabels = null;
- // Invalid number of data series
- if( inputValues.Length != 2 )
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidSeriesNumber);
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [1];
- // X
- outputValues[0] = new double [1];
- // Y
- outputValues[1] = new double [1];
- outLabels[0][0] = SR.LabelStatisticalAverage;
- outputValues[0][0] = 1;
- outputValues[1][0] = Mean( inputValues[1] );
- }
- /// <summary>
- /// Calculates variance
- /// </summary>
- /// <param name="inputValues">Arrays of doubles - Input values</param>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Parameters</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void Variance(double [][] inputValues, out double [][] outputValues, string [] parameterList, out string [][] outLabels )
- {
-
- // Sample Variance value
- bool sampleVariance;
- try
- {
- sampleVariance = bool.Parse( parameterList[0] );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidVariance);
- }
- CheckNumOfPoints(inputValues);
- // Invalid number of data series
- if( inputValues.Length != 2 )
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidSeriesNumber);
- outLabels = null;
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [1];
- // X
- outputValues[0] = new double [1];
-
- // Y
- outputValues[1] = new double [1];
- outLabels[0][0] = SR.LabelStatisticalVariance;
- outputValues[0][0] = 1;
- outputValues[1][0] = Variance( inputValues[1], sampleVariance );
- }
- /// <summary>
- /// Calculates Median
- /// </summary>
- /// <param name="inputValues">Arrays of doubles - Input values</param>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void Median(double [][] inputValues, out double [][] outputValues, out string [][] outLabels )
- {
-
- outLabels = null;
- // Invalid number of data series
- if( inputValues.Length != 2 )
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidSeriesNumber);
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [1];
- // X
- outputValues[0] = new double [1];
-
- // Y
- outputValues[1] = new double [1];
- outLabels[0][0] = SR.LabelStatisticalMedian;
- outputValues[0][0] = 1;
- outputValues[1][0] = Median( inputValues[1] );
- }
- /// <summary>
- /// Calculates Beta Function
- /// </summary>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Parameters</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void BetaFunction(out double [][] outputValues, string [] parameterList, out string [][] outLabels )
- {
- // Degree of freedom
- double m;
- try
- {
- m = double.Parse( parameterList[0], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidDegreeOfFreedom);
- }
- // Degree of freedom
- double n;
- try
- {
- n = double.Parse( parameterList[1], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidDegreeOfFreedom);
- }
-
- outLabels = null;
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [1];
- // X
- outputValues[0] = new double [1];
-
- // Y
- outputValues[1] = new double [1];
- outLabels[0][0] = SR.LabelStatisticalBetaFunction;
- outputValues[0][0] = 1;
- outputValues[1][0] = BetaFunction( m, n );
- }
- /// <summary>
- /// Calculates Gamma Function
- /// </summary>
- /// <param name="outputValues">Arrays of doubles - Output values</param>
- /// <param name="parameterList">Array of strings - Parameters</param>
- /// <param name="outLabels">Array of strings - Used for Labels. Description for output results.</param>
- private void GammaFunction(out double [][] outputValues, string [] parameterList, out string [][] outLabels )
- {
- // Degree of freedom
- double m;
- try
- {
- m = double.Parse( parameterList[0], System.Globalization.CultureInfo.InvariantCulture );
- }
- catch(System.Exception)
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidInputParameter);
- }
-
- if( m < 0 )
- {
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesGammaBetaNegativeParameters);
- }
-
- outLabels = null;
- // Output arrays
- outputValues = new double [2][];
- // Output Labels
- outLabels = new string [1][];
- // Parameters description
- outLabels[0] = new string [1];
- // X
- outputValues[0] = new double [1];
-
- // Y
- outputValues[1] = new double [1];
- outLabels[0][0] = SR.LabelStatisticalGammaFunction;
- outputValues[0][0] = 1;
- outputValues[1][0] = Math.Exp( GammLn( m ) );
- }
- /// <summary>
- /// Sort array of double values.
- /// </summary>
- /// <param name="values">Array of doubles which should be sorted.</param>
- private void Sort( ref double [] values )
- {
-
- double tempValue;
- for( int outLoop = 0; outLoop < values.Length; outLoop++ )
- {
- for( int inLoop = outLoop + 1; inLoop < values.Length; inLoop++ )
- {
- if( values[ outLoop ] > values[ inLoop ] )
- {
- tempValue = values[ outLoop ];
- values[ outLoop ] = values[ inLoop ];
- values[ inLoop ] = tempValue;
- }
- }
- }
- }
- /// <summary>
- /// Returns the median of the given numbers
- /// </summary>
- /// <param name="values">Array of double numbers</param>
- /// <returns>Median</returns>
- private double Median( double [] values )
- {
- // Exception for zero lenght of series.
- if( values.Length == 0 )
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidMedianConditions);
- }
- // Sort array
- Sort( ref values );
- int position = values.Length / 2;
- // if number of points is even
- if( values.Length % 2 == 0 )
- {
- return ( values[position-1] + values[position] ) / 2.0;
- }
- else
- {
- return values[position];
- }
- }
-
- /// <summary>
- /// Calculates a Mean for a series of numbers.
- /// </summary>
- /// <param name="values">series with double numbers</param>
- /// <returns>Returns Mean</returns>
- private double Mean( double [] values )
- {
- // Exception for zero lenght of series.
- if( values.Length == 0 )
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidMeanConditions);
- }
- // Find sum of values
- double sum = 0;
- foreach( double item in values )
- {
- sum += item;
- }
- // Calculate Mean
- return sum / values.Length;
- }
- /// <summary>
- /// Calculates a Variance for a series of numbers.
- /// </summary>
- /// <param name="values">double values</param>
- /// <param name="sampleVariance">If variance is calculated from sample sum has to be divided by n-1.</param>
- /// <returns>Variance</returns>
- private double Variance( double [] values, bool sampleVariance )
- {
- // Exception for zero lenght of series.
- if( values.Length < 1 )
- {
- throw new ArgumentException(SR.ExceptionStatisticalAnalysesInvalidVarianceConditions);
- }
-
- // Find sum of values
- double sum = 0;
- double mean = Mean( values );
- foreach( double item in values )
- {
- sum += (item - mean) * (item - mean);
- }
- // Calculate Variance
- if( sampleVariance )
- {
- return sum / ( values.Length - 1 );
- }
- else
- {
- return sum / values.Length;
- }
- }
- #endregion // Statistical Parameters
-
- # region Distributions
- /// <summary>
- /// Calculates the Percentage Points (probability) for the Student
- /// t-distribution. The t-distribution is used in the hypothesis
- /// testing of small sample data sets. Use this function in place
- /// of a table of critical values for the t-distribution.
- /// </summary>
- /// <param name="tValue">The numeric value at which to evaluate the distribution.</param>
- /// <param name="n">An integer indicating the number of degrees of freedom.</param>
- /// <param name="oneTailed">Specifies the number of distribution tails to return.</param>
- /// <returns>Returns the Percentage Points (probability) for the Student t-distribution.</returns>
- private double StudentsDistribution( double tValue, int n, bool oneTailed )
- {
- // Validation
- tValue = Math.Abs( tValue );
- if( n > 300 )
- {
- n = 300;
- }
- if( n < 1 )
- {
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesStudentsNegativeFreedomDegree);
- }
- double result = 1 - BetaIncomplete( n / 2.0, 0.5, n / (n + tValue * tValue) );
- if( oneTailed )
- return ( 1.0 - result ) / 2.0;
- else
- return 1.0 - result;
- }
- /// <summary>
- /// Returns the standard normal cumulative distribution
- /// function. The distribution has a mean of 0 (zero) and
- /// a standard deviation of one. Use this function in place
- /// of a table of standard normal curve areas.
- /// </summary>
- /// <param name="zValue">The value for which you want the distribution.</param>
- /// <returns>Returns the standard normal cumulative distribution.</returns>
- private double NormalDistribution( double zValue )
- {
-
- double [] a = {0.31938153,-0.356563782,1.781477937,-1.821255978,1.330274429};
- double result;
- if (zValue<-7.0)
- {
- result = NormalDistributionFunction(zValue)/Math.Sqrt(1.0+zValue*zValue);
- }
- else if (zValue>7.0)
- {
- result = 1.0 - NormalDistribution(-zValue);
- }
- else
- {
- result = 0.2316419;
- result=1.0/(1+result*Math.Abs(zValue));
- result=1-NormalDistributionFunction(zValue)*(result*(a[0]+result*(a[1]+result*(a[2]+result*(a[3]+result*a[4])))));
- if (zValue<=0.0)
- result=1.0-result;
- }
- return result;
- }
-
- private double FDistribution( double x, int freedom1, int freedom2 )
- {
- if (x < 0)
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesInvalidTValue);
- if (freedom1 <= 0)
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesInvalidDegreeOfFreedom);
- if (freedom2 <= 0)
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesInvalidDegreeOfFreedom);
- if (x == 0)
- return 1;
- if (x == double.PositiveInfinity)
- return 0;
- return BetaIncomplete( freedom2 / 2.0, freedom1 / 2.0, freedom2 / ( freedom2 + freedom1 * x ) );
- }
- #endregion // Distributions
- # region Inverse Distributions
-
- /// <summary>
- /// Calculates the t-value of the Student's t-distribution
- /// as a function of the probability and the degrees of freedom.
- /// </summary>
- /// <param name="probability">The probability associated with the two-tailed Student's t-distribution.</param>
- /// <param name="n">The number of degrees of freedom to characterize the distribution.</param>
- /// <returns>Returns the t-value of the Student's t-distribution.</returns>
- private double StudentsDistributionInverse( double probability, int n )
- {
- //Fix for boundary cases
- if (probability == 0)
- return double.PositiveInfinity;
- else if (probability == 1)
- return 0;
- else if (probability < 0 || probability > 1)
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesInvalidProbabilityValue);
- int step = 0;
- return StudentsDistributionSearch( probability, n, step, 0.0, 100000.0 );
- }
- /// <summary>
- /// Method for calculation of Inverse T Distribution (Binary tree)
- /// solution for non linear equations
- /// </summary>
- /// <param name="probability">Probability value</param>
- /// <param name="n">Degree of freedom</param>
- /// <param name="step">Step for Numerical solution for non linear equations</param>
- /// <param name="start">Start for numerical process</param>
- /// <param name="end">End for numerical process</param>
- /// <returns>Returns F ditribution inverse</returns>
- private double StudentsDistributionSearch( double probability, int n, int step, double start, double end )
- {
- step++;
-
- double mid = ( start + end ) / 2.0;
- double result = StudentsDistribution( mid, n, false );
- double resultX;
- if( step > 100 )
- {
- return mid;
- }
-
- if( result <= probability )
- {
- resultX = StudentsDistributionSearch( probability, n, step, start, mid );
- }
- else
- {
- resultX = StudentsDistributionSearch( probability, n, step, mid, end );
- }
- return resultX;
- }
- /// <summary>
- /// Returns the inverse of the standard normal cumulative distribution.
- /// The distribution has a mean of zero and a standard deviation of one.
- /// </summary>
- /// <param name="probability">A probability corresponding to the normal distribution.</param>
- /// <returns>Returns the inverse of the standard normal cumulative distribution.</returns>
- private double NormalDistributionInverse( double probability )
- {
-
- // Validation
- if( probability < 0.00001 || probability > 0.99999 )
- {
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesNormalInvalidProbabilityValue);
- }
- double [] a = { 2.50662823884, -18.61500062529, 41.39119773534, -25.44106049637 };
- double [] b = { -8.47351093090, 23.08336743743, -21.06224101826, 3.13082909833 };
- double [] c = { 0.3374754822726147, 0.9761690190917186, 0.1607979714918209, 0.0276438810333863, 0.0038405729373609, 0.0003951896511919, 0.0000321767881768, 0.0000002888167364, 0.0000003960315187};
- double x,r;
- // Numerical Integration
- x = probability - 0.5;
- if ( Math.Abs(x) < 0.42 )
- {
- r = x * x;
- r = x * ( ( ( a[3] * r + a[2] ) * r + a[1] ) * r + a[0] ) / ( ( ( ( b[3] * r + b[2] ) * r + b[1] ) * r + b[0] ) * r + 1.0 );
- return( r );
- }
- r= probability;
- if( x > 0.0 )
- {
- r = 1.0 - probability;
- }
- r = Math.Log( -Math.Log( r ) );
- r = c[0] + r * ( c[1] + r * ( c[2] + r * ( c[3] + r * ( c[4] + r * ( c[5] + r * ( c[6] + r * ( c[7]+r * c[8] ) ) ) ) ) ) );
- if( x < 0.0 )
- {
- r = -r;
- }
- return r;
- }
- /// <summary>
- /// Calculates the inverse of the F probability distribution.
- /// The F distribution can be used in an F-test that compares
- /// the degree of variability in two data sets.
- /// </summary>
- /// <param name="probability">A probability associated with the F cumulative distribution.</param>
- /// <param name="m">The numerator degrees of freedom.</param>
- /// <param name="n">The denominator degrees of freedom.</param>
- /// <returns>Returns the inverse of the F probability distribution.</returns>
- private double FDistributionInverse( double probability, int m, int n )
- {
- //Fix for boundary cases
- if (probability == 0)
- return double.PositiveInfinity;
- else if (probability == 1)
- return 0;
- else if (probability < 0 || probability > 1)
- throw new ArgumentOutOfRangeException(SR.ExceptionStatisticalAnalysesInvalidProbabilityValue);
- int step = 0;
- return FDistributionSearch( probability, m, n, step, 0.0, 10000.0 );
- }
- /// <summary>
- /// Method for calculation of Inverse F Distribution (Binary tree)
- /// solution for non linear equations
- /// </summary>
- /// <param name="probability">Probability value</param>
- /// <param name="m">Degree of freedom</param>
- /// <param name="n">Degree of freedom</param>
- /// <param name="step">Step for solution for non linear equations.</param>
- /// <param name="start">Start for numerical process</param>
- /// <param name="end">End for numerical process</param>
- /// <returns>Returns F ditribution inverse</returns>
- private double FDistributionSearch( double probability, int m, int n, int step, double start, double end )
- {
- step++;
- double mid = ( start + end ) / 2.0;
- double result = FDistribution( mid, m, n );
- double resultX;
- if( step > 30 )
- {
- return mid;
- }
-
- if( result <= probability )
- {
- resultX = FDistributionSearch( probability, m, n, step, start, mid );
- }
- else
- {
- resultX = FDistributionSearch( probability, m, n, step, mid, end );
- }
- return resultX;
- }
- #endregion // Inverse Distributions
- }
- }
|