use of suite.primitive.Dbl_Dbl in project suite by stupidsing.
the class Bfgs method lineSearch.
private double lineSearch(Dbl_Dbl phi, Dbl_Dbl phiGradient, double alphax) {
double c1 = .0001d;
double c2 = .1d;
double alpha0 = 0d;
double v0 = phi.apply(alpha0);
double g0 = phiGradient.apply(alpha0);
// TODO
DblDbl_Dbl interpolate = (a0, a1) -> (a0 + a1) * .5d;
// TODO
DblDbl_Dbl choose = (a0, a1) -> (a0 + a1) * .5d;
DblDbl_Dbl zoom = (alphaLo, alphaHi) -> {
for (int iter = 0; iter < 16; iter++) {
double alpha = interpolate.apply(alphaLo, alphaHi);
double v = phi.apply(alpha);
double g;
if (v0 + c1 * alpha * g0 < v || phi.apply(alphaLo) <= v)
alphaHi = alpha;
else if (Math.abs(g = phiGradient.apply(alpha)) <= -c2 * g0)
return alpha;
else {
if (0d <= g * (alphaHi - alphaLo))
alphaHi = alphaLo;
alphaLo = alpha;
}
}
return alphaLo;
};
double alphap = alpha0;
double vp = v0;
double alpha = choose.apply(alphap, alphax);
for (int iter = 0; iter < 16; iter++) {
double v = phi.apply(alpha);
if (v0 + c1 * alpha * g0 < v || 0 < iter && vp <= v)
return zoom.apply(alphap, alpha);
double g = phiGradient.apply(alpha);
if (Math.abs(g) <= -c2 * g0)
break;
else if (0d <= g)
return zoom.apply(alpha, alphap);
else {
alphap = alpha;
vp = v;
alpha = choose.apply(alpha, alphax);
}
}
return alpha;
}
use of suite.primitive.Dbl_Dbl in project suite by stupidsing.
the class Trade_ method dividend.
public static float dividend(Streamlet<Trade> trades, Fun<String, LngFltPair[]> fun, Dbl_Dbl feeFun) {
float sum = 0f;
for (Pair<String, List<Trade>> pair : trades.toMultimap(trade -> trade.symbol).listEntries()) {
LngFltPair[] dividends = fun.apply(pair.t0);
Outlet<Trade> outlet = Outlet.of(pair.t1);
LngIntPair tn = LngIntPair.of(0l, 0);
Source<LngIntPair> tradeSource = () -> {
Trade trade = outlet.next();
long t = trade != null ? Time.of(trade.date + " 12:00:00").epochSec(8) : Long.MAX_VALUE;
return LngIntPair.of(t, tn.t1 + (trade != null ? trade.buySell : 0));
};
LngIntPair tn1 = tradeSource.source();
for (LngFltPair dividend : dividends) {
while (tn1 != null && tn1.t0 < dividend.t0) {
tn.update(tn1.t0, tn1.t1);
tn1 = tradeSource.source();
}
float amount = tn.t1 * dividend.t1;
sum += amount - feeFun.apply(amount);
}
}
return sum;
}
use of suite.primitive.Dbl_Dbl in project suite by stupidsing.
the class GaussNewton method sym.
public float[] sym(Node[] vars, Node[] rs, float[] initials) {
int nVars = vars.length;
int nrs = rs.length;
@SuppressWarnings("unchecked") Obj_Dbl<float[]>[] residualFuns = Array_.newArray(Obj_Dbl.class, nrs);
Dbl_Dbl[][] gradientFuns = new Dbl_Dbl[nrs][nVars];
for (int i = 0; i < nrs; i++) {
Node r = rs[i];
residualFuns[i] = sym.fun(r, vars);
for (int j = 0; j < nVars; j++) {
Node var = vars[j];
gradientFuns[i][j] = sym.fun(sym.d(var, r), var);
}
}
return gn(r -> {
float[] residuals = new float[nrs];
for (int i = 0; i < nrs; i++) residuals[i] = (float) residualFuns[i].apply(r);
return residuals;
}, betas -> {
float[][] jacobian = new float[nrs][nVars];
for (int i = 0; i < nrs; i++) for (int j = 0; j < nVars; j++) jacobian[i][j] = (float) gradientFuns[i][j].apply(betas[j]);
return jacobian;
}, initials);
}
Aggregations