s_logt.m
来自「经济学专业代码」· M 代码 · 共 59 行
M
59 行
% this script simulates a bivariate log-t distribution
% it shows its pdf as proxied by the 3-D histogram
% it shows the distribution of a generic linear combination (portfolio) of the two variables
% see "Risk and Asset Allocation"-Springer (2005), by A. Meucci
clc; clear; close all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% input parameters
Mu=[0 0]';
s=[0.3 0.2]';
r=-.99;
Nu=7;
LinCombination=[1 -2]';
NumSimulations=100000;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Sigma=[s(1)^2 r*s(1)*s(2)
r*s(1)*s(2) s(2)^2];
Ones=ones(NumSimulations,1);
% generate sample by rescaling the built-in generator
Y = Ones*Mu' + (Ones*s').*mvtrnd([1 r;r 1],Nu,NumSimulations);
X = exp(Y);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plots
figure
% marginals
NumBins=round(10*log(NumSimulations));
subplot('Position',[.05 .3 .2 .6])
[n,D]=hist(X(:,2),NumBins);
barh(D,n,1);
[y_lim]=get(gca,'ylim')
set(gca,'xtick',[])
grid on
subplot('Position',[.3 .05 .6 .2])
[n,D]=hist(X(:,1),NumBins);
bar(D,n,1);
[x_lim]=get(gca,'xlim')
set(gca,'ytick',[])
grid on
% scatter plot
subplot('Position',[.3 .3 .6 .6])
h=plot(X(:,1),X(:,2),'.');
set(gca,'xlim',x_lim,'ylim',y_lim)
grid on
% histogram (~rescaled pdf)
NumBins3d=round(sqrt(NumSimulations)/5);
figure
hist3(X,[NumBins3d NumBins3d]);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?