Skip to content

Commit ad61f79

Browse files
Lu-Jichenlukaseder
andauthored
implement of issue 151, linear regression (jOOQ#386)
* implement of issue 151, linear regression * rewrite of 9 functions * rewrite of 9 functions * update with tuple2 type input * update avg functions Co-authored-by: Lukas Eder <lukas.eder@gmail.com>
1 parent 11aebbd commit ad61f79

File tree

1 file changed

+164
-0
lines changed
  • jOOL-java-8/src/main/java/org/jooq/lambda

1 file changed

+164
-0
lines changed

jOOL-java-8/src/main/java/org/jooq/lambda/Agg.java

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,4 +1163,168 @@ private static <T> double avg0(List<T> list, ToDoubleFunction<? super T> functio
11631163
s -> s.map(Objects::toString).orElse("")
11641164
);
11651165
}
1166+
1167+
/**
1168+
* Realize of linear regression functions.
1169+
*/
1170+
1171+
/**
1172+
* Get a {@link Collector} that calculates the <code>regrCountLong()</code> function.
1173+
*/
1174+
public static Collector<Tuple2<Double,Double>, ?, Optional<Double>> regrCountLong() {
1175+
return regrCountLong(t -> t.v1, t -> t.v2);
1176+
}
1177+
1178+
/**
1179+
* Get a {@link Collector} that calculates the <code>regrCountLong()</code> function.
1180+
*/
1181+
public static <T> Collector<T, ?, Optional<Double>> regrCountLong(ToDoubleFunction<? super T> functionX, ToDoubleFunction<? super T> functionY) {
1182+
return collectingAndThen(toList(), l -> {
1183+
if (l.isEmpty())
1184+
return Optional.empty();
1185+
long result = 0;
1186+
for (T t : l)
1187+
result += t==null ? 0:1;
1188+
return Optional.of((double)result);
1189+
});
1190+
}
1191+
1192+
/**
1193+
* Get a {@link Collector} that calculates the <code>regrAvgXDouble()</code> function.
1194+
*/
1195+
public static Collector<Tuple2<Double,Double>, ?, Double> regrAvgXDouble() {
1196+
return regrAvgXDouble(t -> t.v1);
1197+
}
1198+
1199+
/**
1200+
* Get a {@link Collector} that calculates the <code>regrAvgXDouble()</code> function.
1201+
*/
1202+
public static <T> Collector<T, ?, Double> regrAvgXDouble(ToDoubleFunction<? super T> functionX) {
1203+
return filter(a-> a!=null,averagingDouble(functionX));
1204+
}
1205+
1206+
/**
1207+
* Get a {@link Collector} that calculates the <code>regrAvgYDouble()</code> function.
1208+
*/
1209+
public static Collector<Tuple2<Double,Double>, ?, Double> regrAvgYDouble() {
1210+
return regrAvgYDouble(t -> t.v2);
1211+
}
1212+
1213+
/**
1214+
* Get a {@link Collector} that calculates the <code>regrAvgYDouble()</code> function.
1215+
*/
1216+
public static <T> Collector<T, ?, Double> regrAvgYDouble(ToDoubleFunction<? super T> functionY) {
1217+
return regrAvgXDouble(functionY);
1218+
}
1219+
1220+
/**
1221+
* Get a {@link Collector} that calculates the <code>regrSxxDouble()</code> function.
1222+
*/
1223+
public static Collector<Tuple2<Double,Double>, ?, Optional<Double>> regrSxxDouble() {
1224+
return regrSxxDouble(t -> t.v1, t -> t.v2);
1225+
}
1226+
1227+
/**
1228+
* Get a {@link Collector} that calculates the <code>regrSxxDouble()</code> function.
1229+
*/
1230+
public static <T> Collector<T, ?, Optional<Double>> regrSxxDouble(ToDoubleFunction<? super T> functionX, ToDoubleFunction<? super T> functionY) {
1231+
return collectingAndThen(
1232+
collectors(covarianceDouble(functionX, functionX),
1233+
count()),
1234+
t -> !t.v1.isPresent() || t.v2==0 ? Optional.empty() : Optional.of(t.v1.get() * t.v2 )
1235+
);
1236+
}
1237+
1238+
/**
1239+
* Get a {@link Collector} that calculates the <code>regrSyyDouble()</code> function.
1240+
*/
1241+
public static Collector<Tuple2<Double,Double>, ?, Optional<Double>> regrSyyDouble() {
1242+
return regrSyyDouble(t -> t.v1, t -> t.v2);
1243+
}
1244+
1245+
/**
1246+
* Get a {@link Collector} that calculates the <code>regrSyyDouble()</code> function.
1247+
*/
1248+
public static <T> Collector<T, ?, Optional<Double>> regrSyyDouble(ToDoubleFunction<? super T> functionX, ToDoubleFunction<? super T> functionY) {
1249+
return collectingAndThen(
1250+
collectors(covarianceDouble(functionY, functionY),
1251+
count()),
1252+
t -> !t.v1.isPresent() || t.v2==0 ? Optional.empty() : Optional.of(t.v1.get() * t.v2 )
1253+
);
1254+
}
1255+
1256+
/**
1257+
* Get a {@link Collector} that calculates the <code>regrSxyDouble()</code> function.
1258+
*/
1259+
public static Collector<Tuple2<Double,Double>, ?, Optional<Double>> regrSxyDouble() {
1260+
return regrSxyDouble(t -> t.v1, t -> t.v2);
1261+
}
1262+
1263+
/**
1264+
* Get a {@link Collector} that calculates the <code>regrSxyDouble()</code> function.
1265+
*/
1266+
public static <T> Collector<T, ?, Optional<Double>> regrSxyDouble(ToDoubleFunction<? super T> functionX, ToDoubleFunction<? super T> functionY) {
1267+
return collectingAndThen(
1268+
collectors(covarianceDouble(functionX, functionY),
1269+
count()),
1270+
t -> !t.v1.isPresent() || t.v2==0 ? Optional.empty() : Optional.of(t.v1.get() * t.v2 )
1271+
);
1272+
}
1273+
1274+
/**
1275+
* Get a {@link Collector} that calculates the <code>regrSlopeDouble()</code> function.
1276+
*/
1277+
public static Collector<Tuple2<Double,Double>, ?, Optional<Double>> regrSlopeDouble() {
1278+
return regrSlopeDouble(t -> t.v1, t -> t.v2);
1279+
}
1280+
1281+
/**
1282+
* Get a {@link Collector} that calculates the <code>regrSlopeDouble()</code> function.
1283+
*/
1284+
public static <T> Collector<T, ?, Optional<Double>> regrSlopeDouble(ToDoubleFunction<? super T> functionX, ToDoubleFunction<? super T> functionY) {
1285+
return collectingAndThen(
1286+
collectors(covarianceDouble(functionX, functionY),
1287+
varianceDouble(functionY)),
1288+
t -> !t.v1.isPresent() || t.v2.orElse(0.0) == 0.0 ? Optional.empty() : Optional.of(t.v1.get() / t.v2.get() )
1289+
);
1290+
}
1291+
1292+
/**
1293+
* Get a {@link Collector} that calculates the <code>regrInterceptDouble()</code> function.
1294+
*/
1295+
public static Collector<Tuple2<Double,Double>, ?, Optional<Double>> regrInterceptDouble() {
1296+
return regrInterceptDouble(t -> t.v1, t -> t.v2);
1297+
}
1298+
1299+
/**
1300+
* Get a {@link Collector} that calculates the <code>regrInterceptDouble()</code> function.
1301+
*/
1302+
public static <T> Collector<T, ?, Optional<Double>> regrInterceptDouble(ToDoubleFunction<? super T> functionX, ToDoubleFunction<? super T> functionY) {
1303+
return collectingAndThen(
1304+
collectors(regrAvgXDouble(functionX),
1305+
regrSlopeDouble(functionX, functionY),
1306+
regrAvgYDouble(functionY)),
1307+
t -> t.v1==0.0 || !t.v2.isPresent() || t.v3==0.0 ? Optional.empty() : Optional.of(t.v1 - (t.v2.get() * t.v3) )
1308+
);
1309+
}
1310+
1311+
/**
1312+
* Get a {@link Collector} that calculates the <code>regrR2Double()</code> function.
1313+
*/
1314+
public static Collector<Tuple2<Double,Double>, ?, Optional<Double>> regrR2Double() {
1315+
return regrR2Double(t -> t.v1, t -> t.v2);
1316+
}
1317+
1318+
/**
1319+
* Get a {@link Collector} that calculates the <code>regrR2Double()</code> function.
1320+
*/
1321+
public static <T> Collector<T, ?, Optional<Double>> regrR2Double(ToDoubleFunction<? super T> functionX, ToDoubleFunction<? super T> functionY) {
1322+
return collectingAndThen(
1323+
collectors(varianceDouble(functionX),
1324+
varianceDouble(functionY),
1325+
correlationDouble(functionX,functionY)),
1326+
t -> t.v2.get()==0.0 ? Optional.of(0.0) : (t.v1.get()!=0.0 ? Optional.of(1.0) : Optional.of(Math.pow(t.v3.get(),2)))
1327+
);
1328+
}
1329+
11661330
}

0 commit comments

Comments
 (0)