Преглед изворни кода

[task-1766] 性能验证模块 开发 (二期) / 【后端】验证报告的接口开发

Li Yuan пре 1 година
родитељ
комит
28a3a1c6ed

+ 0 - 6
ibps-provider-root/modules/provider-business/pom.xml

@@ -271,12 +271,6 @@
             <version>3.6.1</version>
             <scope>compile</scope>
         </dependency>
-		<!-- https://mvnrepository.com/artifact/net.sf.jsci/jsci -->
-		<dependency>
-			<groupId>net.sf.jsci</groupId>
-			<artifactId>jsci</artifactId>
-			<version>1.2</version>
-		</dependency>
 
 
 

+ 13 - 2
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/model/CalcVO.java

@@ -7,16 +7,27 @@ import java.text.DecimalFormat;
 
 public class CalcVO {
 
-    public double format(double d,int scale){
+    public static double format(double d,int scale){
         return Double.parseDouble(String.format("%."+scale+"f", d));
 //        return new BigDecimal(d).setScale(scale,BigDecimal.ROUND_HALF_UP).doubleValue();
     }
 
 
-    public double getT(double degreesOfFreedom,double p){
+    public static double getT(double degreesOfFreedom,double p){
         TDistribution t = new TDistribution(degreesOfFreedom);
         double value = t.inverseCumulativeProbability(1 - p/2);
 //        double value = t.inverseCumulativeProbability(1 - 0.01/2);
         return format(value,2);
     }
+
+
+    private  double[][] transposeMatrix(double[][] before) {
+        double[][] after = new double[before[0].length][before.length];
+        for (int i = 0; i < before.length; i++) {
+            for (int j = 0; j < before[i].length; j++) {
+                after[i][j] = before[j][i];
+            }
+        }
+        return after;
+    }
 }

+ 49 - 7
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/model/EP6ALinearRangeVO.java

@@ -4,11 +4,16 @@ import com.lc.ibps.components.verification.regression.PolynomialRegression;
 import org.apache.commons.math3.stat.StatUtils;
 import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
 
+import java.util.Arrays;
+
 public class EP6ALinearRangeVO extends CalcVO{
 
     private double[][] data;
 
     private double[] means;
+    private double[][] diffs;
+    private double[][] dli;
+    private double maxDli;
     private double mean;
     private double[] standardDeviations;
 
@@ -21,6 +26,11 @@ public class EP6ALinearRangeVO extends CalcVO{
     private PolynomialRegression pr1 = new PolynomialRegression();
     private PolynomialRegression pr2 = new PolynomialRegression();
     private PolynomialRegression pr3 = new PolynomialRegression();
+    private PolynomialRegression prBest;
+
+
+    private double sdr;
+    private double cvr;
 
     public EP6ALinearRangeVO(double[][] data, double high, double low){
         this.data = data;
@@ -29,6 +39,8 @@ public class EP6ALinearRangeVO extends CalcVO{
         this.specimensNum = data.length;
         this.repeatNum = data[0].length;
         this.means = new double[this.specimensNum];
+        this.diffs = new double[5][this.specimensNum];
+        this.dli = new double[5][this.specimensNum];
         this.standardDeviations = new double[this.specimensNum];
         this.targetValues = new double[this.specimensNum];
         calculate();
@@ -40,20 +52,50 @@ public class EP6ALinearRangeVO extends CalcVO{
             DescriptiveStatistics stat = new DescriptiveStatistics(data[i]);
             means[i] = format(stat.getMean(),2);
             standardDeviations[i] = stat.getStandardDeviation();
+            if(repeatNum == 2){
+                diffs[1][i] = data[i][0] - data[i][1];  //diff
+                diffs[2][i] = Math.pow(diffs[1][i],2) /2; //diff * diff / 2
+                diffs[3][i] = diffs[1][i] / means[i] ;  //%diff
+                diffs[4][i] = Math.pow(diffs[3][i],2) /2; //%diff * %diff /2
+            }
 
         }
+        diffs[0] = means;
         mean = StatUtils.mean(means);
+        sdr = Math.sqrt(StatUtils.mean(diffs[2]))*100;
+        cvr = Math.sqrt(StatUtils.mean(diffs[4]))*100;
+        //TODO: check cvr
 
         pr1.addData(targetValues, means, repeatNum,1);
         pr2.addData(targetValues, means, repeatNum,2);
         pr3.addData(targetValues, means, repeatNum,3);
-        double d = getT(pr1.getDfDependent(),0.05);
-        double d2 = getT(pr2.getDfDependent(),0.05);
-        double d3 = getT(pr3.getDfDependent(),0.05);
-        double s1 = pr1.getStdError() / mean;
-        double s2 = pr2.getStdError() /mean;
-        double s3 = pr3.getStdError() /mean;
-        pr1.getParameters();
+
+        if(pr2.isNonlinear() && pr3.isNonlinear()){
+            if(pr2.getStdError() > pr3.getStdError()) prBest = pr3;
+            else prBest = pr2;
+        } else if(pr2.isNonlinear()) {
+            prBest = pr2;
+        }
+        else if (pr3.isNonlinear()) {
+            prBest = pr3;
+        }
+        //calc dli
+        if(prBest != null) {
+            dli[0] = targetValues;
+            for (int i = 0; i < specimensNum; i++) {
+                dli[1][i] = prBest.calcFunc(targetValues[i]);
+                dli[2][i] = pr1.calcFunc(targetValues[i]);
+                dli[3][i] = dli[1][i] - dli[2][i];
+                dli[4][i] = 100 * dli[3][i] / dli[0][i];
+            }
+
+            maxDli = Arrays.stream(dli[4]).max().getAsDouble();
+        }
+//
+//        double s1 = pr1.getStdError() / mean;
+//        double s2 = pr2.getStdError() /mean;
+//        double s3 = pr3.getStdError() /mean;
+//        pr1.getParameters();
     }
 
     private void calcTarget(){

+ 38 - 0
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/regression/PolynomialRegression.java

@@ -1,5 +1,6 @@
 package com.lc.ibps.components.verification.regression;
 
+import com.lc.ibps.components.verification.model.CalcVO;
 import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;
 
 import java.util.ArrayList;
@@ -11,6 +12,9 @@ public class PolynomialRegression extends Regression{
 
     private double[] tValue;
 
+    private boolean isNonlinear = false;
+
+
     public PolynomialRegression() {
         ols = new OLSMultipleLinearRegression();
     }
@@ -42,6 +46,22 @@ public class PolynomialRegression extends Regression{
         for (int i = 0; i < getParameters().length; i++) {
             tValue[i] = getParameters()[i] / getStdErrors()[i];
         }
+        if (getParameters().length >2) {
+            double target = CalcVO.getT(getDfDependent(), 0.05);
+            for (int i = 2; i < getParameters().length; i++) {
+                if(tValue[i] > 0){
+                    if(tValue[i] > target) {
+                        isNonlinear = true;
+                        break;
+                    }
+                } else{
+                    if(tValue[i] < -target){
+                        isNonlinear = true;
+                        break;
+                    }
+                }
+            }
+        }
 
     }
     /**
@@ -83,4 +103,22 @@ public class PolynomialRegression extends Regression{
         }
         return function.toString();
     }
+
+    public double calcFunc(double x){
+        double[] parameters = this.getParameters();
+        double y = parameters[0];
+        for (int i = 1; i < parameters.length; i++) {
+            y = y + Math.pow(x,i) * parameters[i];
+        }
+        return y;
+    }
+
+    public boolean isNonlinear() {
+        return isNonlinear;
+    }
+
+    public void setNonlinear(boolean nonlinear) {
+        isNonlinear = nonlinear;
+    }
+
 }

+ 2 - 0
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/regression/Regression.java

@@ -93,4 +93,6 @@ public class Regression {
     public void setStdError(double stdError) {
         this.stdError = stdError;
     }
+
+
 }

+ 2 - 4
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/regression/StatisticsTest.java

@@ -26,8 +26,6 @@ public class StatisticsTest {
         System.out.println(re.getFunction());
         System.out.println("标准误为:" + re.getStdErrors()[0]);
 
-        System.out.println("f值:" + re.getFValue() );
-        System.out.println("f检验P值:" + StatisticsUtil.getPValue(re.getFValue(), 1, data.length - 2));
 
     }
 
@@ -79,7 +77,7 @@ public class StatisticsTest {
         System.out.println("标准误为:" + Arrays.toString(po.getStdErrors()));
 
         System.out.println("f值:" + po.getFValue() );
-        System.out.println("f检验P值:" + StatisticsUtil.getPValue(po.getFValue(), 3, yArray.length - 4));
+//        System.out.println("f检验P值:" + StatisticsUtil.getPValue(po.getFValue(), 3, yArray.length - 4));
         TDistribution t = new TDistribution(16);
 //        t.inverseCumulativeProbability(1 - p/2);
         System.out.println((1-t.cumulativeProbability(0.5)));
@@ -173,7 +171,7 @@ public class StatisticsTest {
         System.out.println("调整后R方 :" + mul.getAdjRSquared());
         System.out.println("f值:" + mul.getFValue()) ;
 
-        System.out.println("f检验P值:" + StatisticsUtil.getPValue(mul.getFValue(), mul.getDfIndependent(), mul.getDfDependent() ));
+//        System.out.println("f检验P值:" + StatisticsUtil.getPValue(mul.getFValue(), mul.getDfIndependent(), mul.getDfDependent() ));
     }
 
     public static double[][] randomX3() {

+ 0 - 70
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/regression/StatisticsUtil.java

@@ -1,7 +1,5 @@
 package com.lc.ibps.components.verification.regression;
 
-import JSci.maths.statistics.FDistribution;
-import JSci.maths.statistics.TDistribution;
 import org.apache.commons.math3.exception.DimensionMismatchException;
 import org.apache.commons.math3.stat.correlation.PearsonsCorrelation;
 import org.apache.commons.math3.stat.correlation.SpearmansCorrelation;
@@ -146,58 +144,6 @@ public class StatisticsUtil {
     }
 
 
-    /**
-     * 计算显著性p值算法
-     * @param rValue 相关系数
-     * @param n 分析数据的个数
-     * @param methodId 使用的方法
-     * @return p-value
-     */
-    public static double getPValue(final double rValue, final int n, String methodId) {
-
-        if (n < 2) {
-            return NO_VALUE;
-        }
-        double pValue = NO_VALUE;
-        double tValue = ((rValue * Math.sqrt(n-2)) / (Math.sqrt(1 - (rValue * rValue))));
-        int free= n-2;
-        TDistribution td=new TDistribution(free);
-        if (PEARSON_ID.equals(methodId)) {
-
-            double cumulative = td.cumulative(tValue);
-            if(tValue>0) {
-                pValue=(1-cumulative)*2;
-            }else {
-                pValue=cumulative*2;
-            }
-
-        } else if (SPEARMAN_ID.equals(methodId)) {
-            if (n > 500) {
-                tValue = (rValue * Math.sqrt(n-1));
-            }
-
-            double cumulative = td.cumulative(tValue);
-            if(tValue>0) {
-                pValue=(1-cumulative)*2;
-            }else {
-                pValue=cumulative*2;
-            }
-
-        }
-
-        return pValue;
-    }
-
-    /**
-     * T检验 - 计算显著性p值算法
-     * @param rValue 相关系数
-     * @param n 分析数据的个数
-     * @return p-value
-     */
-    public static double getPValue(final double rValue, final int n) {
-        return getPValue(rValue, n, PEARSON_ID);
-    }
-
     /**
      * 判空
      * @param values 双精度数组
@@ -207,22 +153,6 @@ public class StatisticsUtil {
         return values == null;
     }
 
-    /**
-     * F检验 - 计算显著性p值算法
-     * @param fValue f检验值 (ESS/K)/(RSS/(n-k-1))
-     * @param dgrP X的自由度 即为自变量的个数
-     * @param dgrQ Y的自由度 样本数 - 自变量个数 -1
-     * @return P-Value
-     */
-    public static double getPValue(double fValue, int dgrP, int dgrQ) {
-
-        FDistribution fd=new FDistribution(dgrP, dgrQ);
-
-        double cumulative = fd.cumulative(fValue);
-
-        return (1-cumulative);
-    }
-
 
 
 }