📄 spm_mar_gen.m.svn-base
字号:
function [v] = spm_mar_gen (w,A,C,n,ndisc)% Generate data from MAR model% FORMAT [v] = spm_mar_gen (w,A,C,n,ndisc)%% Generates n time steps of the MAR(p) process%% v(k,:)' = w' + A1*v(k-1,:)' +...+ Ap*v(k-p,:)' + eta(k,:)', %% where A=[A1 ... Ap] is the coefficient matrix, and w is a vector of% intercept terms that is included to allow for a nonzero mean of the% process. The vectors eta(k,:) are independent Gaussian noise% vectors with mean zero and covariance matrix C.%% This function is adapted from the ARFIT toolbox by Neumaier and% Schneider%___________________________________________________________________________% Copyright (C) 2007 Wellcome Department of Imaging Neuroscience% Will Penny % $Id$m = size(C,1); % dimension of state vectors p = size(A,2)/m; % order of processif (p ~= round(p)) error('Bad arguments.'); endif (length(w) ~= m | min(size(w)) ~= 1) error('Dimensions of arguments are mutually incompatible.')end w = w(:)'; % force w to be row vector% Discard the first ndisc time steps; if ndisc is not given as input% argument, use defaultif (nargin < 5) ndisc = 10^3; end% Compute Cholesky factor of covariance matrix CR = chol(C); % R is upper triangular% Get ndisc+n independent Gaussian pseudo-random vectors with % covariance matrix C=R'*Rrandvec = randn([ndisc+n,m])*R;% Add intercept vector to random vectorsrandvec = randvec + ones(ndisc+n,1)*w;% Get transpose of system matrix A (use transpose in simulation because % we want to obtain the states as row vectors)AT = A';% Take the p initial values of the simulation to equal the process mean, % which is calculated from the parameters A and wif any(w) % Process has nonzero mean mval = inv(B)*w' where % B = eye(m) - A1 -... - Ap; % Assemble B B = eye(m); for j=1:p B = B - A(:, (j-1)*m+1:j*m); end % Get mean value of process mval = w / B'; % The optimal forecast of the next state given the p previous % states is stored in the vector x. The vector x is initialized % with the process mean. x = ones(p,1)*mval;else % Process has zero mean x = zeros(p,m); end% Initialize state vectorsu = [x; zeros(ndisc+n,m)];% Simulate n+ndisc observations. In order to make use of Matlab's% vectorization capabilities, the cases p=1 and p>1 must be treated % separately.if p==1 for k=2:ndisc+n+1; x(1,:) = u(k-1,:)*AT; u(k,:) = x + randvec(k-1,:); endelse for k=p+1:ndisc+n+p; for j=1:p; x(j,:) = u(k-j,:)*AT((j-1)*m+1:j*m,:); end u(k,:) = sum(x)+randvec(k-p,:); endend% return only the last n simulated state vectorsv = u(ndisc+p+1:ndisc+n+p,:);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -