-
Notifications
You must be signed in to change notification settings - Fork 802
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
425469a
commit 76a9aed
Showing
56 changed files
with
1,328 additions
and
1,273 deletions.
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
core/src/main/java/com/alibaba/alink/common/insights/DistributionUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package com.alibaba.alink.common.insights; | ||
|
||
import org.apache.flink.api.java.tuple.Tuple3; | ||
|
||
import breeze.stats.distributions.LogNormal; | ||
import org.apache.commons.math3.distribution.ChiSquaredDistribution; | ||
import org.apache.commons.math3.distribution.ExponentialDistribution; | ||
import org.apache.commons.math3.distribution.LogNormalDistribution; | ||
import org.apache.commons.math3.distribution.LogisticDistribution; | ||
import org.apache.commons.math3.distribution.NormalDistribution; | ||
import org.apache.commons.math3.distribution.TDistribution; | ||
import org.apache.commons.math3.distribution.UniformRealDistribution; | ||
import org.apache.commons.math3.stat.inference.KolmogorovSmirnovTest; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class DistributionUtil { | ||
|
||
public static final KolmogorovSmirnovTest KS_TEST = new KolmogorovSmirnovTest(); | ||
|
||
public static double[] loadDataToVec(List <Number> dataList) { | ||
double[] datas = new double[dataList.size()]; | ||
for (int i = 0; i < dataList.size(); i++) { | ||
datas[i] = Double.valueOf(String.valueOf(dataList.get(i))); | ||
} | ||
return datas; | ||
} | ||
|
||
public static Tuple3 <Double, Double, double[]> getMeanSd(List <Number> dataList) { | ||
if (dataList.size() == 0) { | ||
return Tuple3.of(0D, 0D, null); | ||
} | ||
double[] datas = loadDataToVec(dataList); | ||
double avg = Arrays.stream(datas).average().getAsDouble(); | ||
double variance = 0; | ||
for (int i = 0; i < datas.length; i++) { | ||
variance += Math.pow(datas[i] - avg, 2); | ||
} | ||
variance = variance / datas.length; | ||
double sd = Math.sqrt(variance); | ||
return Tuple3.of(avg, sd, datas); | ||
} | ||
|
||
public static double testNormalDistribution(List <Number> dataList) { | ||
if (dataList.size() == 0) { | ||
return 0; | ||
} | ||
Tuple3 <Double, Double, double[]> avgTuple = getMeanSd(dataList); | ||
NormalDistribution distribution = new NormalDistribution(avgTuple.f0, avgTuple.f1); | ||
return KS_TEST.kolmogorovSmirnovTest(distribution, avgTuple.f2); | ||
} | ||
|
||
public static double testUniformDistribution(List <Number> dataList) { | ||
if (dataList.size() == 0) { | ||
return 0; | ||
} | ||
double[] datas = loadDataToVec(dataList); | ||
double lower = Arrays.stream(datas).min().getAsDouble(); | ||
double upper = Arrays.stream(datas).max().getAsDouble(); | ||
|
||
UniformRealDistribution distribution = new UniformRealDistribution(lower, upper); | ||
return KS_TEST.kolmogorovSmirnovTest(distribution, datas); | ||
} | ||
|
||
// if positive, return degreeOfFreedom | ||
public static double testChiSquaredDistribution(List <Number> dataList) { | ||
if (dataList.size() == 0) { | ||
return 0; | ||
} | ||
double[] datas = loadDataToVec(dataList); | ||
for (int i = 1; i <= 20; i++) { | ||
ChiSquaredDistribution distribution = new ChiSquaredDistribution(i); | ||
double p = KS_TEST.kolmogorovSmirnovTest(distribution, datas); | ||
if (p > 0.05 ) { | ||
return i; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
public static double testExpDistribution(List <Number> dataList) { | ||
if (dataList.size() == 0) { | ||
return 0; | ||
} | ||
double[] datas = loadDataToVec(dataList); | ||
double avg = Arrays.stream(datas).average().getAsDouble(); | ||
ExponentialDistribution distribution = new ExponentialDistribution(avg); | ||
return KS_TEST.kolmogorovSmirnovTest(distribution, datas); | ||
} | ||
|
||
// if positive, return degreeOfFreedom | ||
public static double testTDistribution(List <Number> dataList) { | ||
if (dataList.size() == 0) { | ||
return 0; | ||
} | ||
double[] datas = loadDataToVec(dataList); | ||
for (int i = 1; i <= 20; i++) { | ||
TDistribution distribution = new TDistribution(i); | ||
double p = KS_TEST.kolmogorovSmirnovTest(distribution, datas); | ||
if (p > 0.05) { | ||
return i; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
public static double testLogisticDistribution(List<Number> dataList) { | ||
if (dataList.size() == 0) { | ||
return 0; | ||
} | ||
Tuple3 <Double, Double, double[]> tuple3 = getMeanSd(dataList); | ||
double avg = tuple3.f0; | ||
double sd = tuple3.f1; | ||
double[] datas = tuple3.f2; | ||
LogisticDistribution distribution = new LogisticDistribution(avg, sd); | ||
return KS_TEST.kolmogorovSmirnovTest(distribution, datas); | ||
} | ||
|
||
public static double testLogNormalDistribution(List<Number> dataList) { | ||
if (dataList.size() == 0) { | ||
return 0; | ||
} | ||
double[] datas = loadDataToVec(dataList); | ||
double min = Arrays.stream(datas).min().getAsDouble(); | ||
if (min <= 0) { | ||
return 0; | ||
} | ||
double[] logValues = new double[datas.length]; | ||
for (int i = 0; i < datas.length; i++) { | ||
logValues[i] = Math.log(datas[i]); | ||
} | ||
double avg = Arrays.stream(logValues).average().getAsDouble(); | ||
double var = 0; | ||
for (int i = 0; i < logValues.length; i++) { | ||
var += Math.pow((logValues[i] - avg), 2); | ||
} | ||
var = Math.sqrt(var); | ||
LogNormalDistribution distribution = new LogNormalDistribution(avg, var); | ||
return KS_TEST.kolmogorovSmirnovTest(distribution, datas); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
core/src/main/java/com/alibaba/alink/common/sql/builtin/string/string/DateAdd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.alibaba.alink.common.sql.builtin.string.string; | ||
|
||
import org.apache.flink.table.functions.ScalarFunction; | ||
|
||
import java.sql.Timestamp; | ||
|
||
/** | ||
* @author weibo zhao | ||
*/ | ||
public class DateAdd extends ScalarFunction { | ||
|
||
private static final long serialVersionUID = -4716626352353627712L; | ||
|
||
public String eval(String end, int days) { | ||
if (end == null) { | ||
return null; | ||
} | ||
try { | ||
Timestamp tsEnd = Timestamp.valueOf(end); | ||
long ld = (tsEnd.getTime() + days * 86400000L); | ||
return new Timestamp(ld).toString(); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
|
||
public String eval(Timestamp end, int days) { | ||
if (end == null) { | ||
return null; | ||
} | ||
long ld = (end.getTime() + days * 86400000L); | ||
return new Timestamp(ld).toString(); | ||
|
||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
core/src/main/java/com/alibaba/alink/common/sql/builtin/string/string/DateDiff.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.alibaba.alink.common.sql.builtin.string.string; | ||
|
||
import org.apache.flink.table.functions.ScalarFunction; | ||
|
||
import java.sql.Timestamp; | ||
|
||
/** | ||
* @author weibo zhao | ||
*/ | ||
|
||
public class DateDiff extends ScalarFunction { | ||
|
||
private static final long serialVersionUID = 6298088633116239045L; | ||
|
||
public Long eval(String end, String start) { | ||
if (start == null || end == null) { | ||
return null; | ||
} | ||
try { | ||
Timestamp tsEnd = Timestamp.valueOf(end); | ||
Timestamp tsStart = Timestamp.valueOf(start); | ||
return (tsEnd.getTime() - tsStart.getTime()) / 86400000L; | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
|
||
public Long eval(String end, Timestamp start) { | ||
if (start == null || end == null) { | ||
return null; | ||
} | ||
try { | ||
Timestamp tsEnd = Timestamp.valueOf(end); | ||
return (tsEnd.getTime() - start.getTime()) / 86400000L; | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
|
||
public Long eval(Timestamp end, Timestamp start) { | ||
if (start == null || end == null) { | ||
return null; | ||
} | ||
return (end.getTime() - start.getTime()) / 86400000L; | ||
} | ||
|
||
public Long eval(Timestamp end, String start) { | ||
if (start == null || end == null) { | ||
return null; | ||
} | ||
try { | ||
Timestamp tsStart = Timestamp.valueOf(start); | ||
return (end.getTime() - tsStart.getTime()) / 86400000L; | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.