📄 qh.m
字号:
function varargout = qh(varargin)
% QH M-file for qh.fig
% QH, by itself, creates a new QH or raises the existing
% singleton*.
%
% H = QH returns the handle to a new QH or the handle to
% the existing singleton*.
%
% QH('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in QH.M with the given input arguments.
%
% QH('Property','Value',...) creates a new QH or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before qh_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to qh_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 qh
% Last Modified by GUIDE v2.5 24-Nov-2004 16:12:52
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @qh_OpeningFcn, ...
'gui_OutputFcn', @qh_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 qh is made visible.
function qh_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 qh (see VARARGIN)
% Choose default command line output for qh
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes qh wait for user response (see UIRESUME)
% uiwait(handles.figure1);
states=zeros(1,10);
d=250;
ll=0;
lll=0;
save shuj;
% --- Outputs from this function are returned to the command line.
function varargout = qh_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(hObject,'Value');
if M_A==1
set(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 shuj
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,:),gp(4,:));
obv1_zhsh=Q_obv1(zhsh(2,:),zhsh(3,:),zhsh(4,:));
obv1_gp=Q_obv1(gp(2,:),gp(3,:),gp(4,:));
%---------------------------计算变化量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;
ll=get(handles.edit2,'string');
ll=str2num(ll);
if states(2)~=0
ll=1;
else
msgbox('请选择训练方式');
end
if gp_length<ll+d
d=gp_length-ll;
msgbox('训练时间超出实际数据范围');
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:ll+d)',b_zhsh(ll: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)),';结束时间:',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(20,g_min+(g_max-g_min)*0.975,txt1);
text(20,g_min+(g_max-g_min)*0.95,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_GP3=get(handles.radiobutton3,'Value');
if M_B&C_GP3
%--3.股票指数模型2:P(t)-P(t-1)=a1(H(t)-H(t-1))+a2(Vp(t)*(1-exp(P(t-1)-P(t)))/(1+exp(H(t-1)-H(t)))+a3(Vh(t)*(1-exp(H(t-1)-H(t)))/(1+exp(H(t-1)-H(t)))
[B_GP3,BINT_GP3,R_GP3,RINT_GP3,STATS_GP3]=regress(b_gp(ll:ll+d)',[b_zhsh(ll:ll+d)',obv1_gp(ll:ll+d)',obv1_zhsh(ll:ll+d)'],alpha);
nih=B_GP3'*[b_zhsh',obv1_gp',obv1_zhsh']';
nihez_GP3(1)=gp(2,1);
for i=2:gp_length
nihez_GP3(i)=gp(2,i-1)+nih(i);
end
R_GP3=nihez_GP3-gp(2,:);
%-------计算D。W值
S=0; S1=R_GP3(1)^2;
for i=2:gp_length
S=S+R_GP3(i)*R_GP3(i-1);
S1=S1+R_GP3(i)^2;
end
STATS_GP3(4)=2*(1-S/S1);
%-------计算对数似然值
L=-(gp_length)*(1+log(2*pi)+log(R_GP3*R_GP3'/gp_length))/2;
STATS_GP3(5)=L;
%-------计算AIC值
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -