Matlab tips 2

本文介绍了Matlab中的一些实用技巧,包括使用mldivide解决线性方程、手动实现OLS回归、滚动求和、绘制线条、矩阵数据输出、自定义绘图颜色以及并行计算等。同时,还展示了如何计算月度夏普比率和创建3D图表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

%% plot multi curve shared with same date list in X axis
ccylist={'USDEUR Curncy', 'USDJPY Curncy', 'USDCAD Curncy', 'USDCHF Curncy', 'USDGBP Curncy'};
datelist=<datenum list>.
clmat=<contain all close of the above currencies>.    
plot(datelist, clmat); % row count of datelist and clmat are equal
legend(ccylist);
datetick('x','yyyy-mm-dd','keepticks');


%% logical vector "and"/"or"
x=[1 0 1];
y=~x;
x&y;
x|y;


%% Get strings begin with "USD" from a cell string list
ccylist(strncmpi(ccylist,'USD',3));


%% convert datenum to readable in matlab
[y,m,d]=datevec(data(:,1));
dlist=(y*100+m)*100+d;
newdata=[dlist, data(:,2)];


%% located by datenum in matrix
data(data(:,1)==datenum(2011,5,3),:);


%% function pointer
f1=@(x)x/2;
f1([2 4 6 8])


%% to store un-equal size array in a matrix, use cell array
range = {1:300,1:300,55};


%% string to number table usage
weight={
'USD',0.3
'EUR',0.2
'JPY',0.2};

item=weight(strncmpi(weight(:,1),'EUR',1)==1, :);
if(~isempty(item))
    item{2}
end;

%% output matrix data with header names
data=[180 160];
header={'height', 'wieght'};
rpt(1,:)=header;
rpt(2,:)=num2cell(data);
rpt




%% mldivide(\), solve linear equations
A =

     8     1     6
     3     5     7
     4     9     2
y = [1;2;3];

to solve this: x=A\b

x =

    0.0500
    0.3000
    0.0500
    
    
to VERIFY:
A*x = y;


%% mldivide(\) require same row counts, but mrdivide require same column counts.
so to solve equations, use (\)

%%% manual OLS
function cointres=isCoint(X,Y)
% first perform an OLS and get residuals
beta = [ones(length(X),1),X]\Y;
res = Y - [ones(length(X),1),X]*beta;
% do a simple unit root test on the residuals
[H,PValue,TestStat] = dfARTest(res);
if H
    cointres=TestStat;
else
    cointres=0;
end


%%%% rolling sum
r=filter(ones(1,N),1,x);


%% idx1 store the logical index of entry, calc exit pnl with hold days=3
%calc exit logical index
N=3;
ex=logical([zeros(N,1);idx1(1:size(idx1,1)-N)]);
expx=data(ex);
enpx=data(idx1); % rows(enpx) might > expx
enpx=enpx(1:size(expx,1),:);
d2=[enpx,expx];


%% draw line on (2,4) and (3,5)
line([2 3],[4,5]); xlim([0 10]); ylim([0 10]);


%% output matrix data with header names, more easier way
data=[180 160];
[{'height', 'weight'}
num2cell(data)]


%% assign cell string in a column
strs(:,1)={'Msg'};


%% auto set colors in plot(): hold all
figure;
hold all;
for k=1:size1
    plot(..);
end;

%% auto set colors in plot(): generate HSV
hc=hsv(size1);
figure;
hold on;
for i=1:size1
    plot(...'color',hc(i,:));
end


%% parfor example
matlabpool close force local;
matlabpool open 7;
parfor k=1:N
...
end;
matlabpool close force local;


%% calc monthly sharpe
v1=datevec(dayeqty(:,1));
idx=(v1(:,3)==1);
idx(1)=1;
idx(end)=1;
mthly=dayeqty(idx,2);

mthret=mthly(2:end)./mthly(1:end-1)-1;  
annvol_mth=std(mthret)*sqrt(12.0);
nyear=length(mthret)/12;
endret_mth = mthly(end)/mthly(1);
annrtn_mth = endret_mth ^ (1/nyear) - 1;
sh_mth=annrtn_mth/annvol_mth;   


%% mesh
plot a 2 dimension matrix into 3D chart,
x,y is the row and column of matrix, z is the value




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值