use of suite.primitive.Ints_ in project suite by stupidsing.
the class StatisticalArbitrageTest method testAutoRegressivePowersOfTwo.
@Test
public void testAutoRegressivePowersOfTwo() {
int power = 6;
DataSource ds = cfg.dataSource(Asset.hsiSymbol).cleanse();
float[] prices = ds.prices;
float[][] mas = To.array(power, float[].class, p -> ma.movingAvg(prices, 1 << p));
float[] returns = ts.returns(prices);
LinearRegression lr = stat.linearRegression(//
Ints_.range(1 << power, //
prices.length).map(i -> FltObjPair.of(returns[i], Floats_.toArray(power, p -> mas[p][i - (1 << p)]))));
System.out.println(lr);
}
use of suite.primitive.Ints_ 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);
}
}
use of suite.primitive.Ints_ in project suite by stupidsing.
the class StatisticalArbitrageTest method testCointegration.
// Auto-regressive test
@Test
public void testCointegration() {
// 0004.HK, 0020.HK
// 0011.HK, 0005.HK
int tor = 8;
String symbol0 = "0004.HK";
String symbol1 = "0945.HK";
AlignKeyDataSource<String> akds = cfg.dataSources(period, Read.each(symbol0, symbol1));
Map<String, float[]> pricesBySymbol = akds.dsByKey.mapValue(DataSource::returns).toMap();
int length = akds.ts.length;
float[] prices0 = pricesBySymbol.get(symbol0);
float[] prices1 = pricesBySymbol.get(symbol1);
LinearRegression lr = stat.linearRegression(//
Ints_.range(tor, //
length).map(i -> FltObjPair.of(prices1[i], Floats_.toArray(tor, j -> prices0[i + j - tor]))));
System.out.println(lr);
}
use of suite.primitive.Ints_ in project suite by stupidsing.
the class Render method renderPixels.
public Image renderPixels(int width, int height, IntInt_Obj<R3> f) {
int nThreads = Constants.nThreads;
int[] txs = Ints_.toArray(nThreads + 1, i -> width * i / nThreads);
R3[][] pixels = new R3[width][height];
List<Thread> threads = //
Ints_.range(//
nThreads).map(t -> Thread_.newThread(() -> {
for (int x = txs[t]; x < txs[t + 1]; x++) for (int y = 0; y < height; y++) pixels[x][y] = f.apply(x, y);
})).toList();
Thread_.startJoin(threads);
Image image = new Image(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) {
R3 pixel = limit(pixels[x][y]);
image.setRGB(x, y, new Color(pixel.x, pixel.y, pixel.z).getRGB());
}
return image;
}
Aggregations