因子分析法 C++代码
时间: 2023-09-11 16:04:17 浏览: 128
层次分析法的C++简单代码
5星 · 资源好评率100%
以下是一个简单的 C++ 代码示例,用于执行因子分析:
```cpp
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
// 计算均值
double mean(vector<double> data) {
double sum = 0.0;
for (int i = 0; i < data.size(); i++) {
sum += data[i];
}
return sum / data.size();
}
// 计算方差
double variance(vector<double> data) {
double m = mean(data);
double var = 0.0;
for (int i = 0; i < data.size(); i++) {
var += pow(data[i] - m, 2);
}
return var / (data.size() - 1);
}
// 计算协方差矩阵
vector<vector<double>> covarianceMatrix(vector<vector<double>> data) {
int n = data.size();
int m = data[0].size();
vector<vector<double>> cov(n, vector<double>(n, 0.0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
double sum = 0.0;
for (int k = 0; k < m; k++) {
sum += (data[i][k] - mean(data[i])) * (data[j][k] - mean(data[j]));
}
cov[i][j] = sum / (m - 1);
}
}
return cov;
}
// 计算特征值和特征向量
void eigen(vector<vector<double>> matrix, vector<double>& eigenvalues, vector<vector<double>>& eigenvectors) {
int n = matrix.size();
vector<vector<double>> A(n, vector<double>(n, 0.0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = matrix[i][j];
}
}
eigenvectors.resize(n, vector<double>(n, 0.0));
eigenvalues.resize(n, 0.0);
vector<double> d(n, 0.0), e(n, 0.0);
int l, k, j, i;
double scale, hh, h, g, f;
for (i = n - 1; i > 0; i--) {
l = i - 1;
h = scale = 0.0;
if (l > 0) {
for (k = 0; k <= l; k++) {
scale += abs(A[i][k]);
}
if (scale == 0.0) {
e[i] = A[i][l];
}
else {
for (k = 0; k <= l; k++) {
A[i][k] /= scale;
h += A[i][k] * A[i][k];
}
f = A[i][l];
g = f > 0 ? -sqrt(h) : sqrt(h);
e[i] = scale * g;
h -= f * g;
A[i][l] = f - g;
f = 0.0;
for (j = 0; j <= l; j++) {
A[j][i] = A[i][j] / h;
g = 0.0;
for (k = 0; k <= j; k++) {
g += A[j][k] * A[i][k];
}
for (k = j + 1; k <= l; k++) {
g += A[k][j] * A[i][k];
}
e[j] = g / h;
f += e[j] * A[i][j];
}
hh = f / (h + h);
for (j = 0; j <= l; j++) {
f = A[i][j];
e[j] = g = e[j] - hh * f;
for (k = 0; k <= j; k++) {
A[j][k] -= (f * e[k] + g * A[i][k]);
}
}
}
}
else {
e[i] = A[i][l];
}
d[i] = h;
}
d[0] = 0.0;
e[0] = 0.0;
for (i = 0; i < n; i++) {
if (d[i] != 0.0) {
for (j = 0; j < i; j++) {
g = 0.0;
for (k = 0; k < i; k++) {
g += A[i][k] * A[k][j];
}
for (k = 0; k < i; k++) {
A[k][j] -= g * A[k][i];
}
}
}
d[i] = A[i][i];
A[i][i] = 1.0;
for (j = 0; j < i; j++) {
A[j][i] = A[i][j] = 0.0;
}
}
for (i = 0; i < n - 1; i++) {
k = i;
double p = d[i];
for (j = i + 1; j < n; j++) {
if (d[j] > p) {
k = j;
p = d[j];
}
}
if (k != i) {
d[k] = d[i];
d[i] = p;
for (j = 0; j < n; j++) {
p = A[j][i];
A[j][i] = A[j][k];
A[j][k] = p;
}
}
}
for (i = 0; i < n; i++) {
eigenvalues[i] = d[i];
for (j = 0; j < n; j++) {
eigenvectors[i][j] = A[j][i];
}
}
}
// 执行因子分析
void factorAnalysis(vector<vector<double>> data, int k) {
int n = data.size();
int m = data[0].size();
vector<vector<double>> cov = covarianceMatrix(data);
vector<double> eigenvalues(n, 0.0);
vector<vector<double>> eigenvectors(n, vector<double>(n, 0.0));
eigen(cov, eigenvalues, eigenvectors);
vector<pair<double, int>> ev(n);
for (int i = 0; i < n; i++) {
ev[i] = make_pair(eigenvalues[i], i);
}
sort(ev.rbegin(), ev.rend());
vector<vector<double>> factors(n, vector<double>(k, 0.0));
for (int i = 0; i < k; i++) {
for (int j = 0; j < n; j++) {
factors[j][i] = eigenvectors[j][ev[i].second];
}
}
vector<vector<double>> loadings(m, vector<double>(k, 0.0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
double sum = 0.0;
for (int l = 0; l < n; l++) {
sum += factors[l][j] * (data[l][i] - mean(data[l]));
}
loadings[i][j] = sum / sqrt(variance(data[i]));
}
}
cout << "Factors:\n";
for (int i = 0; i < n; i++) {
cout << "Factor " << i + 1 << ": ";
for (int j = 0; j < k; j++) {
cout << factors[i][j] << " ";
}
cout << "\n";
}
cout << "Loadings:\n";
for (int i = 0; i < m; i++) {
cout << "Variable " << i + 1 << ": ";
for (int j = 0; j < k; j++) {
cout << loadings[i][j] << " ";
}
cout << "\n";
}
}
int main() {
ifstream infile("data.txt");
vector<vector<double>> data;
double value;
while (infile >> value) {
vector<double> row;
row.push_back(value);
for (int i = 0; i < 4; i++) {
infile >> value;
row.push_back(value);
}
data.push_back(row);
}
factorAnalysis(data, 2);
return 0;
}
```
需要注意的是,此代码只是一个简单的示例,可能需要根据实际情况进行调整和优化。
阅读全文