Serving the Quantitative Finance Community

 
User avatar
orangeman44
Topic Author
Posts: 0
Joined: February 7th, 2002, 10:13 pm

Matlab VBA Gurus

July 21st, 2006, 6:04 pm

William Sharpe has the following matlab code on his website. Has anyone translated it in VBA?function [C,sda,CC,e] = wcov(R,h);% [C,sda,CC,e] = wcov(R,h);% produces a weighted covariance matrix using a specified half-life% also produces standard deviations, correlations and expected values% variables:% R: matrix of s observations (states) on n variables% h: half-life (0 for equal weights)% C: n*n covariance matrix % sda: n*1 vector of standard deviations% CC: n*n matrix of correlation coefficients% e: n*1 vector of expected returns % % weight for observation t is 2^(t/h), scaled to sum to 1.0 % copyright 1995, William F. Sharpe% wfsharpe@leland.stanford.edu% this version Nov. 2, 1995 % if matrix has more rows than columns, transpose it if size(R,1) > size(R,2) R = R'; end; % get dimensions [n,s] = size(R); % set up weight vector if h == 0 x = zeros(1,s); else x = (1:s)/h; end; w = (2.^x); p = w/sum(w); % compute expected values e = R*p'; % compute matrix of deviations d = R - e*ones(1,s); % compute weighted covariances C = d*diag(p)*d'; % compute standard deviations sda = sqrt(diag(C)); % compute correlations (if sda = 0, corr = 0) z = sda*sda'; z = z + (z==0); CC = C./z;
 
sjoo
Posts: 0
Joined: March 24th, 2003, 1:54 am

Matlab VBA Gurus

August 2nd, 2006, 11:34 pm

Hello, orangeman44I made a vba code what you want.I compared the results of VBA and Octave(=matlab clone)and identified the same result.Here it is;
Last edited by sjoo on August 9th, 2006, 10:00 pm, edited 1 time in total.
 
User avatar
player
Posts: 0
Joined: August 5th, 2002, 10:00 am

Matlab VBA Gurus

August 3rd, 2006, 7:02 am

I'm writing an algorithm in matlab....I've been doing this step by step..Hence the algorithm is several parts on different scripts.Is there someway of connecting all the scripts together...ie calling teh alogrithm in script 2 from script 1???e.g it VBA you can have part of an algorithm in one module calling stuff in another module
 
User avatar
asd
Posts: 17
Joined: August 15th, 2002, 9:50 pm

Matlab VBA Gurus

August 3rd, 2006, 4:12 pm

"Is there someway of connecting all the scripts together...ie calling teh alogrithm in script 2 from script 1???"In Octave it is simple to connect the scripts - just call the script filename without .meg., test1.m has this code:disp('x');test2.m has this code:test1;disp('y');Result will be:xyProbably might work in Matlab I guess.
 
User avatar
fire
Posts: 0
Joined: July 10th, 2003, 10:11 pm

Matlab VBA Gurus

August 3rd, 2006, 6:46 pm

works the same in MATLAB as mentioned by asd
 
User avatar
user123
Posts: 0
Joined: July 7th, 2009, 3:07 pm

Matlab VBA Gurus

July 9th, 2009, 12:55 pm

I have the following problem, I need traslate this function of MAtlab to visual basic, can someone help me with this? I will appreciate all help in this matterfunction [P_LR, pi_LR, Moments ] = LR_distr(P,pi0,S)% DistributionMax_Iter = 500; %mximum iterationsconverg = 1.e-5;Pk = P; %initialize Pkfor i = 1 : Max_Iter %loop to find the invariant P, i.e. P_LR Precord = Pk; Pk = Pk*P;% if norm( Pk - Precord, Inf )< converg; display('Q converged'); P_LR=Pk; break; end if norm( Pk - Precord, Inf )< converg; P_LR=Pk; break; endend if norm( Pk - Precord, Inf )>= converg; P_LR=[]; pi_LR=[]; display('Q didn`t converge; Increase Max_Iter'); else pik = pi0; %initialize pik for i = 1 : Max_Iter %loop to find the invariant pi, i.e. pi_LR pirecord= pik; pik = P'*pik;% if norm( pik - pirecord, Inf )< converg; display('pi converged'); pi_LR=Pk(1,: )'; break; end if norm( pik - pirecord, Inf )< converg; pi_LR=Pk(1,: )'; break; end end if norm( pik - pirecord, Inf )>= converg;; display('pi didn`t converge; Increase Max_Iter'); end; [V,D] = eig(P'); %obtain number of invariant distributionsnumeig = 0; %count # of positive eigenvalues PI = []; %initialize matrix of distributionsfor i=1:size(P,1); if abs(D(i,i)-1) < converg, numeig = numeig + 1; PI(:,numeig) = V(:,1)/sum(V(:,i)); endend display({'# of invariant distr = %d', numeig});Moments(1,1) = S'*pi_LR;%(1,: )'; %unconditional mean%Moments(2,1) = sum( (S - (S'*pi_LR)).^2.*pi_LR); %unconditional varianceMoments(2,1) = sqrt(pi_LR'*((S - Moments(1,1)).^2)); %unconditional varianceend %this closes the "else" on line 26
 
User avatar
analyst78
Posts: 0
Joined: March 9th, 2009, 8:09 pm

Matlab VBA Gurus

July 24th, 2009, 7:27 pm

Why don't you write them as callable functions??QuoteOriginally posted by: asd"Is there someway of connecting all the scripts together...ie calling teh alogrithm in script 2 from script 1???"In Octave it is simple to connect the scripts - just call the script filename without .meg., test1.m has this code:disp('x');test2.m has this code:test1;disp('y');Result will be:xyProbably might work in Matlab I guess.