-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemos.m
88 lines (71 loc) · 2.03 KB
/
demos.m
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
% This file is part of OctCLA, Copyright (c) 2022 Josh Fogg, released
% under the MIT License. See: https://github.com/Foggalong/OctCLA
% Below we demonstrate using OctCLA to solve a traditional portfolio
% optimization problem by loading data from a file, followed by an
% optimal contribution selection problem where input data is defined
% through in-code variables. The computed turning points are checked
% both using the KKT conditions and against the known solutions.
% set output formatting
format compact
format long
disp("FINANCE CODE DEMO")
addpath(genpath("finance/")) % need functions in path
% read data from input file into variables
data = csvread("demos/data/input.csv");
mu = data(2,:)';
lb = data(3,:)';
ub = data(4,:)';
covar = data(5:end,:);
% calculate turning points using OctCLA finance functions
tic
sols = calculate_turningpoints(mu, covar, lb, ub, 3)';
toc
% write output to file for reference
csvwrite("demos/data/output.csv", sols)
% read truth into matrix
truth = csvread("demos/data/truth.csv");
% check if any entry in absolute error matrix greater than tolerance
tol = 1e-10;
abs_error = abs(truth - sols);
if any(any(abs_error > tol) > 0)
disp("ERROR!")
abs_error
else
disp("Success!")
end
disp("") % padding
disp("GENETICS CODE DEMO")
addpath(genpath("genetics/")) % need functions in path
% set up the problem manually using variables
S = [1,2];
D = [3,4];
mu = [4; 1; 3; 2];
lb = repmat(0.0, 4, 1);
ub = repmat(1.0, 4, 1);
covar = [
2, 0, 0, 0;
0, 1, 0, 0;
0, 0, 2, 0;
0, 0, 0, 1;
];
% true turning points of this problem
truth = [
0.5, 0.0, 0.5, 0.0;
0.5, 0.0, 5/18, 2/9;
1/6, 1/3, 1/3, 1/6
]';
% calculate turning points using OctCLA genetics functions
tic
sols = calculate_turningpoints_gen(mu, covar, lb, ub, S, D, 3)';
toc
% return turning points to terminal
sols
% check if any entry in absolute error matrix greater than tolerance
tol = 1e-10;
abs_error = abs(truth - sols);
if any(any(abs_error > tol) > 0)
disp("ERROR!")
abs_error
else
disp("Success!")
end