using namespace std;
class corr_coefficient
{
matrix x, y;
double sum, C, corr[5];
int j;
public:
double calc_mu(matrix x, int j=0);
double calc_covariance(matrix x, matrix y, int j);
double calc_correlation();
double StandardDeviation(matrix a, int j=0);
double return(int i){return corr[i];
};
corr_coefficient(matrix x, matrix y);
};
double corr_coefficient::calc_mu(matrix x, int j)
{
sum=0;
for(int i=0; i<C; i++)
{
sum = sum+x(i,j);
}
return sum/C;
}
double corr_coefficient::calc_covariance(matrix x, matrix y, int j)
{
double mux=calc_mu(x,0);
double muy=calc_mu(y,j);
sum=0;
for (int i=0; i<C; i++)
{
sum=sum+(x(i,0)-mux)*(y(i,j)-muy);
}
return sum/(C-1);
}
double corr_coefficient::StandardDeviation(matrix a, int j)
{
double mua=calc_mu(a,j);
sum=0;
for (int i=0; i<C; i++)
{
sum=sum+((a(i,j))-mua)*((a(i,j))-mua);
}
return sqrt(sum/(C-1));
}
corr_coefficient::corr_coefficient(matrix x, matrix y)
{
C=35;
for (int j=0; j<5; j++)
{
corr[j]=calc_covariance(x, y, j)/(StandardDeviation(x, 0)*StandardDeviation(y, j));
}
}
答案 0 :(得分:3)
double return(int i){return corr[i];
};
return
是一个关键字。您不能拥有名为return
的函数。只是称它为不同的东西。或许operator[]
适合在这里重载?
double operator[](int i){ return corr[i]; }