Nonnumeric values

Hello,

I am new to CVX, I am trying to use it in Matlab from a code from GITHUB. The code is perfect since the author has made it available and also it runs with the data attached to it.

But when I run with my data, there is an error that my data contains Invalid numeric values, but unfortunately, my data do not possess such.

Error using * (line 55)
Disciplined convex programming error:
Invalid numeric values (NaNs) may not be used in CVX expressions.

Can anyone help me.

Thanking You

Somehow there are NaN 's in your program. You should check the value of every MATLAB variable being used in the statement which triggers the error message. Or you can use the isnan function.

Perhaps an NaN was produced from an earlier unsuccessful computation.

If you supply a reproducible problem, perhaps we can provide more specific help.

How can I send the codes and the data file to you also I asked the author of the code his reply was " The error message " Invalid numeric values (NaNs) may not be used in CVX expressions. " is produced when there is “0/0” case. You can check which denominator becomes 0 before the error halts the program."

So I am unable to figure out a way out

You can copy and paste code and data (hopefully) into a post.

You should be able to determine which line of code triggers the error message. Then examine the value of all the MATLAV variables in that line of code, and you should be able to see which are NaN. Then trace back through your program to determine why they are NaN, such as 0/0 (there are other possible causes, but maybe the program;s author already knows 0/0 can happen on their code, in which case it is not a very robustly written code).

Check, for the dependencies you can have a look in the following link: https://github.com/zhentaoshi/C-Lasso/tree/master/app_saving_PLS

The attached data runs fine with the code, but when I change it to my own data, I get that error

% Liangjun Su, Zhentao Shi, Peter Phillips (2015)
% the master file of PLS estimation in the savings rate application

clear
global p K_max

cvx_solver mosek

IC_needed = 1;
tol = 0.0001;
R = 80;

load(‘balancedPanelX1995.mat’)
X = [lagsaving, cpi, interest, gdp];
y = saving;

p = size(X, 2);
T = 15;
N = 56;

K_max = 5;
lamb.grid = 10;
lamb.min = 0.2;
lamb.max = 2.0;
lamb_const = lamb.min * (lamb.max / lamb.min ).^( ( (1:lamb.grid) - 1) /( lamb.grid -1 ) ); % the constant for lambda. very important!!
numlam = length(lamb_const);

index = dataset( code, year, y, X );
index.Properties.VarNames = {‘N’ ‘T’ ‘y’ ‘X’};

y_raw = y;
X_raw = X;

for i = 1:N
yi = y(index.N == i);
mean_yi = mean(yi);
yi = bsxfun(@minus, yi, mean(yi) );
y(index.N == i) = yi/std(yi, 1);
y_raw(index.N==i) = y(index.N == i) + mean_yi;

Xi = X(index.N == i, : );
mean_Xi = mean(Xi);
Xi = bsxfun(@minus, Xi, mean(Xi) );
X(index.N == i, :) = Xi./repmat( std(Xi, 1), [T 1] ) ;
X_raw(index.N == i, :) = X(index.N == i, :) + repmat( mean(Xi), [T 1]);

end

ds = dataset( code, year, y, X, y_raw, X_raw );
ds.Properties.VarNames = {‘N’ ‘T’ ‘y’ ‘X’ ‘y_raw’ ‘X_raw’};
%% initial values
beta_hat0 = zeros(N, p);
for i = 1:N
yi = ds.y(ds.N == i );
Xi = ds.X(ds.N == i, : );
beta_hat0(i,:slight_smile: = regress( yi , Xi );
end

%% estimation
TT = T;
IC_total = ones(K_max, numlam );

if IC_needed == 1
for ll = 1:numlam
disp(ll)

    a = ds.X \ ds.y; 
    bias = SPJ_PLS(T,ds.y_raw, ds.X_raw);
    a_corr = 2 * a - bias;
    IC_total(1, :) = mean( ( y - X*a_corr ).^2 );
    
    
    for K = 2:K_max
        Q = 999*zeros(K,1);
        
        lam = lamb_const(ll)*var(y) * T^(-1/3);
        [b_K, hat.a] = PLS_est(N, TT, y, X, beta_hat0, K, lam, R, tol); % estimation
        [~, H.b, ~, group] = report_b( b_K, hat.a, K );
        sum(group)            

        post_b = zeros(N, p);
        post_a = zeros(K, p);
        if K >=2
            for i = 1:K
                NN = 1:N;
                H.group = logical(group);
                this_group = group(:,i);
                if sum(this_group) > 0
                    g_index = NN(this_group);
                    g_data = ds( ismember(ds.N, g_index), : );

                    post = post_est_PLS_dynamic(T, g_data);
                    
                    e = g_data.y - g_data.X * post.post_a_corr ;
                    Q(i) = sum( e.^2 );
                    post_b(this_group,:) = repmat(post.post_a_corr', [sum(this_group), 1] );
                end
            end
        end
        
        
        IC_total(K , ll) = sum(Q) / (N*T)
        
    end
end
%% calculate the IC
pen = 2/3 * (N*T)^(-.5) * p .* repmat( (1:K_max)', [1 numlam]);
IC_final = log(IC_total) + pen;
disp(IC_final)

end

%% PLS estimation
K = 2;
lam = 1.5485 *var(y) * T^(-1/3);

[b_K, a] = PLS_est(N, T, y, X, beta_hat0, K, lam, R, tol);
[~, b, ~ , group] = report_b( b_K, a, K );

%% post estimation
est_lasso = zeros(p, 6);
est_post_lasso = zeros(p, 6);

for i = 1:K
NN = 1:N;
group = logical(group);
this_group = group(:,i);
g_index = NN(this_group);
g_data = ds( ismember(ds.N, g_index), : ); % group-specific data
post = post_est_PLS_dynamic(T, g_data);
est_post_lasso(:,(3i-2):(3i)) = [post.post_a_corr, post.se, post.test_b];
end

%% display the estimates
est_post_lasso = mat2dataset( est_post_lasso, ‘VarNames’, …
{‘g1_coef’, ‘g1_sd’, ‘g1_t’, ‘g2_coef’, ‘g2_sd’, ‘g2_t’});
disp(est_post_lasso)

load(‘country56.mat’)
country(group(:,1))
country(group(:,2))

g_PLS = zeros(56,1);
g_PLS( group(:,1) == 1 ) = 1;
g_PLS( group(:,2) == 1 ) = 2;

load(‘group_PGMM.mat’)
g_PGMM = zeros(56,1);
g_PGMM( group_PGMM(:,2) == 1) = 2;
g_PGMM( group_PGMM(:,1) == 1) = 1;

sum(g_PLS == g_PGMM)
%% common FE

g_index = NN;
first_none_zero = min( NN );
g_data = ds( ismember(ds.N, g_index), : ); % group-specific data
post = post_est_PLS_dynamic(T, g_data);

[post.post_a_corr, post.se, post.test_b]

You have just posted a big mess which I don’t have the energy to go through. You need to determine the specific line of code which triggers the error message, and then look at the values of all the MATLAB variables in that line of code at the time that line of code is executed. The author of the code may be better able to more specifically help you, for instance when you find out why the NaN occurs, than can a reader of this forum.

I know its a mess and I am sorry for it. I am pasting what the author replied

Thanks for your message and your interest in our method.

The error message " Invalid numeric values (NaNs) may not be used in CVX expressions. " is produced when there is “0/0” case. You can check which denominator becomes 0 before the error halts the program.

The code is written for our empirical applications and simulations. We didn’t encounter this problem in our examples. Please understand that we are unable to foresee all pathological cases in other datasets.

It is impossible for me I cannot figure out which line is creating an Invalid Numeric expression in the code.

Is it possible for CVX to recognise Invalid Numeric Expressions (my guess is the expression might be 0/0)

You need to determine which line of CVX ode, which must be in one of the github-posted files, is triggering the error message. Then look a t the variables used in that line of code. You need ot use standard debugging techniques.

I know the CVX error, its in the file mtimes.m ( directory: cvx/ builtins/ @cvx) line 55

I am pasting you the whole loop :

%
% Check expression types
%

if cvx_isconstant( x ),

xC = cvx_constant( x );
if nnz( isnan( xC ) ),

(LINE 55) error( ‘Disciplined convex programming error:\n Invalid numeric values (NaNs) may not be used in CVX expressions.’, 1 ); %#ok
elseif cvx_isconstant( y ),
yC = cvx_constant( y );
if nnz( isnan( yC ) ),
error( ‘Disciplined convex programming error:\n Invalid numeric values (NaNs) may not be used in CVX expressions.’, 1 ); %#ok
end
z = feval( [ ‘m’, oper ], xC, yC );
if nnz( isnan( z ) ),
error( ‘Disciplined convex programming error:\n This expression produced one or more invalid numeric values (NaNs).’, 1 ); %#ok
end
z = cvx( z );
return
elseif isequal( oper, ‘rdivide’ ),
error( ‘Disciplined convex programming error:\n Matrix divisor must be constant.’, 1 ); %#ok
end

THANK YOU

You are displaying code from the cvx built-in function ntimes.m. You need to determine what user-level cvx program line this error occurred on, i.e., the line of code in the github files on which this cvx error message was triggered. Then look at everything on that line of code at the time of execution and determine what is NaN.

OK, any guess from you. I know that is very amateurish of me to ask but I am not expert in Matlab so.

You appear to need MATLAB help, not CVX help. Maybe you’re best off getting someone more knowledgeable in MATLAB than you to come to your computer and help.

I don’t know whether your problem solves more then one CVX (optimization) problem or not. If it does, and the optimal value of one CVX problem becomes an input to another CVX optimization problem, then the NaN could occur if the first CVX problem was unsuccessful, for instance, determined to be infeasible.

Thank You very much.