Васильев N2
Вывод программы: c0 = 11 c1 = 21 c2 = 0 b0 = -499.992 b1 = 6.19998 b2 = -4.3 b3 = -2.29928
Описание: с0 = 11 = х1*х6, с1 = 21 = х4*х4, с2 = 0 = х1
Модель:
-500 + 6.2*х1*х6 - 4.3*х4*х4 - 2.3*х1
//============================================================================
// Name : CorrelCoeficent.cpp
// Author : MIHAIL_WAS
// Version :
// Copyright :
// Description : Hello World in C++, Ansi-style
//============================================================================
- include <iostream>
- include <sstream>
- include <fstream>
- include <cstring>
- include <cmath>
- include <vector>
using namespace std;
int const numParametrs = 7; int const numVar = 3;
double** AllocationMatrix (double **x, int dim1, int dim2) { x = new double*[dim1]; for (int i = 0; i < dim1; i++) { x[i] = new double[dim2]; } return x; }
class InputData { public: int size; double *array; double m1; double m2; double sigma; string name;
public: InputData (const double *_array, int _size) { size = _size; array = new double[size]; for (int i = 0; i < size; i++) { array[i] = _array[i]; } }
void parametrs () { m1 = 0; for (int i = 0; i < size; i++) { m1 += array[i]; }
m1 /= size;
m2 = 0; for (int i = 0; i < size; i++) { m2 += (array[i] - m1)*(array[i] - m1); }
m2 /= (size - 1); sigma = pow(m2, 0.5); }
void print() { for (int i = 0; i < size; i++) { cout << array[i] << " "; } cout << endl; }
~InputData() {
delete[] array;
}
};
void correlation_coefficient0 (InputData *x, InputData *y, double *r) { double m1x = x->m1, m1y = y->m1;
for (int i = 0; i < x->size; i++) { *r += (x->array[i] - m1x)*(y->array[i] - m1y); }
*r /= ((x->size - 1)*x->sigma*y->sigma); }
void correlation_coefficient1(InputData *x1, InputData *x2, double *r, double *arrayR, int i, int imax) { double rx1x2 = 0; correlation_coefficient0(x1, x2, &rx1x2);
*r = (arrayR[i] - arrayR[imax]*rx1x2)/(pow( (1 - arrayR[imax]*arrayR[imax])*(1 - rx1x2*rx1x2), 0.5)); }
void correlation_coefficient2(InputData *x1, InputData *x2, InputData *x3, InputData *y, double *r, double *arrayR1, double *arrayR2, int i, int imax1, int imax2) {
double a = arrayR2[i]; double b = arrayR2[imax2]; double c0 = 0, c1 = 0, c2 = 0;
correlation_coefficient0(x1, x3, &c0); correlation_coefficient0(x1, x2, &c1); correlation_coefficient0(x2, x3, &c2);
double c = (c0 - c1*c2)/(pow((1 - c1*c1)*(1 - c2*c2), 0.5)); *r = (a - b*c)/(pow((1 - b*b)*(1 - c*c), 0.5)); }
vector< InputData* > adding_parametrs (vector <InputData*> v, int N) {
int size = v.size(); double *x = new double[N]; for (int i = 0; i < size; i++) {
for (int j = i; j < size; j++) {
for (int e = 0; e < N; e++) { x[e] = v[i]->array[e] * v[j]->array[e]; } v.push_back(new InputData(x, N)); } }
return v; }
vector< InputData* > import_data(int size) { vector < InputData* > parametrs; ifstream file ("data.inp", ios::out); string str = ""; int index = 0; double **x = AllocationMatrix(x, size, numParametrs - 1); double *y = new double[size]; double *current_x = new double[size];
while(getline(file, str)) {
string substr = ""; int count = 0; for (unsigned int i = 0; i < str.length(); i++) {
if ((int)str[i] != 9) { substr += str[i]; } else { count++;
if (count == 1) { index = atoi(substr.c_str()); } else { x[index - 1][count - 2] = atoi(substr.c_str()); }
substr = ""; } }
y[index - 1] = atof(substr.c_str()); }
for (int i = 0; i < numParametrs - 1; i++) { for (int j = 0; j < size; j++) { current_x[j] = x[j][i]; }
InputData *memberOfClass = new InputData(current_x, size); parametrs.push_back(memberOfClass); } parametrs = adding_parametrs(parametrs, size); InputData *memberOfClass = new InputData(y, size); parametrs.push_back(memberOfClass); file.close(); return parametrs; }
void TransposeMatrix (double **x, double **xT, int size_1, int size_2) { for (int i = 0; i < size_1; i++) { for (int j = 0; j < size_2; j++) { xT[j][i] = x[i][j]; } } }
void MultMatrix(double **x, double **y, double **c, int size_11, int size_22, int size_12) { for (int i = 0; i < size_11; i++) { for (int j = 0; j < size_22; j++) { for (int k = 0; k < size_12; k++) { c[i][j] += x[i][k] * y[k][j]; } } } }
void reverseMatrix (double **x, int size) { double **e = AllocationMatrix(e, size, size); double a = 0;
for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) i == j ? e[i][j] = 1 : e[i][j] = 0;
for(int i = 0; i < size; i++){
a = x[i][i]; for(int j = 0 ; j < size; j++){ x[i][j] /= a; e[i][j] /= a; }
for (int k = i + 1; k < size; k++ ){
a = x[k][i]; for(int j = 0; j < size; j++){ x[k][j] -= x[i][j] * a; e[k][j] -= e[i][j] * a; } } }
for(int i = size - 1; i >0; i--){ for(int j = i - 1; j >= 0; j--){
a = x[j][i]; for(int k = 0; k < size; k++){ x[j][k] -= x[i][k] * a; e[j][k] -= e[i][k] * a; } } }
for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) x[i][j] = e[i][j]; }
double* MNK_second_method(double *x1, double *x2, double *x3, double *y, int size) {
double **x = AllocationMatrix(x, size, 4); double **yMatrix = AllocationMatrix(yMatrix, size, 1); double **xT = AllocationMatrix(xT, numVar + 1, size); double **x_xT = AllocationMatrix(x_xT, numVar + 1, numVar + 1); double **x_xT_xT = AllocationMatrix(x_xT_xT, numVar + 1, size); double **B = AllocationMatrix(B, numVar + 1, 1);
for (int i = 0; i < size; i++) { x[i][0] = 1; x[i][1] = x1[i]; x[i][2] = x2[i]; x[i][3] = x3[i]; yMatrix[i][0] = y[i]; }
TransposeMatrix(x, xT, size, numVar + 1); MultMatrix(xT, x, x_xT, numVar + 1, numVar + 1, size); reverseMatrix(x_xT, numVar + 1); MultMatrix(x_xT, xT, x_xT_xT, numVar + 1, size, numVar + 1); MultMatrix(x_xT_xT, yMatrix, B, numVar + 1, 1, size);
double *B_final = new double[numVar + 1]; for (int i = 0; i < numVar + 1; i++) { B_final[i] = B[i][0]; }
return B_final; }
int main() { int N = 50;
vector < InputData* > parametrs; parametrs = import_data(N);
for (unsigned int i = 0; i < parametrs.size(); i++) { parametrs[i]->parametrs(); }
double *r0 = new double[parametrs.size() - 1]; int max_correlationIndex = 0; double max_correlationCoefficient = -1;
for (int i = 0; i < parametrs.size() - 1; i++) { double r = 0; correlation_coefficient0(parametrs[i], parametrs[parametrs.size() - 1], &r); r0[i] = r;
if (fabs(r0[i]) > max_correlationCoefficient) { max_correlationIndex = i; max_correlationCoefficient = fabs(r0[i]); } }
int max_correlationIndex1 = 0; max_correlationCoefficient = -1; int j = 0; double *r1 = new double[parametrs.size() - 1];
for (int i = 0; i < parametrs.size() - 1; i++) { double r = 0;
if (i != max_correlationIndex) { correlation_coefficient1(parametrs[i], parametrs[max_correlationIndex], &r, r0, i, max_correlationIndex); r1[j] = r;
if (fabs(r) > max_correlationCoefficient) { max_correlationCoefficient = fabs(r); max_correlationIndex1 = j; } j++; } else { r1[j] = 0; j++; } }
int max_correlationIndex2 = 0; max_correlationCoefficient = -1; j = 0; double *r2 = new double[parametrs.size() - 3];
for (int i = 0; i < parametrs.size() - 1; i++) { double r = 0;
if ((i != max_correlationIndex) && (i != max_correlationIndex1)) {
correlation_coefficient2(parametrs[i], parametrs[max_correlationIndex], parametrs[max_correlationIndex1], parametrs[parametrs.size() - 1], &r, r0, r1, i, max_correlationIndex, max_correlationIndex1); r2[j] = r;
if (fabs(r) > max_correlationCoefficient) { max_correlationCoefficient = fabs(r); max_correlationIndex2 = j; } j++; } }
double *mnk = new double[numVar + 1]; mnk = MNK_second_method(parametrs[max_correlationIndex]->array, parametrs[max_correlationIndex1]->array, parametrs[max_correlationIndex2]->array, parametrs[parametrs.size() - 1]->array, 50);
cout << "c0 = " << max_correlationIndex << " c1 = " << max_correlationIndex1 << " c2 = " << max_correlationIndex2 << endl; cout << "b0 = " << mnk[0] << " b1 = " << mnk[1] << " b2 = " << mnk[2] << " b3 = " << mnk[3] << endl;
return 0; }