/** * 數據封裝二維數組並調用計算兩個二維數組協方差的方法 */ @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