use of suite.primitive.Int_Flt in project suite by stupidsing.
the class VirtualVectorUtil method add.
public VirtualVector add(VirtualVector vv1) {
VirtualVector vv0 = this;
Int_Flt f0 = vv0.get;
Int_Flt f1 = vv1.get;
return VirtualVectorUtil.checkSizes(vv0, vv1, i -> f0.apply(i) + f1.apply(i));
}
use of suite.primitive.Int_Flt in project suite by stupidsing.
the class BollingerBands method meanVariances.
public Pair<float[], float[]> meanVariances(VirtualVector v, int backPos0, int backPos1) {
int length = v.length;
Int_Flt fun = v.get;
float[] means = new float[length];
float[] variances = new float[length];
int d = backPos0 - backPos1;
double il = 1d / d;
int i = 0, j;
double sum = 0d;
double sumSq = 0d;
for (; i < d; i++) {
float f = fun.apply(i);
sum += f;
sumSq += f * f;
}
for (; (j = i + backPos1) < length; i++) {
double mean = sum * il;
means[j] = (float) mean;
variances[j] = (float) (sumSq * il - mean * mean);
float f0 = fun.apply(i - d);
float fx = fun.apply(i);
sum += fx - f0;
sumSq += fx * fx - f0 * f0;
}
return Pair.of(means, variances);
}
use of suite.primitive.Int_Flt in project suite by stupidsing.
the class StatisticalArbitrageTest method testReturnDistribution.
// Naive Bayes return prediction
@Test
public void testReturnDistribution() {
float[] prices = cfg.dataSource(Asset.hsiSymbol).range(period).prices;
int maxTor = 16;
IntObjMap<float[]> differencesByTor = //
Ints_.range(1, //
maxTor).mapIntObj(tor -> {
float[] differences = ts.differences(tor, prices);
Arrays.sort(differences);
return differences;
}).toMap();
for (int tor = 1; tor < maxTor; tor++) System.out.println("tor = " + tor + ", " + stat.moments(differencesByTor.get(tor)));
Int_Flt predictFun = t -> {
double[][] cpsArray = //
Ints_.range(1, //
maxTor).map(tor -> {
float[] differences = differencesByTor.get(tor);
int length = differences.length;
// cumulative probabilities
double[] cps = new double[11];
for (int cpsi = 0, predDiff = -500; predDiff <= 500; cpsi++, predDiff += 100) {
float f = prices[t - 1] + predDiff - prices[t - tor];
int i = 0;
while (i < length && differences[i] < f) i++;
cps[cpsi] = i / (double) length;
}
return cps;
}).toArray(double[].class);
Map<Double, Double> probabilities = new HashMap<>();
for (int cpsi = 0, predDiff = -500; predDiff < 500; cpsi++, predDiff += 100) {
int cpsi_ = cpsi;
double sum = //
Ints_.range(1, //
maxTor).map(//
i -> i).toDouble(Obj_Dbl.sum(tor -> {
double 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().t0.floatValue();
};
for (int t = maxTor + 1; t < prices.length; t++) {
float predicted = prices[t - 1] + predictFun.apply(t);
System.out.println(//
"t = " + t + ", actual = " + //
prices[t] + ", predicted = " + predicted);
}
}
Aggregations