为稳定盈利 提供动力

这里是我的工作总结和我感性上的碎碎念 3BFund Quant leader CFA 从业6Y Email: wongmanc@88.com

一个简单的量化投资部数学库搭建范本(工作记录)源码


MathLib/
├── include/
│ ├── linear_algebra.hpp
│ ├── probability.hpp
│ └── numerical_analysis.hpp
└── src/
├── linear_algebra.cpp
├── probability.cpp
└── numerical_analysis.cpp

// linear_algebra.hpp
#ifndef LINEAR_ALGEBRA_HPP
#define LINEAR_ALGEBRA_HPP

#include <vector>

namespace MathLib {

class Matrix {
public:
Matrix(int rows, int cols);
void setElement(int row, int col, double value);
double getElement(int row, int col) const;
Matrix multiply(const Matrix& other) const;

private:
int rows_, cols_;
std::vector<std::vector<double>> elements_;
};

} // namespace MathLib

#endif

// linear_algebra.cpp
#include “linear_algebra.hpp”

namespace MathLib {

Matrix::Matrix(int rows, int cols) : rows_(rows), cols_(cols), elements_(rows, std::vector<double>(cols, 0.0)) {}

void Matrix::setElement(int row, int col, double value) {
elements_[row][col] = value;
}

double Matrix::getElement(int row, int col) const {
return elements_[row][col];
}

Matrix Matrix::multiply(const Matrix& other) const {
// 简化的乘法实现,不包含错误检查
Matrix result(rows_, other.cols_);
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < other.cols_; ++j) {
for (int k = 0; k < cols_; ++k) {
result.elements_[i][j] += elements_[i][k] * other.elements_[k][j];
}
}
}
return result;
}

} // namespace MathLib

 

头文件probability.hpp:Copy code
// probability.hpp
#ifndef PROBABILITY_HPP
#define PROBABILITY_HPP

namespace MathLib {

double normalDistribution(double mean, double stddev, double x);

} // namespace MathLib

#endif

// probability.cpp
#include “probability.hpp”
#include <cmath>

namespace MathLib {

double normalDistribution(double mean, double stddev, double x) {
return (1.0 / (stddev * sqrt(2.0 * M_PI))) * exp(-0.5 * pow((x – mean) / stddev, 2));
}

} // namespace MathLib

 

// numerical_analysis.hpp
#ifndef NUMERICAL_ANALYSIS_HPP
#define NUMERICAL_ANALYSIS_HPP

namespace MathLib {

double integrate(double (*f)(double), double a, double b, int n);

} // namespace MathLib

#endif

 

// numerical_analysis.cpp
#include “numerical_analysis.hpp”

namespace MathLib {

double integrate(double (*f)(double), double a, double b, int n) {
double h = (b – a) / n;
double sum = 0.5 * (f(a) + f(b));
for (int i = 1; i < n; ++i) {
sum += f(a + i * h);
}
return sum * h;
}

} // namespace MathLib

#include <iostream>
#include <vector>
#include <cmath>
#include <functional>

// 线性代数基础类
class Matrix {
public:
Matrix(unsigned rows, unsigned cols) : _rows(rows), _cols(cols) {
_data.resize(rows);
for (auto& row : _data)
row.resize(cols, 0);
}

double& operator()(unsigned row, unsigned col) {
return _data[row][col];
}

const double& operator()(unsigned row, unsigned col) const {
return _data[row][col];
}

unsigned rows() const { return _rows; }
unsigned cols() const { return _cols; }

// 矩阵乘法
Matrix multiply(const Matrix& other) const {
Matrix result(_rows, other._cols);
for (unsigned i = 0; i < _rows; ++i)
for (unsigned j = 0; j < other._cols; ++j)
for (unsigned k = 0; k < _cols; ++k)
result(i, j) += (*this)(i, k) * other(k, j);
return result;
}

private:
std::vector<std::vector<double>> _data;
unsigned _rows, _cols;
};

// 概率分布函数:正态分布的PDF
double normalPDF(double x, double mu = 0.0, double sigma = 1.0) {
return (1.0 / (sigma * sqrt(2 * M_PI))) * exp(-0.5 * pow((x – mu) / sigma, 2));
}

// 数值分析:梯形积分
double trapezoidalIntegration(const std::function<double(double)>& f, double a, double b, int n) {
double h = (b – a) / n;
double sum = 0.5 * (f(a) + f(b));
for (int i = 1; i < n; ++i) {
sum += f(a + i * h);
}
return sum * h;
}

int main() {
// 使用Matrix类
Matrix A(2, 2), B(2, 2);
A(0, 0) = 1; A(0, 1) = 2; A(1, 0) = 3; A(1, 1) = 4;
B(0, 0) = 2; B(0, 1) = 0; B(1, 0) = 1; B(1, 1) = 3;
auto C = A.multiply(B);
std::cout << “Matrix multiplication result: ” << C(0, 0) << “, ” << C(0, 1) << “, ” << C(1, 0) << “, ” << C(1, 1) << std::endl;

// 使用normalPDF
std::cout << “PDF of normal distribution at x=1: ” << normalPDF(1) << std::endl;

// 使用trapezoidalIntegration
auto result = trapezoidalIntegration([](double x) { return x * x; }, 0, 1, 1000);
std::cout << “Integration of x^2 from 0 to 1: ” << result << std::endl;

return 0;
}