-1

When working in Matlab, I'm receiving this warning...

Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 6.001064e-34.

Here is my code...

clear
clc

P = [];
X = importdata('MSFT.csv',',');
P = [P X.data(:,5)];
X = importdata('ORCL.csv',',');
P = [P X.data(:,5)];
X = importdata('TEVA.csv',',');
P = [P X.data(:,5)];

TCR = {'msft', 'orcl', 'teva'};
N = 252;

r_ar = P(2:end, :)./P(1:end-1, :) - 1;
mu_d = mean(r_ar(1:end, :));
sigma_d = cov(r_ar(1:end, :));

mu_a = (mu_d + 1).^N -1;
sigma_a = 
   (sigma_d + (mu_d'+1)*(mu_d+1)).^N - (mu_d'+1).^N * (mu_d+1).^N;

rho =inv(sqrt(diag(diag(sigma_a))))*sigma_a*inv(sqrt(diag(diag(sigma_a))));

mu = mu_a';
S1 = inv(sigma_a);

The Warning is given for the line,

S1 = inv(sigma_a);

As I need to take the inverse of the sigma_a matrix is there any way to fix this warning? It's effecting other parts of my code.

user6259845
  • 185
  • 8
  • The warning given when using `inv()` is not there for festive orange lines only. Read it, and act on it. – Adriaan Oct 31 '18 at 09:55
  • I'd like to add to the other comments that computing `inv` is usually not the right way to do things, simply because the inverse is not usually needed on its own. Typically, the inverse is used to solve a linear equation system etc, in which case, as mentioned, `mldivide` should be used. Sometimes you don't need the inverse, but a _pseudo-_ inverse (see also: `pinv`). In least squares, `(M.' * M) \ M.'` is used for non-square matrices. To conclude - we need to know what you are planning to do with the inverse matrices after they're computed, to know for sure which advice to give you. – Dev-iL Oct 31 '18 at 12:28

1 Answers1

0

Look at your workspace. Is sigma_a a n x m Matrix or is it just one integer value? Because I would expect that kind of a warning if it would be just a value. If not, could you post how sigma_a looks like?

Edit: use mldivide instead of inv

S. Chris
  • 115
  • 11