写论文第五天:参数扫描法
2016-04-28 00:11
615 查看
1.parameterSweep函数
[respmax,varmax,resp,var] = parameterSweep(fun,range)
输出的4个变量依次是最优结果(比如最大夏普比例),最优结果对应的参数组合,所有扫描结果以及所有的参数组合。
输入两个变量,第一个是一个指向被扫描函数的function handle,第二个是一个cell类型,可以包含多个参数的范围。
2.rsiFun函数
3.size函数
size():获取矩阵的行数和列数
(1)s=size(A),
当只有一个输出参数时,返回一个行向量,该行向量的第一个元素时矩阵的行数,第二个元素是矩阵的列数。
(2)[r,c]=size(A),
当有两个输出参数时,size函数将矩阵的行数返回到第一个输出变量r,将矩阵的列数返回到第二个输出变量c。
(3)size(A,n)如果在size函数的输入参数中再添加一项n,并用1或2为n赋值,则 size将返回矩阵的行数或列数。其中r=size(A,1)该语句返回的时矩阵A的行数, c=size(A,2) 该语句返回的时矩阵A的列数。
另外,length()=max(size()).
4. parfor
【使用和关闭】
使用matlabpool 命令启动多个workers,使用parfor指令。自己的电脑上是4核4进程,开启4个workers:
matlabpool 4
关闭时用 matlabpool close
【语法】
parfor的语法和普通的for语法没有区别:
for i = 1:N
f(i);
end
parfor i = 1:N
f(i);
end
【并行】
for 语句是按照i的序列顺序执行的,而parfor是由多个worker同时执行i为不同值的结果
for i = 1:12 fprintf(' %d',i); end 的输出为:
1 2 3 4 5 6 7 8 9 10 11 12
parfor i = 1:12 fprintf(' %d',i); end的输出为:
2 1 10
4 3 11
6 5 12
8 7 9
5.rsi指标
参考:http://jingyan.baidu.com/article/ca00d56c54ff84e99eebcff4.html
RSI指标的计算方法
N日RSI=[A÷(A+B)]×100%
公式中,A——N日内收盘涨幅之和
B——N日内收盘跌幅之和(取正值)
N日RSI=100-100/(1+RS)
从计算公式上看,我们可以看到,RSI的计算非常简单,实际理解为:在某一阶段价格上涨所产生的波动占整个波动的半分比。
6.
[respmax,varmax,resp,var] = parameterSweep(fun,range)
输出的4个变量依次是最优结果(比如最大夏普比例),最优结果对应的参数组合,所有扫描结果以及所有的参数组合。
输入两个变量,第一个是一个指向被扫描函数的function handle,第二个是一个cell类型,可以包含多个参数的范围。
>> type parameterSweep function [respmax,varmax,resp,var] = parameterSweep(fun,range) %PARAMETERSWEEP performs a parameters sweep for a given function % RESPMAX = PARAMETERSWEEP(FUN,RANGE) takes as input a function handle in % FUN and the range of allowable values for inputs in RANGE. % % FUN is a function handle that accepts one input and returns one output, % F = FUN(X), where X is an array of size N x M. N is the number of % observations (rows) and M is the number of variables (columns). F is % the response of size N x 1, a column vector; % % RANGE is a cell array of length M that contains a vector of ranges for % each variable. % % [RESPMAX,VARMAX,RESP,VAR] = PARAMETERSWEEP(FUN,RANGE) returns the % maximum response value in RESPMAX, the location of the maximum value in % VARMAX, an N-D array of the response values in RESP, and and N-D array % where the size of the N-D array depends upon the values in RANGE. % NDGRID is used to generate the arrays. % % Examples: % % Example 1: peaks function % range = {-3:0.1:3, -3:0.2:3}; % range of x and y variables % fun = @(x) deal( peaks(x(:,1),x(:,2)) ); % peaks as a function handle % [respmax,varmax,resp,var] = parameterSweep(fun,range); % surf(var{1},var{2},resp) % hold on, grid on % plot3(varmax(1),varmax(2),respmax,... % 'MarkerFaceColor','k', 'MarkerEdgeColor','k',... % 'Marker','pentagram', 'LineStyle','none',... % 'MarkerSize',20, 'Color','k'); % hold off % xlabel('x'),ylabel('y') % legend('Surface','Max Value','Location','NorthOutside') % % See also ndgrid %% % Copyright 2010, The MathWorks, Inc. % All rights reserved. %% Check inputs if nargin == 0 example(1) else %% Generate expression for ndgrid N = length(range); if N == 1 var = range; else in = ''; out = ''; for i = 1:N in = [in,'range{',num2str(i),'},']; out = [out,'var{',num2str(i),'},']; end in(end) = []; % remove last commas out(end) = []; %% Evaluate ndgrid eval( ['[',out,'] = ndgrid(',in,');'] ); end %% Perform parameter sweep sz = size(var{1}); for i = 1:N var{i} = var{i}(:); end resp = fun(cell2mat(var)); %% Find maximum value and location [respmax,idx] = max(resp); for i = 1:N varmax(i) = var{i}(idx); end %% Reshape output only if requested if nargout > 2 resp = reshape(resp,sz); for i = 1:N var{i} = reshape(var{i},sz); end end %if end %if %% Examples function example(ex) for e = 1:length(ex) for e = 1:length(ex) switch ex(e) case 1 figure(1), clf range = {-3:0.1:3, -3:0.2:3}; % range of x and y variables fun = @(x) deal( peaks(x(:,1),x(:,2)) ); % peaks as a function handle [respmax,varmax,resp,var] = parameterSweep(fun,range); surf(var{1},var{2},resp) hold on, grid on plot3(varmax(1),varmax(2),respmax,... 'MarkerFaceColor','k', 'MarkerEdgeColor','k',... 'Marker','pentagram', 'LineStyle','none',... 'MarkerSize',20, 'Color','k'); hold off xlabel('x'),ylabel('y') legend('Surface','Max Value','Location','NorthOutside') end %switch end %for end %for
2.rsiFun函数
>> type rsiFun function sh = rsifun(x,data,scaling,cost) % define rsi to accept vectorized inputs and return only sharpe ratio %% % Copyright 2010, The MathWorks, Inc. % All rights reserved. row = size(x,1); sh = zeros(row,1); parfor i = 1:row [~,~,sh(i)] = rsi(data,[x(i,1),x(i,2)],x(i,3),scaling,cost); end
3.size函数
size():获取矩阵的行数和列数
(1)s=size(A),
当只有一个输出参数时,返回一个行向量,该行向量的第一个元素时矩阵的行数,第二个元素是矩阵的列数。
(2)[r,c]=size(A),
当有两个输出参数时,size函数将矩阵的行数返回到第一个输出变量r,将矩阵的列数返回到第二个输出变量c。
(3)size(A,n)如果在size函数的输入参数中再添加一项n,并用1或2为n赋值,则 size将返回矩阵的行数或列数。其中r=size(A,1)该语句返回的时矩阵A的行数, c=size(A,2) 该语句返回的时矩阵A的列数。
另外,length()=max(size()).
4. parfor
【使用和关闭】
使用matlabpool 命令启动多个workers,使用parfor指令。自己的电脑上是4核4进程,开启4个workers:
matlabpool 4
关闭时用 matlabpool close
【语法】
parfor的语法和普通的for语法没有区别:
for i = 1:N
f(i);
end
parfor i = 1:N
f(i);
end
【并行】
for 语句是按照i的序列顺序执行的,而parfor是由多个worker同时执行i为不同值的结果
for i = 1:12 fprintf(' %d',i); end 的输出为:
1 2 3 4 5 6 7 8 9 10 11 12
parfor i = 1:12 fprintf(' %d',i); end的输出为:
2 1 10
4 3 11
6 5 12
8 7 9
5.rsi指标
参考:http://jingyan.baidu.com/article/ca00d56c54ff84e99eebcff4.html
RSI指标的计算方法
N日RSI=[A÷(A+B)]×100%
公式中,A——N日内收盘涨幅之和
B——N日内收盘跌幅之和(取正值)
N日RSI=100-100/(1+RS)
从计算公式上看,我们可以看到,RSI的计算非常简单,实际理解为:在某一阶段价格上涨所产生的波动占整个波动的半分比。
type rsi function varargout = rsi(price,M,thresh,scaling,cost) % RSI %% % Copyright 2010, The MathWorks, Inc. % All rights reserved. if ~exist('scaling','var') scaling = 1; end if ~exist('M','var') M = 0; % no detrending else if numel(M) > 1 N = M(2); M = M(1); else N = M; M = 15*M; end end if ~exist('thresh','var') thresh = [30 70]; % default threshold else if numel(thresh) == 1 % scalar value thresh = [100-thresh, thresh]; else if thresh(1) > thresh(2) thresh= thresh(2:-1:1); end end end if ~exist('cost','var') cost = 0; % default cost end %% Detrend with a moving average if M == 0 ma = zeros(size(price)); else ma = movavg(price,M,M,'e'); end ri = rsindex(price - ma, N); %% Position signal s = zeros(size(price)); % Crossing the lower threshold indx = ri < thresh(1); indx = [false; indx(1:end-1) & ~indx(2:end)]; s(indx) = 1; % Crossing the upper threshold indx = ri > thresh(2); indx = [false; indx(1:end-1) & ~indx(2:end)]; s(indx) = -1; % Fill in zero values with prior position for i = 2:length(s) if s(i) == 0 s(i) = s(i-1); end end %% PNL Caclulation r = [0; s(1:end-1).*diff(price)-abs(diff(s))*cost/2]; sh = scaling*sharpe(r,0); %% Plot if requested if nargout == 0 ax(1) = subplot(3,1,1); plot([price,ma]), grid on legend('Price',['Moving Average ',num2str(M)]) title(['RSI Results, Sharpe Ratio = ',num2str(sh,3)]) ax(2) = subplot(3,1,2); plot([ri,thresh(1)*ones(size(ri)),thresh(2)*ones(size(ri))]) grid on legend(['RSI ',num2str(N)],'Lower Threshold','Upper Threshold') title('RSI') ax(3) = subplot(3,1,3); plot([s,cumsum(r)]), grid on legend('Position','Cumulative Return','Location','Best') title(['Final Return = ',num2str(sum(r),3),' (',num2str(sum(r)/price(1)*100,3),'%)']) linkaxes(ax,'x') else %% Return values for i = 1:nargout switch i case 1 varargout{1} = s; % signal case 2 varargout{2} = r; % return (pnl) case 3 varargout{3} = sh; % sharpe ratio case 4 varargout{4} = ri; % rsi signal case 5 varargout{5} = ma; % moving average case 6 varargout{6} = thresh; % threshold otherwise warning('RSI:OutputArg',... 'Too many output arguments requested, ignoring last ones'); end %switch end %for end %if %% Examples %% Supporting Functions % Faster implementation of rsindex found in Financial Toolbox function r=rsindex(x,N) L = length(x); dx = diff([0;x]); up=dx; down=abs(dx); % up and down moves I=dx<=0; up(I) = 0; down(~I)=0; % calculate exponential moving averages m1 = movavg(up,N,N,'e'); m2 = movavg(down,N,N,'e'); warning off r = 100*m1./(m1+m2); %r(isnan(r))=50; I2=~((up+down)>0); r(I2)=50;
6.
相关文章推荐
- 个人项目经历-----个人自学路程(1)
- 读书笔记 第三章 数据化运营中常见的数据分析项目类型
- iBatis与Hibernate区别?
- [NOIP2010] 普及组
- NOIP2010普及组题解
- 警告: No configuration found for the specified action: 'upload' in namespace: ''. Form action . . .
- POJ 3320 Jessica's Reading Problem(尺取法)
- Mysql(一)安装使用
- java操作ftp实现文件的上传下载(适用于图片文档服务器)
- Java ArrayList工作原理及实现
- 快速排序 一般分割
- Android 深入理解Android中的自定义属性
- 团队开发
- 写个js程序咖常写的游戏-贪吃蛇
- UEFI模式下安装Windows 10、Ubuntu 16.04 LTS双系统教程
- JSP学生信息管理系统
- Aspnetpager对GridView分页并顺利导出Excel
- ASP.NET项目开发中日期控件DatePicker如何使用
- ASP.NET使用My97DatePicker日期控件实例
- ASP.NET My97DatePicker日期控件实现OA日期记事功能