java 线性数组_Java 计算两个数组之间的(线性)相关性。

//package com.nowjava;//n o w j a v a . c o m - 时 代 Java

public class Main {

public static void main(String[] argv) throws Exception {

double[] v1 = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,

37.1234, 67.2344, 68.34534, 69.87700 };

double[] v2 = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,

37.1234, 67.2344, 68.34534, 69.87700 };

System.out.println(correlation(v1, v2));

}

/**

* Computes the (linear) correlation between two arrays.

* Squaring this result and multiply by 100 gives you

* the percentage of correlation.

*/

public static double correlation(double[] v1, double[] v2) {

double denom = Math.sqrt(variance(v1) * variance(v2));

if (denom != 0)

return (covariance(v1, v2) / denom);

else {

if ((variance(v1) == 0) && (variance(v2) == 0))

return (1.0);

else

return (0.0); // impossible to correlate a null signal with another/**来自 N o w J a v a . c o m - 时代Java**/

}

}

/**

* Computes the (linear) correlation between two arrays.

* Squaring this result and multiply by 100 gives you

* the percentage of correlation.

*/

public static double correlation(int[] v1, int[] v2) {

double denom = Math.sqrt(variance(v1) * variance(v2));

if (denom != 0)

return (covariance(v1, v2) / denom);

else {

if ((variance(v1) == 0) && (variance(v2) == 0))

return (1.0);

else

return (0.0); // impossible to correlate a null signal with another

}

}

/**

* Computes the (bias-corrected sample) variance.

*/

public static double variance(double[] v) {

if (v.length > 1) {

final double m = mean(v);

double ans = 0.0;

for (int i = 0; i < v.length; i++)

ans += (v[i] - m) * (v[i] - m);

return ans / (v.length - 1);

} else

throw new IllegalArgumentException(

"Array length must be of 2 or greater.");

}

/**

* Computes the (bias-corrected sample) variance.

*/

public static double variance(int[] v) {

if (v.length > 1) {

final double m = mean(v);

double ans = 0.0;

for (int i = 0; i < v.length; i++)

ans += (v[i] - m) * (v[i] - m);

return ans / (v.length - 1);

} else

throw new IllegalArgumentException(

"Array length must be of 2 or greater.");

}

/**

* Computes the covariance.

*/

public static double covariance(double[] v1, double[] v2) {

if (v1.length != v2.length)

throw new IllegalArgumentException(

"Arrays must have the same length : " + v1.length

+ ", " + v2.length);

final double m1 = mean(v1);

final double m2 = mean(v2);

double ans = 0.0;

for (int i = 0; i < v1.length; i++)

ans += (v1[i] - m1) * (v2[i] - m2);

return ans / (v1.length - 1);

}

/**

* Computes the covariance.

*/

public static double covariance(int[] v1, int[] v2) {

if (v1.length != v2.length)

throw new IllegalArgumentException(

"Arrays must have the same length : " + v1.length

+ ", " + v2.length);

final double m1 = mean(v1);

final double m2 = mean(v2);

double ans = 0.0;

for (int i = 0; i < v1.length; i++)

ans += (v1[i] - m1) * (v2[i] - m2);

return ans / (v1.length - 1);

}

/**

* Computes the mean.

*/

public static double mean(double[] v) {

if (v.length == 0)

throw new IllegalArgumentException(

"Nothing to compute! The array must have at least one element.");

return (mass(v) / (double) v.length);

}

/**

* Computes the mean.

*/

/**代码未完, 请加载全部代码(NowJava.com).**/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值