Issue with svd_econ() function from Armadillo library

Hi,

I'm new to C++. I have an issue with the svd_econ() function from the Armadillo library. I ran the following code on Visual Studios 2012. I'm trying to fit a least squares regression when the regressor data are the same but the regressand data are not.

Why does the fitted value (Output1 = 7.20161E+022) differ so much from the mean (Output2 = 0.00523477)? Is there a way to overcome this?

Would this issue come up again when the regressor data are very similar? Are there ways to address this?

Any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    #include <iostream>
    #include <armadillo>   
    using namespace arma;
    
    mat regressors(colvec);
    colvec reg_coeffs(mat,colvec);
    
    mat regressors(colvec Input)
    {
    	mat Output(Input.n_rows,4,fill::zeros);	
    	Output.col(0) += 1.0;
    	Output.col(1) = Input;
      	Output.col(2) = Input%Input;
      	Output.col(3) = Input%Input%Input;
    	return Output;
    }		
    	
    colvec reg_coeffs(mat Regressor, colvec Regressand)
    {	
    	mat U;
    	vec S;
    	mat V;		
    	svd_econ(U,S,V,Regressor);
    	colvec Output = V*inv(diagmat(S))*(U.t())*Regressand;		
    	return Output;	
    }
    
    int main()
    {	
    	colvec TestVector(100000);
    	TestVector.fill(5);
    	mat Regressor = regressors(TestVector);
    	colvec Regressand(100000,fill::randn);
    	
    	colvec Coeffs = reg_coeffs(Regressor,Regressand);
    	double FittedValue = sum(Regressor.row(0)*Coeffs);
    
    	double Output1 = FittedValue;
    	double Output2 = mean(Regressand);
    
    	std::cout << Output1 << endl;
    	std::cout << Output2 << endl;
    
    	return 0;
    }


My guess is that the single value decomposition failed and the output objects for svd_econ() are reset to whatever laid in the memory before. Does this sound reasonable? If this is the case, is there a way to perform least squares regression for situations like this?
Last edited on
Topic archived. No new replies allowed.