use of primal.primitive.Dbl_Dbl in project suite by stupidsing.
the class Bfgs method lineSearch.
private double lineSearch(Dbl_Dbl phi, Dbl_Dbl phiGradient, double alphax) {
var c1 = .0001d;
var c2 = .1d;
var alpha0 = 0d;
var v0 = phi.apply(alpha0);
var 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 (var iter = 0; iter < 16; iter++) {
var alpha = interpolate.apply(alphaLo, alphaHi);
var v = phi.apply(alpha);
double g;
if (v0 + c1 * alpha * g0 < v || phi.apply(alphaLo) <= v)
alphaHi = alpha;
else if (abs(g = phiGradient.apply(alpha)) <= -c2 * g0)
return alpha;
else {
if (0d <= g * (alphaHi - alphaLo))
alphaHi = alphaLo;
alphaLo = alpha;
}
}
return alphaLo;
};
var alphap = alpha0;
var vp = v0;
var alpha = choose.apply(alphap, alphax);
for (var iter = 0; iter < 16; iter++) {
var v = phi.apply(alpha);
if (v0 + c1 * alpha * g0 < v || 0 < iter && vp <= v)
return zoom.apply(alphap, alpha);
var g = phiGradient.apply(alpha);
if (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 primal.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) {
var sum = 0f;
for (var pair : Read.fromMultimap(trades.toMultimap(trade -> trade.symbol))) {
var dividends = fun.apply(pair.k);
var puller = Puller.of(pair.v);
LngIntPair tn = LngIntPair.of(0l, 0);
Source<LngIntPair> tradeSource = () -> {
var trade = puller.pull();
var 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));
};
var tn1 = tradeSource.g();
for (var dividend : dividends) {
while (tn1 != null && tn1.t0 < dividend.t0) {
tn.update(tn1.t0, tn1.t1);
tn1 = tradeSource.g();
}
var amount = tn.t1 * dividend.t1;
sum += amount - feeFun.apply(amount);
}
}
return sum;
}
Aggregations