-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppAllocation.m
109 lines (87 loc) · 3.88 KB
/
AppAllocation.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
classdef AppAllocation < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
UIAxes matlab.ui.control.UIAxes
RiskToleranceSliderLabel matlab.ui.control.Label
RiskToleranceSlider matlab.ui.control.Slider
NumBalancing = evalin('base','NumBalancing');
CovMat = evalin('base','Covariance.CovMat');
Mean = evalin('base','DynamicAllocation.mean');
RF = evalin('base','Returns_RF'); % Period Risk Free rate
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
value = 7.5;
Allocation = zeros(3,app.NumBalancing);
for i = 1:app.NumBalancing
Allocation(1:2,i) = 1/value*inv(app.CovMat(:,:,i))*...
(app.Mean(i,:)'-ones(2,1)*app.RF(i+2));
Allocation(3,i) = 1 - ones(1,2)*Allocation(1:2,i);
end
plot(app.UIAxes,linspace(0,app.NumBalancing-1,app.NumBalancing),Allocation);
end
% Value changed function: RiskToleranceSlider
function RiskToleranceSliderValueChanged(app, event)
value = app.RiskToleranceSlider.Value;
Allocation = zeros(3,app.NumBalancing);
for i = 1:app.NumBalancing
Allocation(1:2,i) = 1/value*inv(app.CovMat(:,:,i))*...
(app.Mean(i,:)'-ones(2,1)*app.RF(i+2));
Allocation(3,i) = 1 - ones(1,2)*Allocation(1:2,i);
end
plot(app.UIAxes,linspace(0,app.NumBalancing-1,app.NumBalancing),Allocation);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Dynamic Weights Allocation')
xlabel(app.UIAxes, 'Number of period')
ylabel(app.UIAxes, 'Weights')
app.UIAxes.Position = [56 143 529 276];
% Create RiskToleranceSliderLabel
app.RiskToleranceSliderLabel = uilabel(app.UIFigure);
app.RiskToleranceSliderLabel.HorizontalAlignment = 'right';
app.RiskToleranceSliderLabel.Position = [164 80 84 22];
app.RiskToleranceSliderLabel.Text = 'Risk Aversion';
% Create RiskToleranceSlider
app.RiskToleranceSlider = uislider(app.UIFigure);
app.RiskToleranceSlider.Limits = [0 15];
app.RiskToleranceSlider.ValueChangedFcn = createCallbackFcn(app, @RiskToleranceSliderValueChanged, true);
app.RiskToleranceSlider.Position = [269 89 150 3];
app.RiskToleranceSlider.Value = 7.5;
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = AppAllocation
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @(app)startupFcn(app))
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end