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;
}
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();
};
};
}
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 + "...";
}
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;
}
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();
}
Aggregations