def summarize(results): #print(dumps(results, indent = 2)) precision = 3 std = NormalDist() alpha = 0.01 bad_records = 0 summary = {"mae": [], "mfe": [], "pnl": [], "duration": []} operations = ["mae", "mfe", "pnl"] for record in results: fns = get_summary_ops(record["type"]) for i in range(len(operations)): try: summary[operations[i]].append(fns[i](record)) except (ZeroDivisionError, KeyError): bad_records += 1 continue summary["duration"].append(record["end_index"] - record["start_index"]) #pyplot.hist(summary["mae"], bins = 100, range = (-10, 10)) #pyplot.show() summary["mae"].sort() summary["mfe"].sort() summary["pnl"].sort() summary["duration"].sort() summary["mae"] = trim_outliers(summary["mae"]) summary["mfe"] = trim_outliers(summary["mfe"]) summary["pnl"] = trim_outliers(summary["pnl"]) summary["duration"] = trim_outliers(summary["duration"]) res = { "test": { "group": results[0]["group"], "type": results[0]["type"], "enter": results[0]["enter"], "exit": results[0]["exit"], "samples": len(results) }, "mae": { "mean": round(mean(summary["mae"]), precision), "stdev": round(stdev(summary["mae"]), precision) }, "mfe": { "mean": round(mean(summary["mfe"]), precision), "stdev": round(stdev(summary["mfe"]), precision) }, "mfe/mae": { "mean": round(mean(summary["mfe"]) / mean(summary["mae"]), precision) }, "pnl": { "mean": round(mean(summary["pnl"]), precision), "stdev": round(stdev(summary["pnl"]), precision) }, "duration": { "mean": round(mean(summary["duration"]), precision), "stdev": round(stdev(summary["duration"]), precision) }, "excluded_records": bad_records } for statistic in ["mae", "mfe", "pnl"]: E = std.zscore(alpha / 2) * res[statistic]["stdev"] / sqrt( res["test"]["samples"]) mu = res[statistic]["mean"] res[statistic][f"interval"] = { "lower": round(mu - E, 2 * precision), "upper": round(mu + E, 2 * precision), "alpha": 1 - alpha } return res
def z_normalize(data): """ Perform a z-normalization on the data """ dist = NormalDist(mean(data), stdev(data)) return [dist.zscore(i) for i in data]