Hi guys!
I am experimenting wit Accord: http://accord-framework.net/
This is what I have for now: ShapiroWilkTest and TTest.
But my statistics knowledge are limited. So please tell me exactly what to do.
Thanks.
public object Run(double hypothesizedMean)
{
foreach (long systemId in systems)
{
Series<DateTime, Double> returns = timesheet.GetColumnAsDouble(systemId, EquityType.Rets);
// --------------------------------------------------------------
// All data
// --------------------------------------------------------------
double[] allData = returns.Values.ToArray();
Debug.WriteLine($"-------- SystemId = {systemId}, HypothesizedMean = {hypothesizedMean}, all data - {allData.Length} datapoints-------------");
// --------------------------------------------------------------
// Let's try a normality test
// --------------------------------------------------------------
ShapiroWilkTest shapiroWilkTest_AllData = new ShapiroWilkTest(allData);
Debug.WriteLine(
$"Strategy {systemId}: shapiroWilkTest_AllData: PValue = {shapiroWilkTest_AllData.PValue} " +
$" The null hypothesis 'sample came from a normally distributed population' should be rejected: {shapiroWilkTest_AllData.Significant.ToString().ToUpper()}");
// --------------------------------------------------------------
// TTest
// --------------------------------------------------------------
TTest tt_all_data = new TTest(allData, hypothesizedMean, OneSampleHypothesis.ValueIsDifferentFromHypothesis);
Debug.WriteLine($"Strategy {systemId}: TTest - all data: hypothesizedMean = {hypothesizedMean} PValue = {tt_all_data.PValue} Significant = {tt_all_data.Significant}");
// ==============================================================
// Non-zero data only
// ==============================================================
double[] nonZeroData = returns.Values.Where(x => Math.Abs(x) > 1E-6).ToArray();
Debug.WriteLine($"-------- SystemId = {systemId}, HypothesizedMean = {hypothesizedMean}, non-zero data - {nonZeroData.Length} datapoints -------------");
// --------------------------------------------------------------
// Lets try a normality test
// --------------------------------------------------------------
ShapiroWilkTest shapiroWilkTest_nonZeroData = new ShapiroWilkTest(nonZeroData);
Debug.WriteLine(
$"Strategy {systemId}: shapiroWilkTest_nonZeroData: PValue = {shapiroWilkTest_nonZeroData.PValue} " +
$" The null hypothesis 'sample came from a normally distributed population' should be rejected: {shapiroWilkTest_nonZeroData.Significant.ToString().ToUpper()}");
// --------------------------------------------------------------
// TTest
// --------------------------------------------------------------
TTest tt_non_zero_data = new TTest(nonZeroData, hypothesizedMean, OneSampleHypothesis.ValueIsDifferentFromHypothesis);
Debug.WriteLine($"Strategy {systemId}: TTest - non-zero data: hypothesizedMean = {hypothesizedMean} PValue = {tt_non_zero_data.PValue} Significant = {tt_non_zero_data.Significant}");
}
return null; // return nothing for now
}
}
Results:
-------- SystemId = 84372841, HypothesizedMean = 0, all data - 2067 datapoints-------------
Strategy 84372841: shapiroWilkTest_AllData: PValue = 7.17465290505463E-53 The null hypothesis ‘sample came from a normally distributed population’ should be rejected: TRUE
Strategy 84372841: TTest - all data: hypothesizedMean = 0 PValue = 0.0159801171634086 Significant = True
-------- SystemId = 84372841, HypothesizedMean = 0, non-zero data - 1073 datapoints -------------
Strategy 84372841: shapiroWilkTest_nonZeroData: PValue = 6.06904288872399E-29 The null hypothesis ‘sample came from a normally distributed population’ should be rejected: TRUE
Strategy 84372841: TTest - non-zero data: hypothesizedMean = 0 PValue = 0.0159468184756832 Significant = True
-------- SystemId = 75800796, HypothesizedMean = 0, all data - 2067 datapoints-------------
Strategy 75800796: shapiroWilkTest_AllData: PValue = 2.0954101771429E-40 The null hypothesis ‘sample came from a normally distributed population’ should be rejected: TRUE
Strategy 75800796: TTest - all data: hypothesizedMean = 0 PValue = 0.00302050931792452 Significant = True
-------- SystemId = 75800796, HypothesizedMean = 0, non-zero data - 1511 datapoints -------------
Strategy 75800796: shapiroWilkTest_nonZeroData: PValue = 3.73291914931055E-26 The null hypothesis ‘sample came from a normally distributed population’ should be rejected: TRUE
Strategy 75800796: TTest - non-zero data: hypothesizedMean = 0 PValue = 0.00301297568152137 Significant = True
===================================================================================
-------- SystemId = 84372841, HypothesizedMean = 10, all data - 2067 datapoints-------------
Strategy 84372841: shapiroWilkTest_AllData: PValue = 7.17465290505463E-53 The null hypothesis ‘sample came from a normally distributed population’ should be rejected: TRUE
Strategy 84372841: TTest - all data: hypothesizedMean = 10 PValue = 2.22044604925031E-16 Significant = True
-------- SystemId = 84372841, HypothesizedMean = 10, non-zero data - 1073 datapoints -------------
Strategy 84372841: shapiroWilkTest_nonZeroData: PValue = 6.06904288872399E-29 The null hypothesis ‘sample came from a normally distributed population’ should be rejected: TRUE
Strategy 84372841: TTest - non-zero data: hypothesizedMean = 10 PValue = 2.22044604925031E-16 Significant = True
-------- SystemId = 75800796, HypothesizedMean = 10, all data - 2067 datapoints-------------
Strategy 75800796: shapiroWilkTest_AllData: PValue = 2.0954101771429E-40 The null hypothesis ‘sample came from a normally distributed population’ should be rejected: TRUE
Strategy 75800796: TTest - all data: hypothesizedMean = 10 PValue = 2.22044604925031E-16 Significant = True
-------- SystemId = 75800796, HypothesizedMean = 10, non-zero data - 1511 datapoints -------------
Strategy 75800796: shapiroWilkTest_nonZeroData: PValue = 3.73291914931055E-26 The null hypothesis ‘sample came from a normally distributed population’ should be rejected: TRUE
Strategy 75800796: TTest - non-zero data: hypothesizedMean = 10 PValue = 2.22044604925031E-16 Significant = True
==============================
The above code would be converted to something like this in C2Explorer:
long[] systems = new long[] { 84372841, 75800796 };
StatisticsTesting statisticsTesting = new StatisticsTesting(db, systems);
TABLE = statisticsTesting.Run(0.0);
TABLE = statisticsTesting.Run(10.0);