MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayTo resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Asked by Kevin van Berkel on 2 Jun 2013

Hi guys,

I constantly receive these errors:

Error using lscov (line 105) B must have the same number of rows as A.

Error in bisquare (line 10) beta(:,1)=lscov(X,y);

Error in Portfolioweights_it_bs (line 13) abeta = bisquare(X, r(:,t+1),k);

Anyone a clue how to resolve?

Thanks!

This is the code for the bisquare estimator:

function BS = bisquare(X, y, k) %X is regression matrix %y are values of dependent variables N_max = 20; % maximum number of iterations for biqsquare optimization [M,N]=size(X); beta = zeros(N,N_max); beta(:,1)=lscov(X,y); e = y-X*beta(:,1); sigmahat = mad(e)/0.6745; k=sigmahat*4.685;

The code I use to generate the data (partly) before that is this one:

function Weights = Portfolioweights_it_bs(r, z, gamma, r_f, constr_ON, it_ON) N_max = 10; k = 1; [M,T] = size(r); x = zeros(M,T-1); %% t=T-1; %% X = [ones(M,1);z(t,1) ;z(t,1).^2]; abeta = bisquare(X, r(:,t+1),k); ahat = X*abeta; bbeta = bisquare(X, r(:,t+1).^2,k); bhat = X*bbeta; x(:,t) = r_f/gamma * ahat./bhat; if it_ON==1 disp('4th order Taylor iteration used') x_it = zeros(M,N_max); i=1; x_it(:,1) = x(:,t); cbeta = bisquare(X, (x_it(:,i).*r(:,t+1)).^2*r(:,t+1),k); chat = X*cbeta; dbeta = bisquare(X, (x_it(:,i).*r(:,t+1)).^3*r(:,t+1),k); dhat = X*dbeta; x_it(:,i+1) = x(:,t)- b(hat).^(-1).*(0.5*(-gamma-1)/r_f*chat+1/6*(-gamma-1)/r_f^2*(-gamma-2)*dhat); while ( max(abs(x_it(:,i+1)-x_it(:,i)))>0.01 && i<N_max) i=i+1 cbeta = bisquare(X, (x_it(:,i).r(:,t+1)).^2.*r(:,t+1),k); chat = X*cbeta; dbeta = bisquare(X, (x_it(:,i).r(:,t+1)).^3.*r(:,t+1),k); dhat = X*dbeta; x_it(:,i+1) = x(:,t)- b(hat).^(-1).*(0.5*(-gamma-1)/r_f*chat+1/6*(-gamma-1)/r_f^2*(-gamma-2)*dhat); end if i==N_max disp('Maximum number of iteration steps used') end x(:,t)=x_it(:,i+1); else disp('2nd order Taylor iteration used') end if constr_ON==1 x(:,t)=max(0,min(x(:,t),1)); disp('Constraints on x') end j=1; beta(:,j+1)= inv(X'*spdiags(W_BS(e,k),0,M,M)*X)*X'*spdiags(W_BS(e,k),0,M,M)*y; while (max(abs(beta(:,j+1)-beta(:,j)))>0.001 && j<N_max) e = y-X*beta(:,j+1); sigmahat = mad(e)/0.6745; k=sigmahat*4.685; j=j+1; beta(:,j+1)= inv(X'*spdiags(W_BS(e,k),0,M,M)*X)*X'*spdiags(W_BS(e,k),0,M,M)*y; end if j==N_max disp('Bisquare uses maximal number of iterations') end BS = beta(:,j+1);

So I am clearly missing something but can't figure out what...

*No products are associated with this question.*

Answer by the cyclist on 2 Jun 2013

Edited by the cyclist on 2 Jun 2013

It would be helpful if you showed us some of your actual code. However, it looks like you call the function lscov in line 10 of your function bisquare, and when you do that, the variable *X* has a different number of rows than your variable *y*. You might want to take a look at the documentation for lscov:

doc lscov

Added after code was included:

Is it possible for you to edit your code so that it is a completely self-contained example that can be executed and show the error?

Have you tried using debugging mode to breakpoint into the code and look at the sizes of the inputs to lscov?

http://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html#brqxeeu-177

Answer by Kevin van Berkel on 10 Jun 2013

Hi Cyclist,

Sorry for my late response. I tried numerous ways to resolve it, even with lscov but it still does not work.

I tried using debugging mode and it gave me this result:

X = [ones(M,1); z(t,1); z(t,1).^2]; abeta = bisquare(X, r(:,t+1),k);

it seems my problem is in here. I can't figure out how to match A and B together..

Answer by Kevin van Berkel on 10 Jun 2013

So,

X = [ones(M,1); z(t,1); z(t,1).^2]; abeta = bisquare(X, r(:,t+1),k);

in here the problem must be somewhere, since X does not agree with y(r(:,t+1)

## 1 Comment

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/77737#comment_152430

Kevin, I moved your code up from an "answer" into your question where it is better placed.