BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Cherrie
Fluorite | Level 6

Hi,

 

I have some code that tries to minimize a function.

The code is below:

 

start odr_yrly (matrix, year);
		yr_odr = j(nrow(matrix), year + 1, 0);
		yr_odr[,1] = matrix[,ncol(matrix)];
		do i = 1 to year;
			yr_odr[,i + 1] = matrix ** 4 * yr_odr[,i];
		end;
		yr_odr = yr_odr[,2:6];
		return(yr_odr);
	finish;

	start multi_year_dr_error(odr, full_matrix, year, pop);
		odr_last = j(1,ncol(odr),1);
		odr_full = odr // odr_last;
		pop_dist = pop[,5] // {0};
		odr_eff = odr_full[,3:6];
		idx = loc(odr_eff > 0);
		log_odr	= j(nrow(odr_eff),ncol(odr_eff),0);
		if ncol(idx) > 0 then log_odr[idx] = log(odr_eff[idx]);
		odr_est = odr_yrly(full_matrix, year);
		idx2 = loc(odr_est > 0);
		log_odr_est	= j(nrow(odr_est),ncol(odr_est),0);
		if ncol(idx2) > 0 then log_odr_est[idx2] = log(odr_est[idx2]);
		diff = (log_odr - log_odr_est[,2:5]) # pop_dist;
		print log_odr diff pop_dist log_odr_est;
		diff_sq = diff ## 2;
		ssq = diff_sq[+];
		return(ssq);
	finish;

Now I can't see any problem with my matrix multiplication line yr_odr[,i + 1] = matrix ** 4 * yr_odr[,i];

however it is giving me errors saying it overflowed.

Cherrie_0-1660523252208.png

anyone has any idea why that is the case? Happy to post the full optimisation code here but its some long code with multiple parts to the optimisation.

 

Regards,

Cherrie

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

The error occurs while you are attempting a nonlinear optimization with a nonlinear constraint. Without seeing the full program, I cannot determine why the error occurs. 

View solution in original post

4 REPLIES 4
IanWakeling
Barite | Level 11

The syntax "matrix ** 4" is taking the 4th power of the matrix - so the whole matrix multiplied by itself 4 times.  This might not be what you intend and could potentially lead to an overflow.

Cherrie
Fluorite | Level 6

Hi,

 

Thanks for your reply. But this is what I intend to do though. However wouldn't overflow be on numbers that are extremely large? My matrix is a transition probability matrix with all entries less than 1...

 

That's why I don't understand the error and how to solve it.

Rick_SAS
SAS Super FREQ

The error occurs while you are attempting a nonlinear optimization with a nonlinear constraint. Without seeing the full program, I cannot determine why the error occurs. 

Cherrie
Fluorite | Level 6

Thanks for the reply. I solved this issue.

 

What happens is I need to trial and error the migration matrix which supposed to be sum to 1 and have values between 0 and 1. however the optimisation program did not have this constraint programmed in so it gives me really large matrices (with individual cell even more than 3e60) which I used to 4th power. This resulted in an overflow. 

 

I have customised the error function so that when row sums of the matrix are more than 1 I force the error function to a really large value. This resolved this issue. Thanks for the reply! 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 4 replies
  • 1035 views
  • 1 like
  • 3 in conversation