private static void BuildHtmlTableRow(string p_tableTitle, string p_rowTitle, bool p_isRowEven, ref MaskItem p_maskItem, StringBuilder p_sb) { string aMeanPerYearRowId = "id" + (p_tableTitle + p_rowTitle).Replace(' ', '_').Replace(',', '_'); string aMeanPerYearCSV = String.Join(", ", p_maskItem.AMeanPerYear.Select(r => r.Item1 + ":" + r.Item2.ToString("#0.000%"))); p_sb.AppendFormat("<tr{0}><td>" + p_rowTitle + "</td>", (p_isRowEven)? " class='even'":""); p_sb.Append("<td>" + p_maskItem.Samples.Count() + "</td>"); p_sb.Append("<td>" + p_maskItem.WinPct.ToString("#0.0%") + "</td>"); //p_sb.AppendFormat(@"<td onclick=""document.getElementById('{0}').style.color = 'red'"">" + p_maskItem.AMean.ToString("#0.000%") + "</td>", aMeanPerYearRowId); //p_sb.AppendFormat(@"<td onclick=""document.getElementById('{0}').style.display = 'table-row'"">" + p_maskItem.AMean.ToString("#0.000%") + @"<button onclick=""document.getElementById('{0}').style.display = 'table-row'"">*</button></td>", aMeanPerYearRowId); //p_sb.AppendFormat(@"<td onclick=""document.getElementById('{0}').style.display = 'table-row'"">" + p_maskItem.AMean.ToString("#0.000%") + @"</td>", aMeanPerYearRowId); //p_sb.AppendFormat(@"<td>" + p_maskItem.AMean.ToString("#0.000%") + @"<a href="""" onclick=""document.getElementById('{0}').style.display = 'table-row'"">*</a></td>", aMeanPerYearRowId); //p_sb.AppendFormat(@"<td onclick=""document.getElementById('{0}').style.display = 'table-row'"">" + p_maskItem.AMean.ToString("#0.000%") + @"<span style=""color: #2581cc; font-size: x-small; vertical-align:super;"">i</span></td>", aMeanPerYearRowId); p_sb.AppendFormat(@"<td{0} onclick=""InvertVisibilityOfTableRow('{1}')"">" + p_maskItem.AMean.ToString("#0.000%") + @"<span style=""color: #2581cc; font-size: x-small; vertical-align:super;"">i</span></td>", (p_maskItem.AMean > 0.0) ? " class='green'" : " class='red'", aMeanPerYearRowId); p_sb.Append("<td>" + p_maskItem.GMean.ToString("#0.000%") + "</td>"); p_sb.Append("<td>" + p_maskItem.Median.ToString("#0.000%") + "</td>"); p_sb.Append("<td>" + p_maskItem.CorrectedStDev.ToString("#0.000%") + "</td>"); p_sb.Append("<td>" + p_maskItem.StandardError.ToString("#0.000%") + "</td>"); p_sb.AppendFormat("<td{0} >" + p_maskItem.TvalueToZero.ToString("#0.00") + "</td>", (p_maskItem.TvalueToZero >= 1.0) ? " class='green'" : ((p_maskItem.TvalueToZero <= -1.0) ? " class='red'" : "")); p_sb.Append("<td>" + p_maskItem.PvalueToZero.ToString("#0.00%") + "</td>"); p_sb.Append("<td>" + ((p_maskItem.PvalueToZero < 0.05)?"Yes":"") + "</td>"); p_sb.Append("<td>" + p_maskItem.TvalueToAMean.ToString("#0.00") + "</td>"); p_sb.Append("<td>" + p_maskItem.PvalueToAMean.ToString("#0.00%") + "</td>"); p_sb.Append("<td>" + ((p_maskItem.PvalueToAMean < 0.05) ? "Yes" : "") + "</td>"); p_sb.Append("</tr>"); p_sb.AppendFormat(@"<tr{0} ID=""{1}"" style=""display:none;""><td colspan=""14"">{2} aMean:{3}</td>", (p_isRowEven) ? " class='even'" : "", aMeanPerYearRowId, p_rowTitle, aMeanPerYearCSV); p_sb.Append("</tr>"); }
private static void CreateBoolMasks(string p_dailyMarketDirectionMaskTotM, string p_dailyMarketDirectionMaskTotMM, out MaskItem[] totMForwardMask, out MaskItem[] totMBackwardMask, out MaskItem[] totMMForwardMask, out MaskItem[] totMMBackwardMask) { totMForwardMask = new MaskItem[30]; // (initialized to null: Neutral, not bullish, not bearish) // trading days; max. 25 is expected. totMBackwardMask = new MaskItem[30]; totMMForwardMask = new MaskItem[30]; totMMBackwardMask = new MaskItem[30]; for (int k = 0; k < 30; k++) { totMForwardMask[k].Samples = new List<Tuple<DateTime, double>>(); totMBackwardMask[k].Samples = new List<Tuple<DateTime, double>>(); totMMForwardMask[k].Samples = new List<Tuple<DateTime, double>>(); totMMBackwardMask[k].Samples = new List<Tuple<DateTime, double>>(); } int iInd = p_dailyMarketDirectionMaskTotM.IndexOf('.'); if (iInd != -1) { for (int i = iInd + 1; i < p_dailyMarketDirectionMaskTotM.Length; i++) { Utils.StrongAssert(i - (iInd + 1) < 30, "Mask half-length length should be less than 30: " + p_dailyMarketDirectionMaskTotM); switch (p_dailyMarketDirectionMaskTotM[i]) { case 'U': totMForwardMask[i - (iInd + 1)].IsBullish = true; break; case 'D': totMForwardMask[i - (iInd + 1)].IsBullish = false; break; case '0': totMForwardMask[i - (iInd + 1)].IsBullish = null; break; default: throw new Exception("Cannot interpret p_dailyMarketDirectionMaskTotM: " + p_dailyMarketDirectionMaskTotM); //break; } } for (int i = iInd - 1; i >= 0; i--) { Utils.StrongAssert((iInd - 1) - i < 30, "Mask half-length length should be less than 30: " + p_dailyMarketDirectionMaskTotM); switch (p_dailyMarketDirectionMaskTotM[i]) { case 'U': totMBackwardMask[(iInd - 1) - i].IsBullish = true; break; case 'D': totMBackwardMask[(iInd - 1) - i].IsBullish = false; break; case '0': totMBackwardMask[(iInd - 1) - i].IsBullish = null; break; default: throw new Exception("Cannot interpret p_dailyMarketDirectionMaskTotM: " + p_dailyMarketDirectionMaskTotM); //break; } } } iInd = p_dailyMarketDirectionMaskTotMM.IndexOf('.'); if (iInd != -1) { for (int i = iInd + 1; i < p_dailyMarketDirectionMaskTotMM.Length; i++) { Utils.StrongAssert(i - (iInd + 1) < 30, "Mask half-length length should be less than 30: " + p_dailyMarketDirectionMaskTotMM); switch (p_dailyMarketDirectionMaskTotMM[i]) { case 'U': totMMForwardMask[i - (iInd + 1)].IsBullish = true; break; case 'D': totMMForwardMask[i - (iInd + 1)].IsBullish = false; break; case '0': totMMForwardMask[i - (iInd + 1)].IsBullish = null; break; default: throw new Exception("Cannot interpret p_dailyMarketDirectionMaskTotMM: " + p_dailyMarketDirectionMaskTotMM); //break; } } for (int i = iInd - 1; i >= 0; i--) { Utils.StrongAssert((iInd - 1) - i < 30, "Mask half-length length should be less than 30: " + p_dailyMarketDirectionMaskTotMM); switch (p_dailyMarketDirectionMaskTotMM[i]) { case 'U': totMMBackwardMask[(iInd - 1) - i].IsBullish = true; break; case 'D': totMMBackwardMask[(iInd - 1) - i].IsBullish = false; break; case '0': totMMBackwardMask[(iInd - 1) - i].IsBullish = null; break; default: throw new Exception("Cannot interpret p_dailyMarketDirectionMaskTotMM: " + p_dailyMarketDirectionMaskTotMM); //break; } } } }
// http://www.danielsoper.com/statcalc3/calc.aspx?id=8 // http://en.wikipedia.org/wiki/Student's_t-test // http://hu.wikipedia.org/wiki/Egymint%C3%A1s_t-pr%C3%B3ba //H0: assumption: real population Mean = 0 // == real daily %change on day T is 0 //H1: real population Mean >0 // because I chose ">", I will use the one-tailed (not two tailed test); I want to know this. //calculate P-value. (with one-tailed Student-distribution) //If P-value < //The decision rule is: Reject H0 if T > 1.645, which is equivalent that P < 5%. //So, I reject H0. //That says. The Population Mean cannot be = 0 (+- Epsilon), because that would be too unlikely that the sample mean is the value that I have for the sample. //Becuse the sample mean is positive. (assume), and we rejected H0, therefore think the real population mean != 0 => it is event less likely that the population mean is negative. //Therefore "the real population mean (of the daily%changes on day T) should be Positive (statistically with 95% probability), so populationMean is significantly > 0" // put this into Tooltip of Significant // Tooltip: "With at least 1-P=95% probability: the real population mean (of the daily%changes on day T) > 0" // "With at least 1-P=95% probability: the real population mean (of the daily%changes on day T) > allDayMean" private static void CalculateSampleStats(ref MaskItem p_maskItem, double p_pctChgTotalAMean) { int nInt = p_maskItem.Samples.Count(); double n = (double)nInt; List<double> samples = p_maskItem.Samples.Select(r => r.Item2).ToList(); double aMean = samples.Average(); double correctedStDev = Math.Sqrt(samples.Sum(r => (r - aMean) * (r - aMean)) / (n - 1.0)); double standardError = correctedStDev / Math.Sqrt(n); p_maskItem.WinPct = (double)samples.Count(r => r > 0) / n; p_maskItem.AMean = aMean; p_maskItem.GMean = samples.GMeanExtendingWithOne(); p_maskItem.Median = samples.Median(); p_maskItem.CorrectedStDev = correctedStDev; p_maskItem.StandardError = standardError; p_maskItem.TvalueToZero = (p_maskItem.AMean - 0.0) / standardError; p_maskItem.TvalueToAMean = (p_maskItem.AMean - p_pctChgTotalAMean) / standardError; if (!Double.IsNaN(p_maskItem.TvalueToZero)) p_maskItem.PvalueToZero = g_statTool.TDistribution(p_maskItem.TvalueToZero, nInt - 1, true); else p_maskItem.PvalueToZero = Double.NaN; if (!Double.IsNaN(p_maskItem.TvalueToAMean)) p_maskItem.PvalueToAMean = g_statTool.TDistribution(p_maskItem.TvalueToAMean, nInt - 1, true); else p_maskItem.PvalueToAMean = Double.NaN; // PvalueToZero = P = probability that the observed aMean result is due to chance p_maskItem.AMeanPerYear = new List<Tuple<string, double>>(); var years = p_maskItem.Samples.Select(r => r.Item1.Year).Distinct().OrderBy(r => r).ToList(); int nYears = years.Count(); int[] nSamplesPerYear = new int[nYears]; double[] aMeanPerYear = new double[nYears]; for (int i = 0; i < n; i++) { int iYears = years.IndexOf(p_maskItem.Samples[i].Item1.Year); nSamplesPerYear[iYears]++; aMeanPerYear[iYears] += p_maskItem.Samples[i].Item2; } for (int i = 0; i < nYears; i++) { aMeanPerYear[i] /= (double)nSamplesPerYear[i]; p_maskItem.AMeanPerYear.Add(new Tuple<string, double>(years[i].ToString() + "(" + nSamplesPerYear[i] + ")", aMeanPerYear[i])); } }