use of suite.primitive.Floats_ in project suite by stupidsing.
the class Arima method armaBackcast.
// http://math.unice.fr/~frapetti/CorsoP/Chapitre_4_IMEA_1.pdf
// "Least squares estimation using backcasting procedure"
public Arima_ armaBackcast(float[] xs, float[] ars, float[] mas) {
int length = xs.length;
int p = ars.length;
int q = mas.length;
float[] xsp = Floats_.concat(new float[p], xs);
float[] epq = new float[length + q];
Arma arma = new Arma(ars, mas);
for (int iter = 0; iter < 64; iter++) {
// backcast
// ep[t]
// = (xs[t + q] - ep[t + q]
// - ars[0] * xs[t + q - 1] - ... - ars[p - 1] * xs[t + q - p]
// - mas[0] * ep[t + q - 1] - ... - mas[q - 2] * ep[t + 1]
// ) / mas[q - 1]
arma.backcast(xsp, epq);
// forward recursion
// ep[t] = xs[t]
// - ars[0] * xs[t - 1] - ... - ars[p - 1] * xs[t - p]
// - mas[0] * ep[t - 1] - ... - mas[q - 1] * ep[t - q]
double error = arma.forwardRecursion(xsp, epq);
// minimization
// xs[t]
// = ars[0] * xs[t - 1] + ... + ars[p - 1] * xs[t - p]
// + mas[0] * ep[t - 1] + ... + mas[q - 1] * ep[t - q]
// + ep[t]
LinearRegression lr = stat.linearRegression(//
Ints_.range(//
length).map(t -> {
int tp = t + p, tpm1 = tp - 1;
int tq = t + q, tqm1 = tq - 1;
FltStreamlet lrxs0 = Ints_.range(p).collect(Int_Flt.lift(i -> xsp[tpm1 - i]));
FltStreamlet lrxs1 = Ints_.range(q).collect(Int_Flt.lift(i -> epq[tqm1 - i]));
return FltObjPair.of(xsp[tp], Floats_.concat(lrxs0, lrxs1).toArray());
}));
System.out.println("iter " + iter + ", error = " + To.string(error) + lr);
System.out.println();
float[] coefficients = lr.coefficients();
Floats_.copy(coefficients, 0, ars, 0, p);
Floats_.copy(coefficients, p, mas, 0, q);
}
double x1 = arma.sum(xsp, epq);
return new Arima_(ars, mas, (float) x1);
}
use of suite.primitive.Floats_ in project suite by stupidsing.
the class Arima method armaIa.
// extended from
// "High Frequency Trading - A Practical Guide to Algorithmic Strategies and
// Trading Systems", Irene Aldridge, page 100
// xs[t]
// = ars[0] * xs[t - 1] + ... + ars[p - 1] * xs[t - p]
// + ep[t]
// + mas[0] * ep[t - 1] + ... + mas[q - 1] * ep[t - q]
private Arima_ armaIa(float[] xs, int p, int q) {
int length = xs.length;
int lengthp = length + p, lengthpm1 = lengthp - 1;
int lengthq = length + q, lengthqm1 = lengthq - 1;
int iter = 0;
float[] xsp = new float[lengthp];
float[][] epqByIter = new float[q][];
Arrays.fill(xsp, 0, p, xs[0]);
System.arraycopy(xs, 0, xsp, p, length);
while (true) {
int iter_ = iter;
LinearRegression lr = stat.linearRegression(//
Ints_.range(//
length).map(t -> {
int tp = t + p;
int tq = t + q, tqm1 = tq - 1;
float[] lrxs = //
Floats_.concat(Floats_.reverse(xsp, t, tp), //
Ints_.range(iter_).collect(Int_Flt.lift(i -> epqByIter[i][tqm1 - i]))).toArray();
return FltObjPair.of(xsp[tp], lrxs);
}));
float[] coeffs = lr.coefficients();
if (iter < q)
System.arraycopy(lr.residuals, 0, epqByIter[iter++] = new float[lengthq], q, length);
else {
float[] ars = Floats.of(coeffs, 0, p).toArray();
float[] mas = Floats.of(coeffs, p).toArray();
double x1 = //
0d + //
Ints_.range(p).toDouble(Int_Dbl.sum(i -> ars[i] * xsp[lengthpm1 - i])) + Ints_.range(q).toDouble(Int_Dbl.sum(i -> mas[i] * epqByIter[i][lengthqm1 - i]));
return new Arima_(ars, mas, (float) x1);
}
}
}
use of suite.primitive.Floats_ 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.Floats_ 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.Floats_ in project suite by stupidsing.
the class Render method render.
public Image render(int width, int height, BiFun<Float, R3> f) {
float scale = 1f / max(width, height);
int centerX = width / 2, centerY = height / 2;
float[] xs = Floats_.toArray(width + 1, x -> (x - centerX) * scale);
float[] ys = Floats_.toArray(height + 1, y -> (y - centerY) * scale);
return renderPixels(width, height, (IntInt_Obj<R3>) (x, y) -> {
R3 color;
try {
color = f.apply(xs[x], ys[y]);
} catch (Exception ex) {
LogUtil.error(new RuntimeException("at (" + x + ", " + y + ")", ex));
color = new R3(1d, 1d, 1d);
}
return color;
});
}
Aggregations