[Resource] Descriptive Statistics in Papyrus

Post » Thu Jul 11, 2013 5:07 am

I decided to make optimal (?) use of the excess time on my hands and created some functions that offer statistical insights into your float arrays. Each array is treated as a variable, and each element is a case.

For univariate statistics, the mode, median, mean and standard deviation are represented, as well as a generic "moment" function which you can use to calculate skewness and curtosis. You may also perform correlations and bivariate regression.

I'd love to add the Chi-Squared statistic, but I can't figure out a way to crosstabulate. Do I need a multi-dimensional array (with while loops etc)?

Spoiler

Scriptname Statistics extends MathFloat[] Function Values(Float[] afArray) Global	Float[] ValueList	Int Case = afArray.Length	Int Code = 0	Float CurrentValue = http://forums.bethsoft.com/topic/1466007-resource-descriptive-statistics-in-papyrus/0.0	While Case> 0		Case -= 1		CurrentValue = http://forums.bethsoft.com/topic/1466007-resource-descriptive-statistics-in-papyrus/afArray[Case]		If ValueList.Find(CurrentValue) < 0			ValueList[Code] = CurrentValue			Code += 1		EndIf	EndWhile	Return ValueListEndFunctionFloat[] Function Sort(Float[] afArray) Global	Float[] Sorted	Float[] ValueList = Values(afArray)	Float HighestValue = MaxValue(ValueList)	Int Code = 0	While Code < ValueList.Length		If ValueList[Code] != HighestValue			Sorted[Code] = MinValue(ValueList)			ValueList[ValueList.Find(Sorted[Code])] = HighestValue		EndIf		Code += 1	EndWhile	Sorted[Code] = HighestValue	Return SortedEndFunctionFloat Function Min(Float af1, Float af2) Global	If af1> af2		Return af2	Else		Return af1	EndIfEndFunctionFloat Function Max(Float af1, Float af2) Global	If Min(af1, af2) == af2		Return af1	Else		Return af2	EndIfEndFunctionFloat Function MinValue(Float[] afArray) Global	Int Case = afArray.Length - 1	Float LowestValue = http://forums.bethsoft.com/topic/1466007-resource-descriptive-statistics-in-papyrus/afArray[afArray.Length]	While Case> 0		Case -= 1		If LowestValue > afArray[Case]			LowestValue = afArray[Case]		EndIf	EndWhile	Return LowestValueEndFunctionFloat Function MaxValue(Float[] afArray) Global	Int Case = afArray.Length - 1	Float HighestValue = http://forums.bethsoft.com/topic/1466007-resource-descriptive-statistics-in-papyrus/afArray[afArray.Length]	While Case> 0		Case -= 1		If HighestValue < afArray[Case]			HighestValue = http://forums.bethsoft.com/topic/1466007-resource-descriptive-statistics-in-papyrus/afArray[Case]		EndIf	EndWhile	Return HighestValueEndFunctionInt Function Frequency(Float[] afArray, Float afValue) Global	Int Case = afArray.RFind(afValue, afArray.Length - 1)	Int Freq = 0	While Case> 0		Case = afArray.RFind(afValue, Case - 1)		Freq += 1	EndWhile	Return FreqEndFunctionInt[] Function Frequencies(Float[] afArray) Global	Int[] Freqs	Float[] ValueList = Values(afArray)	Int Code = ValueList.Length	While Code > 0		Code -= 1		Freqs[Code] = Frequency(afArray, ValueList[Code])	EndWhile	Return FreqsEndFunctionFloat Function Sum(Float[] afArray) Global	int Case = afArray.Length	float Sum	While Case > 0		Case -= 1		Sum += afArray[Case]	EndWhile	Return SumEndFunctionFloat Function Mode(Float[] afArray) Global	Int[] Freqs = Frequencies(afArray)	Float[] FreqsFloat	Int Code = Freqs.Length	While Code > 0		Code -= 1		FreqsFloat[Code] = Freqs[Code] as Float	EndWhile	Int ModalCat = MaxValue(FreqsFloat) as Int	Return afArray[ModalCat]EndFunctionFloat Function Median(Float[] afArray) Global	Float[] Sorted = Sort(afArray)	Float MedCat = Sorted.Length / 2	If MedCat == Math.Floor(MedCat)		Return Sorted[MedCat as Int]	Else		Return (Sorted[Math.Floor(MedCat)] + Sorted[Math.Ceiling(MedCat)]) / 2	EndIfEndFunctionFloat Function Mean(Float[] afArray) Global	Return (Sum(afArray) / afArray.Length)EndFunctionFloat Function Variance(Float[] afArray) Global	Float Mean = Mean(afArray)	Float SumOfSquares = 0.0	Int Case = afArray.Length	While Case > 0		Case -= 1		SumOfSquares += Math.Pow((afArray[Case] - Mean), 2)	EndWhile	Return SumOfSquares / afArray.LengthEndFunctionFloat Function StandardDeviation(Float[] afArray) Global	Return Math.sqrt(Variance(afArray))EndFunctionFloat Function Moment(Float[] afArray, Float afCenter, Float k) Global	Float SumOfSquares = 0.0	Int Case = afArray.Length	While Case > 0		Case -= 1		SumOfSquares += Math.Pow((afArray[Case] - afCenter), k)	EndWhileEndFunctionFloat Function Covariance(Float[] afArray1, Float[] afArray2) Global	Float Cov	Float MeanX = Mean(afArray1)	Float MeanY = Mean(afArray2)	Float Sum = 0	Int N = Min(afArray1.Length, afArray2.Length) as Int	Int Case = N	While Case > 0		Case -= 1		Sum += ((afArray1[Case] - MeanX) * (afArray2[Case] - MeanY))	EndWhile	Return Sum / NEndFunctionFloat Function Correlation(Float[] afArray1, Float[] afArray2) Global	Return Covariance(afArray1, afArray2) / (StandardDeviation(afArray1) * StandardDeviation(afArray2))EndFunctionFloat Function RSquared(Float[] afArray1, Float[] afArray2) Global	Return Math.Pow(Correlation(afArray1, afArray2), 2)EndFunctionFloat Function OLSBeta(Float[] afX, Float[] afY) Global	Int N = Min(afX.Length, afY.Length) as Int	Return (Covariance(afX, afY) * N) / (Variance(afX) * afX.Length)EndFunctionFloat Function OLSConstant(Float[] afX, Float[] afY) Global	Return Mean(afY) - (Mean(afX) * OLSBeta(afX, afY))EndFunctionFunction Regression(Float[] afX, Float[] afY) Global	Debug.Messagebox(afY + " = " + OLSConstant(afX, afY) + " + " + OLSBeta(afX, afY) + "\nR = " + Correlation(afX, afY) + "\tR2 = " + RSquared(afX, afY))EndFunction
User avatar
Trista Jim
 
Posts: 3308
Joined: Sat Aug 25, 2007 10:39 pm

Return to V - Skyrim