use of suite.math.numeric.Statistic.MeanVariance in project suite by stupidsing.
the class AnalyzeTimeSeriesTest method analyze.
private void analyze(float[] prices) {
int length = prices.length;
int log2 = Quant.log2trunc(length);
double nYears = length * Trade_.invTradeDaysPerYear;
float[] fds = dct.dct(Arrays.copyOfRange(prices, length - log2, length));
float[] returns = ts.returns(prices);
float[] logPrices = To.vector(prices, Math::log);
float[] logReturns = ts.differences(1, logPrices);
MeanVariance rmv = stat.meanVariance(returns);
double variance = rmv.variance;
double kelly = rmv.mean / variance;
IntFltPair max = IntFltPair.of(Integer.MIN_VALUE, Float.MIN_VALUE);
for (int i = 4; i < fds.length; i++) {
float f = Math.abs(fds[i]);
if (max.t1 < f)
max.update(i, f);
}
IntFunction<BuySell> momFun = n -> {
int d0 = 1 + n;
int d1 = 1;
return buySell(d -> Quant.sign(prices[d - d0], prices[d - d1])).start(d0);
};
IntFunction<BuySell> revert = d -> momFun.apply(d).scale(0f, -1f);
IntFunction<BuySell> trend_ = d -> momFun.apply(d).scale(0f, +1f);
BuySell[] reverts = To.array(8, BuySell.class, revert);
BuySell[] trends_ = To.array(8, BuySell.class, trend_);
BuySell tanh = buySell(d -> Tanh.tanh(3.2d * reverts[1].apply(d)));
float[] holds = mt.hold(prices, 1f, 1f, 1f);
float[] ma200 = ma.movingAvg(prices, 200);
BuySell mat = buySell(d -> {
int last = d - 1;
return Quant.sign(ma200[last], prices[last]);
}).start(1).longOnly();
BuySell mt_ = buySell(d -> holds[d]);
Pair<float[], float[]> bbmv = bb.meanVariances(VirtualVector.of(logReturns), 9, 0);
float[] bbmean = bbmv.t0;
float[] bbvariances = bbmv.t1;
BuySell ms2 = buySell(d -> {
int last = d - 1;
int ref = last - 250;
float mean = bbmean[last];
return Quant.sign(logPrices[last], logPrices[ref] - bbvariances[last] / (2d * mean * mean));
}).start(1 + 250);
LogUtil.info(//
"" + "\nsymbol = " + //
symbol + "\nlength = " + //
length + "\nnYears = " + //
nYears + "\nups = " + //
Floats_.of(returns).filter(return_ -> 0f <= return_).size() + "\ndct period = " + //
max.t0 + //
Ints_.range(//
10).map(//
d -> "\ndct component [" + d + "d] = " + fds[d]).collect(//
As::joined) + "\nreturn yearly sharpe = " + //
rmv.mean / Math.sqrt(variance / nYears) + "\nreturn kelly = " + //
kelly + "\nreturn skew = " + //
stat.skewness(returns) + "\nreturn kurt = " + //
stat.kurtosis(returns) + //
Ints_.of(1, 2, 4, 8, 16, //
32).map(//
d -> "\nmean reversion ols [" + d + "d] = " + ts.meanReversion(prices, d).coefficients[0]).collect(//
As::joined) + //
Ints_.of(4, //
16).map(//
d -> "\nvariance ratio [" + d + "d over 1d] = " + ts.varianceRatio(prices, d)).collect(//
As::joined) + "\nreturn hurst = " + //
ts.hurst(prices, prices.length / 2) + "\nhold " + //
buySell(d -> 1d).invest(prices) + "\nkelly " + //
buySell(d -> kelly).invest(prices) + "\nma200 trend " + //
mat.invest(prices) + //
Ints_.range(1, //
8).map(//
d -> "\nrevert [" + d + "d] " + reverts[d].invest(prices)).collect(//
As::joined) + //
Ints_.range(1, //
8).map(//
d -> "\ntrend_ [" + d + "d] " + trends_[d].invest(prices)).collect(//
As::joined) + //
Ints_.range(1, //
8).map(//
d -> "\nrevert [" + d + "d] long-only " + reverts[d].longOnly().invest(prices)).collect(//
As::joined) + //
Ints_.range(1, //
8).map(//
d -> "\ntrend_ [" + d + "d] long-only " + trends_[d].longOnly().invest(prices)).collect(//
As::joined) + "\nms2 " + //
ms2.invest(prices) + "\nms2 long-only " + //
ms2.longOnly().invest(prices) + "\ntanh " + //
tanh.invest(prices) + "\ntimed " + //
mt_.invest(prices) + "\ntimed long-only " + mt_.longOnly().invest(prices));
}
use of suite.math.numeric.Statistic.MeanVariance in project suite by stupidsing.
the class TimeSeries method hurstFwf.
// http://www.financialwisdomforum.org/gummy-stuff/hurst.htm
public double hurstFwf(float[] ys, int tor) {
float[] logys = To.vector(ys, Math::log);
float[] returns0 = dropDiff_(1, logys);
int length = returns0.length;
List<FltObjPair<float[]>> pairs = new ArrayList<>();
for (int n = 0; n < length * 3 / 4; n++) {
float[] returns = Arrays.copyOfRange(returns0, n, length);
MeanVariance mv = stat.meanVariance(returns);
double mean = mv.mean;
float[] devs = To.vector(returns, r -> r - mean);
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
double sum = 0d;
for (float dev : devs) {
sum += dev;
min = min(sum, min);
max = max(sum, max);
}
double x = Math.log(returns.length);
double y = (max - min) / mv.standardDeviation();
pairs.add(FltObjPair.of((float) y, new float[] { (float) x, 1f }));
}
return stat.linearRegression(Read.from(pairs)).coefficients[0];
}
Aggregations