0

这是我的代码,当我构建它并创建 .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();
}
4

0 回答 0