107 lines
2.6 KiB
Java
107 lines
2.6 KiB
Java
|
|
package com.gunshi.project.hsz.model;
|
|||
|
|
|
|||
|
|
import lombok.Data;
|
|||
|
|
|
|||
|
|
import java.math.BigDecimal;
|
|||
|
|
import java.util.List;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 线性回归方程实体类
|
|||
|
|
*/
|
|||
|
|
@Data
|
|||
|
|
public class RegressionEquation {
|
|||
|
|
/**
|
|||
|
|
* 方程阶数
|
|||
|
|
*/
|
|||
|
|
private int order;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 方程系数(从常数项到最高次项)
|
|||
|
|
* 例如:对于二次方程 y = ax² + bx + c,coefficients = [c, b, a]
|
|||
|
|
*/
|
|||
|
|
private List<BigDecimal> coefficients;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 相关系数 R²
|
|||
|
|
*/
|
|||
|
|
private BigDecimal rSquared;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 数据点数
|
|||
|
|
*/
|
|||
|
|
private int dataCount;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 方程字符串表示
|
|||
|
|
*/
|
|||
|
|
private String equationString;
|
|||
|
|
|
|||
|
|
public RegressionEquation(int order, List<BigDecimal> coefficients, BigDecimal rSquared, int dataCount) {
|
|||
|
|
this.order = order;
|
|||
|
|
this.coefficients = coefficients;
|
|||
|
|
this.rSquared = rSquared;
|
|||
|
|
this.dataCount = dataCount;
|
|||
|
|
this.equationString = generateEquationString();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 生成方程字符串
|
|||
|
|
*/
|
|||
|
|
private String generateEquationString() {
|
|||
|
|
StringBuilder sb = new StringBuilder("y = ");
|
|||
|
|
|
|||
|
|
for (int i = coefficients.size() - 1; i >= 0; i--) {
|
|||
|
|
BigDecimal coeff = coefficients.get(i);
|
|||
|
|
if (coeff.compareTo(BigDecimal.ZERO) == 0) {
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 符号处理
|
|||
|
|
if (sb.length() > 4 && coeff.compareTo(BigDecimal.ZERO) > 0) {
|
|||
|
|
sb.append(" + ");
|
|||
|
|
} else if (sb.length() > 4 && coeff.compareTo(BigDecimal.ZERO) < 0) {
|
|||
|
|
sb.append(" - ");
|
|||
|
|
coeff = coeff.abs();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 系数和变量
|
|||
|
|
if (i == 0) {
|
|||
|
|
// 常数项
|
|||
|
|
sb.append(String.format("%.4f", coeff));
|
|||
|
|
} else {
|
|||
|
|
// 非常数项
|
|||
|
|
if (coeff.compareTo(BigDecimal.ONE) != 0 && coeff.compareTo(BigDecimal.ONE.negate()) != 0) {
|
|||
|
|
sb.append(String.format("%.4f", coeff));
|
|||
|
|
}
|
|||
|
|
sb.append("x");
|
|||
|
|
if (i > 1) {
|
|||
|
|
sb.append("^").append(i);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return sb.toString();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 根据x值预测y值
|
|||
|
|
*/
|
|||
|
|
public BigDecimal predict(BigDecimal x) {
|
|||
|
|
BigDecimal result = BigDecimal.ZERO;
|
|||
|
|
BigDecimal xPower = BigDecimal.ONE;
|
|||
|
|
|
|||
|
|
for (int i = 0; i < coefficients.size(); i++) {
|
|||
|
|
result = result.add(coefficients.get(i).multiply(xPower));
|
|||
|
|
xPower = xPower.multiply(x);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public String toString() {
|
|||
|
|
return String.format("%s", equationString);
|
|||
|
|
}
|
|||
|
|
}
|