Search in sources :

Example 1 with Dbl_Dbl

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;
}
Also used : Obj_Dbl(primal.primitive.DblPrim.Obj_Dbl) Vector(suite.math.linalg.Vector) Fun(primal.fp.Funs.Fun) Matrix(suite.math.linalg.Matrix) Dbl_Obj(primal.primitive.DblPrim.Dbl_Obj) Math.abs(java.lang.Math.abs) FiniteDifference(suite.math.FiniteDifference) DblDbl_Dbl(primal.primitive.DblDbl_Dbl) Dbl_Dbl(primal.primitive.Dbl_Dbl) DblDbl_Dbl(primal.primitive.DblDbl_Dbl)

Example 2 with Dbl_Dbl

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;
}
Also used : Union(primal.Verbs.Union) Math.log1p(java.lang.Math.log1p) Fun(primal.fp.Funs.Fun) Math_(suite.math.Math_) HashMap(java.util.HashMap) LngFltPair(primal.primitive.adt.pair.LngFltPair) Math.abs(java.lang.Math.abs) Obj_Flt(primal.primitive.FltPrim.Obj_Flt) Dbl_Dbl(primal.primitive.Dbl_Dbl) Streamlet_.forInt(suite.util.Streamlet_.forInt) ArrayList(java.util.ArrayList) Streamlet(primal.streamlet.Streamlet) Map(java.util.Map) Math.expm1(java.lang.Math.expm1) Valuation(suite.trade.Account.Valuation) Pair(primal.adt.Pair) IntFltPair(primal.primitive.adt.pair.IntFltPair) IntIntSink(primal.primitive.IntIntSink) Source(primal.fp.Funs.Source) Set(java.util.Set) Math.min(java.lang.Math.min) Read(primal.MoreVerbs.Read) LngIntPair(primal.primitive.adt.pair.LngIntPair) List(java.util.List) Eod(suite.trade.data.DataSource.Eod) AsInt(primal.primitive.fp.AsInt) Math.max(java.lang.Math.max) Math.floor(java.lang.Math.floor) Equals(primal.Verbs.Equals) Puller(primal.puller.Puller) LngIntPair(primal.primitive.adt.pair.LngIntPair)

Aggregations

Math.abs (java.lang.Math.abs)2 Fun (primal.fp.Funs.Fun)2 Dbl_Dbl (primal.primitive.Dbl_Dbl)2 Math.expm1 (java.lang.Math.expm1)1 Math.floor (java.lang.Math.floor)1 Math.log1p (java.lang.Math.log1p)1 Math.max (java.lang.Math.max)1 Math.min (java.lang.Math.min)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Read (primal.MoreVerbs.Read)1 Equals (primal.Verbs.Equals)1 Union (primal.Verbs.Union)1 Pair (primal.adt.Pair)1 Source (primal.fp.Funs.Source)1 DblDbl_Dbl (primal.primitive.DblDbl_Dbl)1 Dbl_Obj (primal.primitive.DblPrim.Dbl_Obj)1