二维数组计算协方差java

/**
 *     数据封装二维数组并调用计算两个二维数组协方差的方法
 */
@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的sql

<!-- 查询某个因子的某天到某天的值 -->
<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的意思是求均值。

X为一个因子的126个数据,E(X)为X的均值。

Y是另一个因子的126个数据,E(Y)为Y的均值。

把对应的126个X和126个Y相乘,得到第三组数据有126个,对第三组数据求均值,得到E(X*Y)。

转载于:https://my.oschina.net/u/3526783/blog/994188

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值