📄 gup.m
字号:
function varargout = gup(varargin)
% GUP M-file for gup.fig
% GUP, by itself, creates a new GUP or raises the existing
% singleton*.
%
% H = GUP returns the handle to a new GUP or the handle to
% the existing singleton*.
%
% GUP('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUP.M with the given input arguments.
%
% GUP('Property','Value',...) creates a new GUP or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before gup_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to gup_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help gup
% Last Modified by GUIDE v2.5 24-Sep-2004 14:15:46
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gup_OpeningFcn, ...
'gui_OutputFcn', @gup_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before gup is made visible.
function gup_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to gup (see VARARGIN)
% Choose default command line output for gup
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gup wait for user response (see UIRESUME)
% uiwait(handles.figure1);
global states
global d
global ll
global lll
global val
global zhsh
global gp
states=zeros(1,10);
d=250;
ll=0;lll=0;val=1;
save shuju;
% --- Outputs from this function are returned to the command line.
function varargout = gup_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in radiobutton1.function radiobutton1_Callback(hObject, eventdata, handles)% hObject handle to radiobutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of radiobutton1% --- Executes on button press in radiobutton2.function radiobutton2_Callback(hObject, eventdata, handles)% hObject handle to radiobutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of radiobutton2% --- Executes on button press in radiobutton3.function radiobutton3_Callback(hObject, eventdata, handles)% hObject handle to radiobutton3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of radiobutton3% --- Executes on button press in radiobutton4.function radiobutton4_Callback(hObject, eventdata, handles)% hObject handle to radiobutton4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of radiobutton4% --- Executes on button press in radiobutton5.function radiobutton5_Callback(hObject, eventdata, handles)% hObject handle to radiobutton5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of radiobutton5% --- Executes on button press in radiobutton6.function radiobutton6_Callback(hObject, eventdata, handles)% hObject handle to radiobutton6 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of radiobutton6% --- Executes on button press in radiobutton7.function radiobutton7_Callback(hObject, eventdata, handles)% hObject handle to radiobutton7 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)
M_A=get(handles.radiobutton7,'Value')
if M_A==1set(handles.radiobutton4,'Value',1);
set(handles.radiobutton5,'Value',1);
set(handles.radiobutton6,'Value',1);
end% Hint: get(hObject,'Value') returns toggle state of radiobutton7% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)close;
main;% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject handle to pushbutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)
load shuju
switch states(2)
case 0
msgbox('请选择训练方式');
break;
case 1
s=get(handles.edit2,'string')
kd =str2num(s);
if kd==0
kd=zhsh(1,1);
ll=0;
else
is_ll=0;
for i=1:gp_length
if gp(1,i)==kd
is_ll=1; ll=i;
end
end
end
if is_ll==0
msgbox('起始日期错误,请重新输入,0为从第一个数据开始');
end
s=get(handles.edit3,'string');
d =str2num(s);
if d<5
msgbox('训练时间太短,请重新输入');
else
lll=ll+d;
ld=zhsh(1,lll);
end
break
case 2
s=get(handles.edit2,'string')
kd =str2num(s);
if kd==0
kd=zhsh(1,1);
ll=0;
else
is_ll=0;
for i=1:gp_length
if gp(1,i)==kd
ll=i;
is_ll=1;
end
end
end
if is_ll==0
msgbox('起始日期错误,请重新输入,0为从第一个数据开始');
end
s=get(handles.edit3,'string');
ld =str2num(s);
if ld==0
ld=zhsh(1,end);
lll=gp_length-ll;
else
for i=1:gp_length
if gp(1,i)==ld
lll=i;
end
end
end
if lll==0
msgbox('结束日期错误,请重新输入,0为从第一个数据开始');
end
end
d=lll-ll;
i=1;
while ad_gp(1)~='.'
ad_gp1(i)=ad_gp(1);
ad_gp(1)=[];
i=i+1;
end
ad_gp1(i)='.'; ad_gp1(i+1)='t'; ad_gp1(i+2)='x'; ad_gp1(i+3)='t';
fid=fopen(ad_gp1,'W');%——————————计算OBV以及OBV改变量
obv_zhsh=obv(zhsh(2,:),zhsh(3,:),1);
obv_gp=obv(gp(2,:),gp(3,:),val);
obv1_zhsh=obv1(zhsh(2,:),zhsh(3,:),1);
obv1_gp=obv1(gp(2,:),gp(3,:),val);
%---------------------------计算变化量b_x和收益率shy_x
shy_zhsh(1)=0; shy_gp(1)=0; b_zhsh(1)=0; b_gp(1)=0;
for i=2:gp_length
shy_zhsh(i)=(zhsh(2,i)-zhsh(2,i-1))/zhsh(2,i-1);
shy_gp(i)=(gp(2,i)-gp(2,i-1))/gp(2,i-1);
b_zhsh(i)=zhsh(2,i)-zhsh(2,i-1);
b_gp(i)=gp(2,i)-gp(2,i-1);
end
%--------------------------计算对数股价d_x和对数收益率db_x
d_zhsh=log(zhsh(2,:)); d_gp=log(gp(2,:));
db_zhsh(1)=0; db_gp(1)=0;
for i=2:gp_length
db_zhsh(i)=d_zhsh(i)-d_zhsh(i-1);
db_gp(i)=d_gp(i)-d_gp(i-1);
end
%--------------建立各种模型
alpha=0.05;
if gp_length<ll+d
d=gp_length-ll;
end
%--------------------计算股票和指数的相关系数
cor=corrcoef(zhsh(2,:),gp(2,:));
ti=['股票和指数的相关系数为:',num2str(cor(1,2))];
disp(ti);
fprintf(fid,'%s\r\n',ti);
M_B=get(handles.radiobutton4,'Value');
C_GP1=get(handles.radiobutton1,'Value');
if M_B&C_GP1
%--1.股票指数模型1:P(t)-P(t-1)=a1(H(t)-H(t-1))
[B_GP1,BINT_GP1,R_GP1,RINT_GP1,STATS_GP1]=regress(b_gp(ll+1:ll+d)',b_zhsh(ll+1:ll+d)',alpha);
nih=B_GP1'*b_zhsh;
nihez_GP1(1)=gp(2,1);
for i=2:gp_length
nihez_GP1(i)=gp(2,i-1)+nih(i);
end
R_GP1=nihez_GP1-gp(2,:);
%-------计算D。W值
S=0; S1=R_GP1(1)^2;
for i=2:gp_length
S=S+R_GP1(i)*R_GP1(i-1);
S1=S1+R_GP1(i)^2;
end
STATS_GP1(4)=2*(1-S/S1);
%-------计算对数似然值
L=-(gp_length)*(1+log(2*pi)+log(R_GP1*R_GP1'/gp_length))/2;
STATS_GP1(5)=L;
%-------计算AIC值
STATS_GP1(6)=2*(-L+3)/gp_length;
%-------计算SC值
STATS_GP1(7)=2*(-L+3*log(gp_length))/gp_length;
%------------计算相对误差
xr_GP1=100*R_GP1'./gp(2,:)';
xr_min=min(xr_GP1);%最小值
xr_max=max(xr_GP1);%最大值
STATS_GP1(8)=xr_min;
STATS_GP1(9)=xr_max;
nu=mean(gp(2,:));
STATS_GP1(10)=dot(gp(2,:)-nihez_GP1,gp(2,:)-nihez_GP1)/dot(gp(2,:)-nu,gp(2,:)-nu);
figure
txt=['P(t)-P(t-1)=',num2str(B_GP1(1)),'*(H(t)-H(t-1))'];
txt1=['训练样本开始时间:',num2str(zhsh(1,ll+1)),';结束时间:',num2str(zhsh(1,d+ll)),'。共',num2str(d),'天数据'];
txt2=['样本决定系数(R^2):',num2str(STATS_GP1(1)),';AIC值:',num2str(STATS_GP1(4)),';股票和指数相关系数:',num2str(cor(1,2))];
disp('只考虑指数的差分模型为:'); disp(txt);
% fprintf(fid,'%s\n','最优模型为:',txt);
plot(gp(2,:),'-+');
hold on
plot(nihez_GP1,'r.');
legend('股价真实值','拟合股价');
xlabel(txt);
title('只考虑指数的差分模型')
g_max=max(ylim);
g_min=min(ylim);
text(10,g_min+(g_max-g_min)*0.975,txt1);
text(10,g_min+(g_max-g_min)*0.925,txt2);
hold on
plot(ll+d,gp(2,ll+d),'ko','Markersize',20);
plot(ll,gp(2,ll),'ko','Markersize',20);
figure
plot(xr_GP1);
legend('相对误差','相对误差=100*(拟合值-真实值)/真实值');
xlabel(txt);
title('只考虑指数的差分模型')
hold on
plot(ll+d,xr_GP1(ll+d),'k.','Markersize',20)
plot(ll,xr_GP1(ll),'k.','Markersize',20)
figure
normplot(R_GP1);
legend('误差对于正态分布的Q-Q图');
xlabel(txt);
title('只考虑指数的差分模型')
end
M_B=get(handles.radiobutton4,'Value');
C_GP2=get(handles.radiobutton2,'Value');
if M_B&C_GP2
%--2.股票指数模型1:P(t)-P(t-1)=a1(H(t)-H(t-1))+a2(Vp(t)*(1-exp(P(t-1)-P(t)))/(1+exp(P(t-1)-P(t)))
[B_GP2,BINT_GP2,R_GP2,RINT_GP2,STATS_GP2]=regress(b_gp(ll+1:ll+d)',[b_zhsh(ll+1:ll+d)',obv1_gp(ll+1:ll+d)'],alpha);
nih=B_GP2'*[b_zhsh',obv1_gp']';
nihez_GP2(1)=gp(2,1);
for i=2:gp_length
nihez_GP2(i)=gp(2,i-1)+nih(i);
end
R_GP2=nihez_GP2-gp(2,:);
%-------计算D。W值
S=0; S1=R_GP2(1)^2;
for i=2:gp_length
S=S+R_GP2(i)*R_GP2(i-1);
S1=S1+R_GP2(i)^2;
end
STATS_GP2(4)=2*(1-S/S1);
%-------计算对数似然值
L=-(gp_length)*(1+log(2*pi)+log(R_GP2*R_GP2'/gp_length))/2;
STATS_GP2(5)=L;
%-------计算AIC值
STATS_GP2(6)=2*(-L+3)/gp_length;
%-------计算SC值
STATS_GP2(7)=2*(-L+3*log(gp_length))/gp_length;
%------------计算相对误差
xr_GP2=100*R_GP2'./gp(2,:)';
xr_min=min(xr_GP2);%最小值
xr_max=max(xr_GP2);%最大值
STATS_GP2(8)=xr_min;
STATS_GP2(9)=xr_max;
nu=mean(gp(2,:));
STATS_GP2(10)=dot(gp(2,:)-nihez_GP2,gp(2,:)-nihez_GP2)/dot(gp(2,:)-nu,gp(2,:)-nu);
figure
txt=['P(t)-P(t-1)=',num2str(B_GP2(1)),'*(H(t)-H(t-1))+',num2str(B_GP2(2)),'*(Vp(t)*hp(t))'];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -