-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
50 lines (38 loc) · 1.18 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#define EIGEN_RUNTIME_NO_MALLOC
#include <Eigen/Dense>
#include <iostream>
#include "ipm.h"
using Eigen::MatrixXd;
using Eigen::VectorXd;
int main(int argc, char** argv) {
int n = 10;
// Setup problem data
double gamma = 0.5; // Risk aversion parameter
Eigen::MatrixXd A(1, n);
Eigen::MatrixXd Q(n, n); // Return Covariance
Eigen::MatrixXd G(n, n);
Eigen::VectorXd q(n); // Expected Return
Eigen::VectorXd b(1);
Eigen::VectorXd h(n);
// Generate random return covariance matrix (PSD matrix)
Q.setRandom();
Q = Q.transpose().eval() + Q;
Q += n * Eigen::MatrixXd::Identity(n, n);
Q *= gamma;
// Generate random expected return
q.setRandom();
// No shorting (non-negativity constraint)
G = -Eigen::MatrixXd::Identity(n, n);
h.setZero(n);
// Variables represent fraction of investment
A.setConstant(1);
b << 1;
Solver solver(Q, q, G, h, A, b);
solver.solve();
// Check solver status
std::cout << "Converged?" << std::endl;
std::cout << solver.converged() << std::endl;
// Print optimal portfolio
std::cout << "x_opt:" << std::endl;
std::cout << solver.vars.x << std::endl;
}