use of primal.primitive.Int_Flt in project suite by stupidsing.
the class StatisticalArbitrageTest method testReturnDistribution.
// Naive Bayes return prediction
@Test
public void testReturnDistribution() {
var prices = cfg.dataSource(Instrument.hsiSymbol).range(period).prices;
var maxTor = 16;
var differencesByTor = //
forInt(1, maxTor).mapIntObj(tor -> {
var differences = ts.differences(tor, prices);
Arrays.sort(differences);
return differences;
}).toMap();
for (var tor = 1; tor < maxTor; tor++) System.out.println("tor = " + tor + ", " + stat.moments(differencesByTor.get(tor)));
Int_Flt predictFun = t -> {
var cpsArray = //
forInt(1, maxTor).map(tor -> {
var differences = differencesByTor.get(tor);
var length = differences.length;
// cumulative probabilities
var cps = new double[11];
for (int cpsi = 0, predDiff = -500; predDiff <= 500; cpsi++, predDiff += 100) {
var f = prices[t - 1] + predDiff - prices[t - tor];
var i = 0;
while (i < length && differences[i] < f) i++;
cps[cpsi] = i / (double) length;
}
return cps;
}).toArray(double[].class);
var probabilities = new HashMap<Double, Double>();
for (int cpsi = 0, predDiff = -500; predDiff < 500; cpsi++, predDiff += 100) {
var cpsi_ = cpsi;
var sum = //
forInt(1, maxTor).map(//
i -> i).toDouble(AsDbl.sum(tor -> {
var probability = cpsArray[tor - 1][cpsi_ + 1] - cpsArray[tor - 1][cpsi_];
return 1d / probability;
}));
probabilities.put(predDiff + 100d / 2d, sum);
}
return //
Read.from2(//
probabilities).sortByValue(//
(p0, p1) -> Double.compare(p1, p0)).first().k.floatValue();
};
for (var t = maxTor + 1; t < prices.length; t++) {
var predicted = prices[t - 1] + predictFun.apply(t);
System.out.println(//
"t = " + t + ", actual = " + //
prices[t] + ", predicted = " + predicted);
}
}
Aggregations