lxw
2023-04-25 a3dd5e747bc4b8e8ceaead18f1055c07f51a0cf0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package com.whyc.util;
 
import com.whyc.dto.Response;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
 
/**
 * 计算工具类
 */
public class MathUtil {
 
    public static final Integer TYPE_FLOAT = 1;
    public static final Integer TYPE_FLOAT_100 = 2;
    public static final Integer TYPE_FLOAT_PERCENT = 3;
    /**
     * 提取公共方法,相除获取比例,返回可选String或者Float
     * @param type 1表示小数,2表示除去%的比例,3表示%的比例
     * @return Object String或者Float类型
     * */
    public static Object divide(Object num,Object num2,Integer type){
        float res = 0;
        double num2Double=Double.parseDouble(num2.toString());
        if(num2Double!=0){
 
            if (num instanceof Integer) {
                res = BigDecimal.valueOf((Integer) num).divide(BigDecimal.valueOf((Integer) num2), 2, RoundingMode.HALF_UP).floatValue();
            }
            if (num instanceof Float) {
                res = BigDecimal.valueOf((Float) num).divide(BigDecimal.valueOf((Float) num2), 2, RoundingMode.HALF_UP).floatValue();
            }
            if (num instanceof Double) {
                res = BigDecimal.valueOf((Double) num).divide(BigDecimal.valueOf((Double) num2), 2, RoundingMode.HALF_UP).floatValue();
            }
        }
        //0.05
        if (type == 1) {
            return res;
        }
        //5
        else if (type == 2) {
            return (int) (res * 100);
        }
        //5%
        return (int) (res * 100) + "%";
    }
 
    /**
     * 提取公共方法,相除获取比例,返回可选String或者Float
     * @param type 1表示小数,2表示除去%的比例,3表示%的比例
     * @return Object String或者Float类型
     * */
    public static Object divide(Object num, Object num2, Integer type, Integer scale){
        Object res = 0;
        double num2Double=Double.parseDouble(num2.toString());
        double numDouble=Double.parseDouble(num.toString());
        if(num2Double!=0){
            //0.05
            if (type == 1) {
                res = BigDecimal.valueOf(numDouble).divide(BigDecimal.valueOf(num2Double), scale, RoundingMode.HALF_UP).floatValue();
            }
            //5
            else {
                BigDecimal multiply = BigDecimal.valueOf(numDouble).multiply(BigDecimal.valueOf(100));
                BigDecimal divide = multiply.divide(BigDecimal.valueOf(num2Double), scale, RoundingMode.HALF_UP);
                if (type == 2) {
                    res = divide.floatValue()+"";
                }
                //5%
                else{
                    res = divide.floatValue()+"%";
                }
            }
            return res;
        }else{
            return 0;
        }
    }
 
    /**
     * 求自变量X,因变量Y的线性回归 @PerryHsu
     * y = ax+b
     *
     * a = ∑Yi(Xi-X的平均值)/∑(Xi^2)-1/n*(∑(Xi^2)
     *
     * b = 1/n*∑(Yi-aXi)
     *
     * */
    public static Response linearRegressionUnary(List<Double> listX, List<Double> listY,Double x) {
        List<Double> resList = new LinkedList<>();
 
        int sizeX = listX.size();
        int sizeY = listY.size();
        DecimalFormat df = new DecimalFormat("0.##");
 
        if (sizeX != sizeY) {
            return new Response<>().set(1,false,"分子分母数量不一致。");
        }
 
        Double sum = 0d;
        for (Double xi : listX) {
            sum += xi;
        }
        double avgX = sum/ sizeY;
 
        //a的分子
        double aMolecule = 0;
        for (int i = 0; i < sizeX; i++) {
            aMolecule += listY.get(i) * (listX.get(i) - avgX);
        }
 
        //a的分母
        double aDenominator = 0;
        int aDenominatorXi = 0;
        for (int i = 0; i < sizeX; i++) {
            aDenominator += Math.pow(listX.get(i), 2);
            aDenominatorXi += listX.get(i);
        }
        aDenominator = aDenominator - (1.0 / sizeX) * (Math.pow(aDenominatorXi, 2));
 
        //System.out.println("w_denominator:"+w_denominator+" w_denominator_xi:"+w_denominator_xi);
 
        double a = aMolecule / aDenominator;
 
        double b = 1.0 / sizeX;
        double sumYAX = 0;
        for (int i = 0; i < sizeX; i++) {
            sumYAX += (listY.get(i) - a * listX.get(i));
        }
        b = b * sumYAX;
 
        String symbol = "+";
        if (b < 0) {
            symbol = "";
        }
        //System.out.println("y=" + df.format(a) + "x" + symbol + df.format(b));
 
        Double y = a*x + b;
        resList.add(a);
        resList.add(b);
        resList.add(y);
        return new Response().setII(1,true,resList,null);
    }
 
    public static void main(String[] args) throws ParseException {
 
        /*List<Double> x = Arrays.asList(5d, 9d, 15d, 19d, 19d, 45d);
 
        List<Double> y = Arrays.asList(4d, 6d, 12d, 15d, 15d, 37d);*/
        //按照年为单位为x.变化差值为y
        /*List<Double> x = Arrays.asList(
                DateUtil.YYYY_MM_DD_HH_MM_SS.parse("2021-04-13 09:14:36").getTime()*1d,
                DateUtil.YYYY_MM_DD_HH_MM_SS.parse("2021-10-14 13:46:36").getTime()*1d,
                DateUtil.YYYY_MM_DD_HH_MM_SS.parse("2022-03-18 11:08:36").getTime()*1d,
                DateUtil.YYYY_MM_DD_HH_MM_SS.parse("2022-03-18 13:56:36").getTime()*1d);
 
        List<Double> y = Arrays.asList(
                99d,
                80d,
                71.9d,
                71d);*/
        long time1 = DateUtil.YYYY_MM_DD_HH_MM_SS.parse("2021-04-13 09:14:36").getTime();
        long time2 = DateUtil.YYYY_MM_DD_HH_MM_SS.parse("2021-10-14 13:46:36").getTime();
        long time3 = DateUtil.YYYY_MM_DD_HH_MM_SS.parse("2022-03-18 11:08:36").getTime();
        long time4 = DateUtil.YYYY_MM_DD_HH_MM_SS.parse("2022-03-18 13:56:36").getTime();
 
        long time5 = DateUtil.YYYY_MM_DD_HH_MM_SS.parse("2023-03-12 13:56:36").getTime();
 
        double c1 = 99d;
        double c2 = 98d;
        double c3 = 96.9d;
        double c4 = 95d;
 
        List<Double> x = Arrays.asList(
                0d,
                (time2-time1)*1d/1000/60/60/24,
                (time3-time1)*1d/1000/60/60/24,
                (time4-time1)*1d/1000/60/60/24
        );
 
 
        List<Double> y = Arrays.asList(
                c1,
                c2,
                c3,
                c4
        );
        double v = (time5 - time1) * 1d / 1000 / 60 / 60 /24;
        System.out.println(v);
        Response response = linearRegressionUnary(x, y, v);
        System.out.println(response);
    }
 
 
 
}