线性相关系数(Pearson product-moment correlation coefficient)-线性相关

double coefficient(double acuan[],double sample[],int start1,int start2,int m)
 {
	 int Length=m;

	 double sum_sam=0;
	 double avg_sam=0;
	 double sum_acu=0;
	 double avg_acu=0;
	 for(int i=start1,j=start2;i<(start1+Length),j<(start2+Length);i++,j++)
	 {
		 sum_acu=sum_acu+acuan[i];
		 sum_sam=sum_sam+sample[j];
		 
	 }
	 avg_sam=sum_sam/Length;
	 avg_acu=sum_acu/Length;

	 double coeff=-1;
	 double top=0;
	 double bottom=0;
	 double bottom1=0;
	 double bottom2=0;
	 for(int i=start1,j=start2;i<(start1+Length),j<(start2+Length);i++,j++)
	 {
		 top=top+(acuan[i]-avg_acu)*(sample[j]-avg_sam);
		 bottom1=bottom1+(acuan[i]-avg_acu)*(acuan[i]-avg_acu);
		 bottom2=bottom2+(sample[j]-avg_sam)*(sample[j]-avg_sam);
	 }
	 bottom=sqrt(bottom1*bottom2);

	 if(bottom !=0)
	 {
		 coeff=top/bottom;
	 }
	 else
	 {
		 printf("the bottom is 0\n");
	 }

	 return coeff;
 }
//double acuan[],double sample[]   输入两个向量
//int start1,int start2   截取向量的位置
//int m       进行相关性计算的两个向量的长度

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值