/** * 数据封装二维数组并调用计算两个二维数组协方差的方法 */ @Override public double[][] selectCovariance(int i) { double[][] allList1 = new double[45][]; double[][] allList2 = new double[45][]; //======================时间计算========================= Calendar cal = Calendar.getInstance(); Date now = cal.getTime(); //当前时间减1天 为查询结束时间 cal.add(Calendar.DATE, -1); Date endDate = cal.getTime(); //当前时间减i天 为开始查询时间 cal.add(Calendar.DATE, -i); Date benginDate = cal.getTime(); //格式化日期 String endDateString = dateFormat.format(endDate); String benginDateString = dateFormat.format(benginDate); //======================================================== //查询出全部的行业因子名称 List<String> industrys = ipbFactorRateMapper.selectIndustry(); for (int j=0;j<industrys.size();j++) { //根据行1·业因子名称,2·开始时间,3·结束时间,查一个因子固定时间内的全部值 ====后附sql语句==== List<Double> bigList = ipbFactorRateMapper.selectCovariance(industrys.get(j),benginDateString, endDateString); double[] array = new double[bigList.size()]; for(int v = 0; v<bigList.size(); v++){ double decimal = bigList.get(v); array[v] = decimal; } //把每个拼起来的一维数组 放到二维数组里面 allList1[j]=array; } allList2 = allList1; return doubleArr(allList1,allList2,benginDate,endDate); } /** * 计算两个二维数组的协方差 * @param X * @param Y * @return */ public double[][] doubleArr(double[][] X,double[][] Y,Date benginDate,Date endDate){ double[][] covXY=new double[45][45]; //建立一个相同的String数组,用来存放数据库 String[][] c=new String[45][45]; for (int i=0;i<X.length;i++){ double avgX=calculateAvg( X[i]); for (int j=0;j<Y.length;j++){ //求数组平均值 double avgY=calculateAvg( Y[j]); double avgXY=calculateMultiplyAvg( X[i], Y[j]); //相乘以后的平均值 减去 数组平均值相乘 double result=avgXY-(avgX*avgY); covXY[i][j]=result; //格式化科学计数法 DecimalFormat decimalFormat = new DecimalFormat("#,##0.000000");//格式化设置 String format = decimalFormat.format(covXY[i][j]); c[i][j] = format; } } //添加到数据库 IpbAvgCovariance covariance = new IpbAvgCovariance(); String string = JSONArray.fromObject(c).toString(); covariance.setCovariance(string); covariance.setBegindate(benginDate); covariance.setEnddate(endDate); covariance.setDate(new Date()); covarianceMapper.insertSelective(covariance); return covXY; } /** * 求数组平均值 * @param arr 传一个一维数组 * @return */ public double calculateAvg(double arr[]) { double avg=0; for(int i=0;i<arr.length;i++) { avg+=arr[i]; } //数组里面的数据相加求和,再求平均数 avg=avg/arr.length; return avg; } /** * 求XY相乘以后的平均值 * @param x * @param y * @return */ public double calculateMultiplyAvg(double x[],double y[]){ int len=x.length; double a[]=new double[len]; for(int i=0;i<x.length;i++){ if(i<y.length){ //先把两个数组的同角标相乘,放到新的数组中 a[i]=x[i]*y[i]; } } //调用求平均值的方法 return calculateAvg(a); }
下面这是用的myBatis的sqlsql
<!-- 查询某个因子的某天到某天的值 --> <select id="selectCovariance" resultType="Double"> select rate_value from ipb_factor_rate where industry =#{industry} AND trade_date BETWEEN #{bengin} AND #{end} GROUP BY trade_date </select>
具体理解能够看这个公式:数据库
Cov (X,Y) = E(X*Y) − E(X)*E(Y)数组
E的意思是求均值。app
X为一个因子的126个数据,E(X)为X的均值。ide
Y是另外一个因子的126个数据,E(Y)为Y的均值。orm
把对应的126个X和126个Y相乘,获得第三组数据有126个,对第三组数据求均值,获得E(X*Y)。ip