Search in sources :

Example 1 with Dbl_Dbl

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;
}
Also used : Dbl_Obj(suite.primitive.DblPrimitives.Dbl_Obj) Dbl_Dbl(suite.primitive.Dbl_Dbl) Vector(suite.math.linalg.Vector) Matrix(suite.math.linalg.Matrix) DblDbl_Dbl(suite.primitive.DblDbl_Dbl) FiniteDifference(suite.math.FiniteDifference) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Fun(suite.util.FunUtil.Fun) DblDbl_Dbl(suite.primitive.DblDbl_Dbl)

Example 2 with Dbl_Dbl

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;
}
Also used : LngIntPair(suite.primitive.adt.pair.LngIntPair) Outlet(suite.streamlet.Outlet) Read(suite.streamlet.Read) Obj_Flt(suite.primitive.FltPrimitives.Obj_Flt) IntFltPair(suite.primitive.adt.pair.IntFltPair) IntIntSink(suite.primitive.IntIntSink) HashMap(java.util.HashMap) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) String_(suite.util.String_) Dbl_Dbl(suite.primitive.Dbl_Dbl) Map(java.util.Map) Ints_(suite.primitive.Ints_) Valuation(suite.trade.Account.Valuation) Set_(suite.util.Set_) LngFltPair(suite.primitive.adt.pair.LngFltPair) Friends.min(suite.util.Friends.min) Source(suite.util.FunUtil.Source) Set(java.util.Set) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Obj_Int(suite.primitive.IntPrimitives.Obj_Int) Streamlet(suite.streamlet.Streamlet) Eod(suite.trade.data.DataSource.Eod) MathUtil(suite.math.MathUtil) As(suite.streamlet.As) LngFltPair(suite.primitive.adt.pair.LngFltPair) LngIntPair(suite.primitive.adt.pair.LngIntPair) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with Dbl_Dbl

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);
}
Also used : Node(suite.node.Node) Dbl_Dbl(suite.primitive.Dbl_Dbl) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl)

Aggregations

Dbl_Dbl (suite.primitive.Dbl_Dbl)3 Obj_Dbl (suite.primitive.DblPrimitives.Obj_Dbl)2 Fun (suite.util.FunUtil.Fun)2 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 Pair (suite.adt.pair.Pair)1 FiniteDifference (suite.math.FiniteDifference)1 MathUtil (suite.math.MathUtil)1 Matrix (suite.math.linalg.Matrix)1 Vector (suite.math.linalg.Vector)1 Node (suite.node.Node)1 DblDbl_Dbl (suite.primitive.DblDbl_Dbl)1 Dbl_Obj (suite.primitive.DblPrimitives.Dbl_Obj)1 Obj_Flt (suite.primitive.FltPrimitives.Obj_Flt)1 IntIntSink (suite.primitive.IntIntSink)1 Obj_Int (suite.primitive.IntPrimitives.Obj_Int)1 Ints_ (suite.primitive.Ints_)1