具体程序代码如下,主要问题就是不管我怎么调整参数,最终的预测结果都没有太大的变化,还请了解这两大算法的帮忙看看问题出在哪里了,十分感谢!
tic;
close all;
clear;
clc;
format compact;
load M2.mat %载入数据
% 提取数据
N=length(M2)-36;
train_tsx=zeros(N,36);
for i=1:N
train_tsx(i,:)=M2(i:i+35);
Y(i)=M2(i+36); %36个月代表货币周期,从历史数据来看,大致与短经济周期一样(3-4年)
end
train_ts=Y';
%数据预处理,将原始数据进行归一化
ts = train_ts';
tsx = train_tsx';
% mapminmax为matlab自带的映射函数
% 对ts进行归一化
[TS,TSps] = mapminmax(ts,0,1);
% 对TS进行转置,以符合libsvm工具箱的数据格式要求
TS = TS';
% mapminmax为matlab自带的映射函数
% 对tsx进行归一化
[TSX,TSXps] = mapminmax(tsx,0,1);
% 对TSX进行转置,以符合libsvm工具箱的数据格式要求
TSX = TSX';
% PSO寻找最佳的SVM参数c&g
% 参数初始化
%粒子群算法中的两个参数
c1 = 0.5; % c1 belongs to [0,2]
c2 = 0.5; % c2 belongs to [0,2]
maxgen=200; % 进化次数
sizepop=50; % 种群规模
popcmax=10^(2);
popcmin=10^(-1);
popgmax=10^(3);
popgmin=10^(-2);
k = 0.5; % k belongs to [0.1,1.0];
Vcmax = 500;
Vcmin = 1 ;
Vgmax = 10;
Vgmin = 0.1 ;
% SVM参数初始化
v = 5;
% 产生初始粒子和速度
for i=1:sizepop % 随机产生种群
pop(i,1) = (popcmax-popcmin)*rand+popcmin; % 初始种群
pop(i,2) = (popgmax-popgmin)*rand+popgmin;
V(i,1)=Vcmax*rands(1); % 初始化速度
V(i,2)=Vgmax*rands(1);
% 计算初始适应度
cmd = ['-v ',num2str(v),' -c ',num2str( pop(i,1) ),' -g ',num2str( pop(i,2) ),' -s 3 -t 2 -p 0.01 '];
fitness(i) = svmtrain(TS, TSX, cmd);
fitness(i) = -fitness(i);
end
% 找极值和极值点
[global_fitness, bestindex]=min(fitness); % 全局极值
local_fitness=fitness; % 个体极值初始化
global_x=pop(bestindex,:); % 全局极值点
local_x=pop; % 个体极值点初始化
% 迭代寻优
for i=1:maxgen
for j=1:sizepop %速度更新
wV = 1; % wV best belongs to [0.8,1.2]
V(j,:) = wV*V(j,:) + c1*rand*(local_x(j,:) - pop(j,:)) + c2*rand*(global_x - pop(j,:));
if V(j,1) > Vcmax
V(j,1) = Vcmax;
end
if V(j,1) < Vcmin
V(j,1) = Vcmin;
end
if V(j,2) > Vgmax
V(j,2) = Vgmax;
end
if V(j,2) < Vgmin
V(j,2) = Vgmin;
end
%种群更新
wP = 0.5;
pop(j,:)=pop(j,:)+wP*V(j,:);
if pop(j,1) > popcmax
pop(j,1) = popcmax;
end
if pop(j,1) < popcmin
pop(j,1) = popcmin;
end
if pop(j,2) > popgmax
pop(j,2) = popgmax;
end
if pop(j,2) < popgmin
pop(j,2) = popgmin;
end
% 自适应粒子变异
if rand>0.5
k=ceil(2*rand);
if k == 1
pop(j,k) = (20-1)*rand+1;
end
if k == 2
pop(j,k) = (popgmax-popgmin)*rand+popgmin;
end
end
%适应度值
cmd = ['-v ',num2str(v),' -c ',num2str( pop(j,1) ),' -g ',num2str( pop(j,2) ),' -s 3 -t 2 -p 0.01 '];
fitness(j) = svmtrain(TS, TSX, cmd);
fitness(j) = -fitness(j);
end
%个体最优更新
if fitness(j) < local_fitness(j)
local_x(j,:) = pop(j,:);
local_fitness(j) = fitness(j);
end
%群体最优更新
if fitness(j) < global_fitness
global_x = pop(j,:);
global_fitness = fitness(j);
end
fit_gen(i)=global_fitness;
end
toc
bestc = global_x(1);
bestg = global_x(2);
bestmse = -fit_gen(maxgen);
% 利用回归预测分析最佳的参数进行SVM训练
cmd = ['-c ',num2str( bestc ),' -g ',num2str( bestg ),' -s 3 -t 2 -p 0.01 '];
model = svmtrain(TS,TSX,cmd);
% 进行SVM预测
test_tsx1 = TS(163:198);
test_tsx1=test_tsx1';
[predict1,mse,detesvalue] = svmpredict(8.29,test_tsx1,model);
test_ts1 = mapminmax('reverse',predict1',TSps);