⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blackscholes.asv

📁 介绍了kf,ekf,ukf ,pf ,upf 的程序代码
💻 ASV
📖 第 1 页 / 共 2 页
字号:
% PURPOSE : Demonstrate the differences between the following% filters on an options pricing problem.%           %           1) Extended Kalman Filter  (EKF)%           2) Unscented Kalman Filter (UKF)%           3) Particle Filter         (PF)%           4) PF with EKF proposal    (PFEKF)%           5) PF with UKF proposal    (PFUKF)% For more details refer to:% AUTHORS  : Nando de Freitas      (jfgf@cs.berkeley.edu)%            Rudolph van der Merwe (rvdmerwe@ece.ogi.edu)%            + We re-used a bit of code by Mahesan Niranjan.             % DATE     : 17 August 2000clear all;echo off;path('./ukf',path);path('./data',path);% INITIALISATION AND PARAMETERS:% ==============================doPlot = 0;                 % 1 plot online. 0 = only plot at the end.g1 = 3;                     % Paramater of Gamma transition prior.g2 = 2;                     % Parameter of Gamman transition prior.                            % Thus mean = 3/2 and var = 3/4.T = 204;                    % Number of time steps.R = diag([1e-5 1e-5]);      % EKF's measurement noise variance. Q = diag([1e-7 1e-5]);      % EKF's process noise variance.P01 = 0.1;                  % EKF's initial variance of the                            % interest rate.P02 = 0.1;                  % EKF's initial variance of the volatility.N = 10;                     % Number of particles.optionNumber = 1;           % There are 5 pairs of options.resamplingScheme = 1;       % The possible choices are                            % systematic sampling (2),                            % residual (1)                            % and multinomial (3).                             % They're all O(N) algorithms. P01_ukf = 0.1P02_ukf = 0.1Q_ukf = Q;R_ukf = R;initr=.01initsig=.15Q_pfukf = 10*1e-5*eye(2);R_pfukf = 1e-6*eye(2);			    alpha = 1;                     % UKF : point scaling parameterbeta  = 2;                     % UKF : scaling parameter for higher order terms of Taylor series expansion kappa = 1;                     % UKF : sigma point selection scaling parameter (best to leave this = 0)no_of_experiments = 1;         % Number of times the experiment is                               % repeated (for statistical purposes).% DATA STRUCTURES FOR RESULTS% ===========================errorcTrivial = zeros(no_of_experiments,1);errorpTrivial = errorcTrivial;errorcPF      = errorcTrivial;errorpPF      = errorcTrivial;errorcPFUKF   = errorcTrivial;errorpPFUKF   = errorcTrivial;% LOAD THE DATA:% =============fprintf('\n')fprintf('Loading the data')fprintf('\n')load c2925.prn;         load p2925.prn;load c3025.prn;         load p3025.prn;X=[2925; 3025];[d1,i1]=sort(c2925(:,1));  Y1=c2925(i1,:);      Z1=p2925(i1,:);[d2,i2]=sort(c3025(:,1));  Y2=c3025(i2,:);      Z2=p3025(i2,:);d=Y1(:,1); % d - date to maturity.St(1,:) = Y1(:,3)';   C(1,:) = Y1(:,2)';  P(1,:) = Z1(:,2)';St(2,:) = Y2(:,3)';   C(2,:) = Y2(:,2)';  P(2,:) = Z2(:,2)';% St - Stock price.% C - Call option price.% P - Put Option price.% X - Strike price.% Normalise with respect to the strike price:for i=1:2   Cox(i,:) = C(i,:) / X(i);   Sox(i,:) = St(i,:) / X(i);   Pox(i,:) = P(i,:) / X(i);endCpred=zeros(T,2);Ppred=zeros(T,2);% PLOT THE LOADED DATA:% ====================figure(1)clf;plot(Cox');ylabel('Call option prices','fontsize',15);xlabel('Time to maturity','fontsize',15);fprintf('\n')fprintf('Press a key to continue')  pause;fprintf('\n')fprintf('\n')fprintf('Training has started')fprintf('\n')% SPECIFY THE INPUTS AND OUTPUTS% ==============================ii=optionNumber;   % Only one call price. Change 1 to 3, etc. for other prices.X = X(ii,1);St = Sox(ii,1:T);C = Cox(ii,1:T);P = Pox(ii,1:T);counter=1:1:T;tm = (224*ones(size(counter))-counter)/260;u = [St' tm']';y = [C' P']'; % Call and put prices.% MAIN LOOP% =========for expr=1:no_of_experiments,  rand('state',sum(100*clock));   % Shuffle the pack!  randn('state',sum(100*clock));   % Shuffle the pack!  %%%%%%%%%%%%%%%  PERFORM SEQUENTIAL MONTE CARLO  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  ==============================  %%%%%%%%%%%%%%%%%%%%%% INITIALISATION:% ==============xparticle_pf = ones(2,T,N);      % These are the particles for the estimate                                 % of x. Note that there's no need to store                                 % them for all t. We're only doing this to                                 % show you all the nice plots at the end.xparticlePred_pf = ones(2,T,N);    % One-step-ahead predicted values of the states.yPred_pf = ones(2,T,N);            % One-step-ahead predicted values of y.w = ones(T,N);                   % Importance weights.% Initialisation:for i=1:N,  xparticle_pf(1,1,i) = initr; % sqrt(initr)*randn(1,1);  xparticle_pf(2,1,i) = initsig; %sqrt(initsig)*randn(1,1);end;disp(' '); tic;                             % Initialize timer for benchmarkingfor t=2:T,      fprintf('PF :  t = %i / %i  \r',t,T);  fprintf('\n')    % PREDICTION STEP:  % ================   % We use the transition prior as proposal.  for i=1:N,    xparticlePred_pf(:,t,i) = feval('bsffun',xparticle_pf(:,t-1,i),t) + sqrtm(Q)*randn(2,1);      end;  % EVALUATE IMPORTANCE WEIGHTS:  % ============================  % For our choice of proposal, the importance weights are give by:    for i=1:N,    yPred_pf(:,t,i) = feval('bshfun',xparticlePred_pf(:,t,i),u(:,t),t);            lik = exp(-0.5*(y(:,t)-yPred_pf(:,t,i))'*inv(R)*(y(:,t)-yPred_pf(:,t,i)) ) + 1e-99; % Deal with ill-conditioning.    w(t,i) = lik;      end;    w(t,:) = w(t,:)./sum(w(t,:));                % Normalise the weights.    % SELECTION STEP:  % ===============  % Here, we give you the choice to try three different types of  % resampling algorithms. Note that the code for these algorithms  % applies to any problem!  if resamplingScheme == 1    outIndex = residualR(1:N,w(t,:)');        % Residual resampling.  elseif resamplingScheme == 2    outIndex = systematicR(1:N,w(t,:)');      % Systematic resampling.  else      outIndex = multinomialR(1:N,w(t,:)');     % Multinomial resampling.    end;  xparticle_pf(:,t,:) = xparticlePred_pf(:,t,outIndex); % Keep particles with                                                    % resampled indices.end;   % End of t loop.time_pf = toc;    % How long did this take?% Compute posterior mean predictions:yPFmeanC=zeros(1,T);yPFmeanP=zeros(1,T);for t=1:T,  yPFmeanC(t) = mean(yPred_pf(1,t,:));  yPFmeanP(t) = mean(yPred_pf(2,t,:));  end;  figure(4)clf;domain = zeros(T,1);range = zeros(T,1);thex=[0:1e-3:20e-3];hold onylabel('Time (t)','fontsize',15)xlabel('r_t','fontsize',15)zlabel('p(r_t|S_t,t_m,C_t,P_t)','fontsize',15)for t=11:20:200,  [range,domain]=hist(xparticle_pf(1,t,:),thex);  waterfall(domain,t,range/sum(range));end;view(-30,80);rotate3d on;a=get(gca);set(gca,'ygrid','off');figure(5)clf;domain = zeros(T,1);range = zeros(T,1);thex=[0.1:1e-2:0.25];hold onylabel('Time (t)','fontsize',15)xlabel('r_t','fontsize',15)zlabel('p(\sigma_t|S_t,t_m,C_t,P_t)','fontsize',15)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -