Чтение 2 CSV-файлов и использование векторов для хранения значений, а затем вычисление коэффициента. Возвращает -1. # IND

Это мой код, который у меня есть, который при сборке работает и создает файл .exe, однако на протяжении всего процесса я печатаю функцию (т. Е. Среднее значение, ковариацию, коэффициент), и все они возвращаются как -1 # IND. Подумайте, может быть, неправильно извлекаются данные из файлов CSV?

// basic file operations
#include <iterator>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

using namespace std;

typedef vector<double> Prices;

Prices parse_csv_line(string& line)
{
Prices result;
string datum;
stringstream ss(line);

int count=0;
while(getline(ss,datum,','))
{
// convert string to
count++;
if (count%2 == 0)
result.push_back(atof(datum.c_str()));
}
return result;
}

Prices parse_csv_file(const char* filename)
{
ifstream file(filename);
Prices prices;
string line;

// This will discard the header line
getline(file, line);

// This will get each line in the file, and collate its values
while (getline(file, line))
{
Prices v = parse_csv_line(line);
prices.insert(prices.end(), v.begin(), v.end());
}
for(Prices::iterator it=prices.begin(); it != prices.end(); it++)
cout << " " << *it;
return prices;
}

//Calculate Correlation of series A and B, then return

/* Calculatethe mean averages for A and B.
(For each series, add each sample and then divide by the number of samples.) */
double CalculateMean(Prices x)
{
double sum = 0;
for(size_t i = 0; i < x.size(); i++)
sum += x[i];
return (sum / x.size());

}

/* Calculate the variance for A and B.
(First calculate the difference from the mean for each sample number. Square each number then divide by the number of samples (n).
If the numbers you are calculating represent a sample of a larger group, then you would divide by n – 1.) */
double CalculateVariance(Prices x)
{
double mean = CalculateMean(x);
double temp = 0;
for(size_t i = 0; i < x.size(); i++)
{
temp += (x[i] - mean) * (x[i] - mean) ;
}
return temp / x.size();
}

/* calculate the standard deviation for A and B, which is the square root of the variance.
(This number will tell you how closely your samples are located to the mean.) */
double Calculate_StandardDeviation(Prices x)
{
return sqrt(CalculateVariance(x));
}

/* Lastly, calculate the Covariance of the 2 series.
(This value can be used to represent the linear relationship between two variables.) */
double Calculate_Covariance(Prices x, Prices y)
{
double meanX = CalculateMean(x);
double meanY = CalculateMean(y);

cout << "mean x = " << meanX << "\n";
cout << "mean y = " << meanY << "\n";

double total = 0;

for(size_t i = 0; i < x.size(); i++)
{
total += (x[i] - meanX) * (y[i] - meanY);
}
return total / x.size();

}

// Using the calculated values, these can then be inputted into the Correlation Coefficient formula to find the correlation of series A and B.
double Calculate_Correlation(Prices x, Prices y)
{
double covariance = Calculate_Covariance(x, y);

cout << "covariance =" << covariance << "\n";

double correlation = covariance / (Calculate_StandardDeviation(x) * Calculate_StandardDeviation(y));
return correlation;
};

int main()
{
Prices a = parse_csv_file("PC1_A.CSV");
Prices b = parse_csv_file("PC1_B.CSV");

double correlation = Calculate_Correlation(a, b);

cout << "Correlation is: " << correlation;

cin.get();
}

0

Решение

Задача ещё не решена.

Другие решения


По вопросам рекламы [email protected]