Search in sources :

Example 11 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class Rewrite_ method map.

public static Node map(Node node, Iterate<Node> fun) {
    NodeRead nr = NodeRead.of(node);
    List<Pair<Node, Node>> children1 = new ArrayList<>();
    boolean isSame = true;
    for (Pair<Node, Node> pair : nr.children) {
        Node child0 = pair.t1;
        Node childx = fun.apply(child0);
        if (child0 != childx) {
            isSame = false;
            children1.add(Pair.of(pair.t0, childx));
        } else
            children1.add(pair);
    }
    if (isSame)
        return node;
    else
        return new NodeWrite(nr.type, nr.terminal, nr.op, children1).node;
}
Also used : Node(suite.node.Node) ArrayList(java.util.ArrayList) Pair(suite.adt.pair.Pair)

Example 12 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class BackAllocatorUtil method stop.

public default BackAllocator stop(double stopLoss, double stopGain) {
    return (akds, indices) -> {
        OnDateTime onDateTime = allocate(akds, indices);
        Map<String, DataSource> dsBySymbol = akds.dsByKey.toMap();
        Mutable<Map<String, Double>> mutable = Mutable.of(new HashMap<>());
        Map<String, List<DblFltPair>> entriesBySymbol = new HashMap<>();
        return index -> {
            int last = index - 1;
            List<Pair<String, Double>> potentialBySymbol = onDateTime.onDateTime(index);
            Map<String, Double> potentialBySymbol0 = mutable.get();
            Map<String, Double> potentialBySymbol1 = Read.from2(potentialBySymbol).toMap();
            // find out the transactions
            Map<String, Double> diffBySymbol = // 
            Read.from(// 
            Set_.union(potentialBySymbol0.keySet(), potentialBySymbol1.keySet())).map2(symbol -> {
                double potential0 = potentialBySymbol0.getOrDefault(symbol, 0d);
                double potential1 = potentialBySymbol1.getOrDefault(symbol, 0d);
                return potential1 - potential0;
            }).toMap();
            // check on each stock symbol
            for (Entry<String, Double> e : diffBySymbol.entrySet()) {
                String symbol = e.getKey();
                double diff = e.getValue();
                int bs = Quant.sign(diff);
                float price = dsBySymbol.get(symbol).prices[last];
                List<DblFltPair> entries0 = entriesBySymbol.getOrDefault(symbol, new ArrayList<>());
                List<DblFltPair> entries1 = new ArrayList<>();
                Collections.sort(entries0, (pair0, pair1) -> -bs * Float.compare(pair0.t1, pair1.t1));
                for (DblFltPair entry0 : entries0) {
                    double potential0 = entry0.t0;
                    float entryPrice = entry0.t1;
                    double cancellation;
                    // a recent buy would cancel out the lowest price sell
                    if (bs == -1)
                        cancellation = min(0, max(diff, -potential0));
                    else if (bs == 1)
                        cancellation = max(0, min(diff, -potential0));
                    else
                        cancellation = 0d;
                    double potential1 = potential0 + cancellation;
                    diff -= cancellation;
                    double min = entryPrice * (potential1 < 0 ? stopGain : stopLoss);
                    double max = entryPrice * (potential1 < 0 ? stopLoss : stopGain);
                    // drop entries that got past their stopping prices
                    if (min < price && price < max)
                        entries1.add(DblFltPair.of(potential1, entryPrice));
                }
                if (diff != 0d)
                    entries1.add(DblFltPair.of(diff, price));
                entriesBySymbol.put(symbol, entries1);
            }
            mutable.update(potentialBySymbol1);
            // re-assemble the entries into current profile
            return // 
            Read.multimap(// 
            entriesBySymbol).groupBy(// 
            entries -> entries.toDouble(Obj_Dbl.sum(pair -> pair.t0))).toList();
        };
    };
}
Also used : Arrays(java.util.Arrays) Read(suite.streamlet.Read) Trade_(suite.trade.Trade_) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) Mutable(suite.adt.Mutable) DblDbl_Dbl(suite.primitive.DblDbl_Dbl) HashMap(java.util.HashMap) Deque(java.util.Deque) IntPredicate(java.util.function.IntPredicate) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) DblFltPair(suite.primitive.adt.pair.DblFltPair) String_(suite.util.String_) Map(java.util.Map) Set_(suite.util.Set_) Streamlet2(suite.streamlet.Streamlet2) Object_(suite.util.Object_) Friends.min(suite.util.Friends.min) Statistic(suite.math.numeric.Statistic) Predicate(java.util.function.Predicate) ObjObj_Dbl(suite.primitive.DblPrimitives.ObjObj_Dbl) Datum(suite.trade.data.DataSource.Datum) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Quant(ts.Quant) Usex(suite.trade.Usex) Objects(java.util.Objects) WalkForwardAllocator(suite.trade.walkforwardalloc.WalkForwardAllocator) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Configuration(suite.trade.data.Configuration) Entry(java.util.Map.Entry) DataSource(suite.trade.data.DataSource) As(suite.streamlet.As) Asset(suite.trade.Asset) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) Fail(suite.util.Fail) DblFltPair(suite.primitive.adt.pair.DblFltPair) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Mutable(suite.adt.Mutable) HashMap(java.util.HashMap) Map(java.util.Map) DblFltPair(suite.primitive.adt.pair.DblFltPair) Pair(suite.adt.pair.Pair)

Example 13 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class WalkForwardAllocTester method conclusion.

public String conclusion() {
    float[] valuations_ = valuations.toFloats().toArray();
    int length = valuations_.length;
    double deltaMs = (start - System.currentTimeMillis()) / length;
    ReturnsStat rs = ts.returnsStat(valuations_, deltaMs);
    StringBuilder sb = new StringBuilder();
    for (Pair<String, Double> e : Read.from2(holdBySymbol).sortBy((symbol, value) -> -value).take(5)) sb.append(e.t0 + ":" + String.format("%.0f", e.t1 * 100d / length) + "%,");
    return // 
    "nTicks:" + length + " val:" + // 
    (0 < length ? valuations_[length - 1] : "N/A") + " tickRtn:" + // 
    To.string(rs.return_) + " sharpe:" + // 
    To.string(rs.sharpeRatio()) + " skew:" + // 
    To.string(stat.skewness(valuations_)) + " " + // 
    account.transactionSummary(cfg::transactionFee).out0() + " holds::" + sb + "...";
}
Also used : Read(suite.streamlet.Read) Statistic(suite.math.numeric.Statistic) FloatsBuilder(suite.primitive.Floats.FloatsBuilder) Trade_(suite.trade.Trade_) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) HashMap(java.util.HashMap) To(suite.util.To) UpdatePortfolio(suite.trade.Trade_.UpdatePortfolio) ArrayList(java.util.ArrayList) Pair(suite.adt.pair.Pair) List(java.util.List) Trade(suite.trade.Trade) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Configuration(suite.trade.data.Configuration) Eod(suite.trade.data.DataSource.Eod) ReturnsStat(ts.TimeSeries.ReturnsStat) Map(java.util.Map) Account(suite.trade.Account) Entry(java.util.Map.Entry) DataSource(suite.trade.data.DataSource) Sink(suite.util.FunUtil.Sink) TimeSeries(ts.TimeSeries) Asset(suite.trade.Asset) ReturnsStat(ts.TimeSeries.ReturnsStat)

Example 14 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class Object_ method mapper.

public static Mapper mapper(Type type) {
    Mapper mapper;
    if (type instanceof Class) {
        Class<?> clazz = (Class<?>) type;
        if (Type_.isSimple(clazz))
            mapper = new Mapper(object -> object, object -> object);
        else if (clazz.isArray()) {
            Class<?> componentType = clazz.getComponentType();
            mapper = new Mapper(object -> {
                Map<Object, Object> map = new HashMap<>();
                int length = Array.getLength(object);
                for (int i = 0; i < length; i++) map.put(i, Array.get(object, i));
                return map;
            }, object -> {
                Map<?, ?> map = (Map<?, ?>) object;
                Object objects = Array.newInstance(componentType, map.size());
                int i = 0;
                while (map.containsKey(i)) {
                    Array.set(objects, i, map.get(i));
                    i++;
                }
                return objects;
            });
        } else if (// polymorphism
        clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers()))
            mapper = new Mapper(object -> {
                Class<?> clazz1 = object.getClass();
                Object m = apply_(mapper(clazz1).map, object);
                if (m instanceof Map) {
                    @SuppressWarnings("unchecked") Map<String, String> map = (Map<String, String>) m;
                    map.put("@class", clazz1.getName());
                    return map;
                } else
                    // happens when an enum implements an interface
                    return m;
            }, object -> {
                if (object instanceof Map) {
                    Map<?, ?> map = (Map<?, ?>) object;
                    String className = map.get("@class").toString();
                    Class<?> clazz1 = Rethrow.ex(() -> Class.forName(className));
                    return apply_(mapper(clazz1).unmap, object);
                } else
                    // happens when an enum implements an interface
                    return object;
            });
        else {
            Inspect inspect = Singleton.me.inspect;
            List<Pair<String, Field>> sfs = // 
            Read.from(// 
            inspect.fields(clazz)).map(// 
            field -> Pair.of(field.getName(), field)).toList();
            mapper = new Mapper(object -> Rethrow.ex(() -> {
                Map<Object, Object> map = new HashMap<>();
                for (Pair<String, Field> sf : sfs) map.put(sf.t0, sf.t1.get(object));
                return map;
            }), object -> Rethrow.ex(() -> {
                Map<?, ?> map = (Map<?, ?>) object;
                Object object1 = new_(clazz);
                for (Pair<String, Field> sf : sfs) sf.t1.set(object1, map.get(sf.t0));
                return object1;
            }));
        }
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        Type rawType = pt.getRawType();
        Class<?> clazz = rawType instanceof Class ? (Class<?>) rawType : null;
        if (List.class.isAssignableFrom(clazz))
            mapper = new Mapper(object -> {
                Map<Object, Object> map = new HashMap<>();
                int i = 0;
                for (Object o : (Collection<?>) object) map.put(i++, o);
                return map;
            }, object -> {
                Map<?, ?> map = (Map<?, ?>) object;
                Collection<Object> object1 = new ArrayList<>();
                int i = 0;
                while (map.containsKey(i)) object1.add(map.get(i++));
                return object1;
            });
        else if (Map.class.isAssignableFrom(clazz))
            mapper = new Mapper(object -> object, object -> object);
        else
            mapper = mapper(rawType);
    } else
        mapper = Fail.t("unrecognized type " + type);
    return mapper;
}
Also used : Read(suite.streamlet.Read) Array(java.lang.reflect.Array) Singleton(suite.node.util.Singleton) Inspect(suite.inspect.Inspect) Collection(java.util.Collection) IOException(java.io.IOException) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) Type_(suite.jdk.gen.Type_) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) Iterate(suite.util.FunUtil.Iterate) Pair(suite.adt.pair.Pair) List(java.util.List) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Closeable(java.io.Closeable) Modifier(java.lang.reflect.Modifier) Map(java.util.Map) Comparator(java.util.Comparator) HashMap(java.util.HashMap) Inspect(suite.inspect.Inspect) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Pair(suite.adt.pair.Pair)

Example 15 with Pair

use of suite.adt.pair.Pair in project suite by stupidsing.

the class TextUtil method diff.

public List<Pair<Bytes, Bytes>> diff(Bytes bytesx, Bytes bytesy) {
    Lccs lccs = new Lccs();
    Pair<Segment, Segment> diff = lccs.lccs(bytesx, bytesy);
    Segment sx = diff.t0, sy = diff.t1;
    int x0 = 0, x1 = sx.start, x2 = sx.end, xx = bytesx.size();
    int y0 = 0, y1 = sy.start, y2 = sy.end, yx = bytesy.size();
    Bytes common = bytesx.range(x1, x2);
    if (!sx.isEmpty() && !sy.isEmpty()) {
        List<Pair<Bytes, Bytes>> patch = new ArrayList<>();
        patch.addAll(diff(bytesx.range(x0, x1), bytesy.range(y0, y1)));
        patch.add(Pair.of(common, common));
        patch.addAll(diff(bytesx.range(x2, xx), bytesy.range(y2, yx)));
        return patch;
    } else if (!bytesx.isEmpty() || !bytesy.isEmpty())
        return List.of(Pair.of(bytesx, bytesy));
    else
        return List.of();
}
Also used : Bytes(suite.primitive.Bytes) Lccs(suite.lcs.Lccs) ArrayList(java.util.ArrayList) Pair(suite.adt.pair.Pair)

Aggregations

Pair (suite.adt.pair.Pair)36 ArrayList (java.util.ArrayList)22 List (java.util.List)22 Read (suite.streamlet.Read)22 HashMap (java.util.HashMap)16 Map (java.util.Map)16 Node (suite.node.Node)14 Fail (suite.util.Fail)13 Fun (suite.util.FunUtil.Fun)12 Reference (suite.node.Reference)11 Tree (suite.node.Tree)10 TermOp (suite.node.io.TermOp)10 Set (java.util.Set)9 Atom (suite.node.Atom)9 Int (suite.node.Int)9 As (suite.streamlet.As)8 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7 Streamlet (suite.streamlet.Streamlet)7 Time (suite.trade.Time)6