全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
3160 3
2020-06-20

经管之家
作者:刘旭东


Copyright © https://www.federalreserve.gov/
The Federal Reserve System is the central bank of the United States.
License: GPL-2


R PACKAGE AMA/DEsc riptION

Package: AMA
Type: Package
Title: Anderson-Moore Algorithm
Version: 1.0.8
Depends: rJava
Date: 2010-10-13
Author: Gary Anderson, Aneesh Raghunandan
Maintainer: Gary Anderson <gary.anderson@frb.gov>
Desc ription: Implements Anderson-Moore algorithm for solving linear
        rational expectations models.  For information about the
        algorithm and its uses, please see
        http://www.federalreserve.gov/pubs/oss/oss4/aimindex.html.
        This version works on both Unix and Windows.  For questions
        about the algorithm and its implementation/applications, please
        contact gary.anderson@frb.gov.  If you are having technical
        issues with the package, please contact
        aneesh.raghunandan@yale.edu.
License: GPL-2
Copyright: The "C" code, sparseAim.c, implementing the basic algorithm
        is in the public domain and may be used freely.  However, the
        authors would appreciate acknowledgement of the source by
        citation of any of the following papers: Anderson, G. and
        Moore, G. "A Linear Algebraic Procedure For Solving Linear
        Perfect Foresight Models."  Economics Letters, 17, 1985.
        Anderson, G. "Solving Linear Rational Expectations Models: A
        Horse Race." Computational Economics, 2008, vol. 31, issue 2,
        pp. 95-113 Anderson, G. "A Reliable and Computationally
        Efficient Algorithm for Imposing the Saddle Point Property in
        Dynamic Models." Journal of Economic Dynamics and Control,
        2010, vol 34, issue 3, pp. 472-489. \n
SystemRequirements: Java
LazyLoad: yes
Packaged: 2010-10-27 15:11:14 UTC; m1gsa00
Repository: CRAN
Date/Publication: 2010-10-27 19:18:40

PACKAGE AMA/src/callSparseAim.c

C file

/* ---------------------------------------------------------------------------- 
 * callSparseAim.c                                                              
 * wrapper to sparseAim()												        
 * Allocates memory																
 * for sparseAim and calls sparseAim 
 * takes as input a full H matrix, and returns a full cofb matrix
 * also, Q (asymptotic constraints) and S (structural coef) matrices
 *
 *
 * At the bottom is an R wrapper (the function is "callSparseAimFromR")
 * R wrapper is necessary because when calling C from R, all arguments
 * must be passed by reference.
 * ---------------------------------------------------------------------------- */
#include "sparseAim.h"

#define cpuTime() (( (double)clock() ) / CLOCKS_PER_SEC)	      


void extern obtainSparseReducedForm();

void extern submat_();
int USEARPACK=1;
int TESTBLANCHARDKAHN=1;
double ZERO_TOLERANCE = 0.00000000001;
double ZERO_TOL1 = 0.0001;



void callSparseAim(double *hmatFull,int hrows,int hcols,
				   int neq,int leads,int lags,int nstate,
				   int qmax, 
				   double *returnCodePointer,
				   double *cofb, double *qmatrix)
{

	/* declare coefficient matrices, etc */
	double *hmat, *newHmat, *qmat, *bmat, *sbmat;
	int *hmatj, *newHmatj, *qmatj, *bmatj, *sbmatj;
	int *hmati, *newHmati, *qmati, *bmati, *sbmati;
	double *rootr, *rooti;

	/* declarations for sparseAim call */ 
	int qrows, qcols, brows, bcols, sbrows, sbcols;
	int maxSize, discreteTime ;
	int auxCond, rowsInQ, essential;
	void *aPointerToVoid ;

	/* rwt work variables  */
	int i, nnz, ierr;
	int returnCode;
	int one = 1;
   	qrows = neq * leads;
   	qcols = neq * (lags + leads);

   	bcols = neq * lags;
	maxSize = qmax ;
	rowsInQ = auxCond = 0 ;


	/* alloc space for H matrix */
	hmat = (double *)calloc(maxSize,sizeof(double)) ;
	hmatj = (int *)calloc(maxSize,sizeof(int)) ;
	hmati = (int *)calloc((neq+1),sizeof(int)) ;
    /* convert incoming full H matrix (hmatFull) to CSR sparse */
	int hrowsCopy = hrows;

	dnsToCsr (&hrows, &hcols, &maxSize, hmatFull, &hrowsCopy, hmat, hmatj, hmati, &ierr) ;

	/* allocate space for arrays needed in call to sparseAim*/
	newHmat = (double *)calloc(maxSize,sizeof(double)) ;
	newHmatj = (int *)calloc(maxSize,sizeof(int)) ;
	newHmati = (int *)calloc((neq+1),sizeof(int)) ;

	qmat = (double *)calloc(maxSize,sizeof(double)) ;
	qmatj = (int *)calloc(maxSize,sizeof(int)) ;
	qmati = (int *)calloc((qrows+1),sizeof(int)) ;

	rootr = (double *)calloc(qcols,sizeof(double)) ;
	rooti = (double *)calloc(qcols,sizeof(double)) ;


	
			      
	/* zero roots to start */
	for (i=0; i<qcols; i++) 
		rootr[i] = rooti[i] = 0.0 ;

	/* initialize other arguments */
	discreteTime = 1 ;
	auxCond = 0 ;
	rowsInQ = 0 ;
	qmati[0] = 1 ;	/*this makes for a sparse matrix with no nonzero elements */
	essential = 0 ;
	returnCode = 0 ;
	aPointerToVoid = (void *)NULL ;


	/* and call sparseAim */
	sparseAim (
		&maxSize, discreteTime, hrows, hcols, leads,
		hmat, hmatj, hmati, newHmat, newHmatj, newHmati,
		&auxCond, &rowsInQ, qmat, qmatj, qmati,
		&essential, rootr, rooti, &returnCode, aPointerToVoid
	) ;
	

	*returnCodePointer = returnCode;
	if (returnCode == 0) {
		/* compute B matrix */
		bmat = (double *)calloc(maxSize,sizeof(double)) ;
		bmatj = (int *)calloc(maxSize,sizeof(int)) ;
		bmati = (int *)calloc((qrows+1),sizeof(int)) ;

		obtainSparseReducedForm (&maxSize, hrows*leads, hcols-hrows,
			qmat, qmatj, qmati, bmat, bmatj, bmati
		) ;
	fflush(stdout);

	
		csrToDns (&qrows, &qcols, qmat, qmatj, qmati, qmatrix, &qrows, &ierr);
	

		free (hmat) ; free (hmatj) ; free (hmati);
		free (newHmat);	free (newHmatj) ; free (newHmati);
		free (qmat); free (qmatj); free (qmati);


		// take submat of bmat to include only the first HROWS rows.
		sbmat = (double *)calloc(maxSize,sizeof(double)) ;
		sbmatj = (int *)calloc(maxSize,sizeof(int)) ;
		sbmati = (int *)calloc((hrows+1),sizeof(int)) ;
		sbrows = hrows;
		sbcols = bcols;

		submat_(&qrows,&one,&one,&hrows,&one,&bcols,bmat,bmatj,bmati,&sbrows,&sbcols,sbmat,sbmatj,sbmati);
		free(bmat); free(bmatj); free(bmati);



		csrToDns (&sbrows, &sbcols, sbmat, sbmatj, sbmati, cofb, &sbrows, &ierr) ;
		nnz = sbmati[sbrows]-sbmati[0];


		
	}   /* if returnCode == 0 */


}	/* end main fn */




/* BEGIN R WRAPPER */

void callSparseAimFromR(double *hmatFull, int *Rhrows, int * Rhcols,
		int * Rneq, int * Rleads, int * Rlags, int * Rnstate,
		int * Rqmax, double * returnCodePointer,
		double *cofb, double *qmatrix){

	int hrows = *Rhrows;
	int hcols = *Rhcols;
	int neq = *Rneq;
	int leads = *Rleads;
	int lags = *Rlags;
	int nstate = *Rnstate;
	int qmax = *Rqmax;
	callSparseAim(hmatFull, hrows, hcols, neq, leads, lags,
			nstate, qmax, returnCodePointer, cofb, qmatrix);

}

package AMA/src/dstatn.f

FORTRAN file

c
c     %---------------------------------------------%
c     | Initialize statistic and timing information |
c     | for nonsymmetric Arnoldi code.              |
c     %---------------------------------------------%
c
c\Author
c     Danny Sorensen               Phuong Vu
c     Richard Lehoucq              CRPC / Rice University
c     Dept. of Computational &     Houston, Texas
c     Applied Mathematics
c     Rice University           
c     Houston, Texas    
c
c\SCCS Information: @(#) 
c FILE: statn.F   SID: 2.4   DATE OF SID: 4/20/96   RELEASE: 2
c
      subroutine dstatn
c
c     %--------------------------------%
c     | See stat.doc for documentation |
c     %--------------------------------%
c
      include   'stat.h'
c 
c     %-----------------------%
c     | Executable Statements |
c     %-----------------------%
c
      nopx   = 0
      nbx    = 0
      nrorth = 0
      nitref = 0
      nrstrt = 0
c 
      tnaupd = 0.0D+0
      tnaup2 = 0.0D+0
      tnaitr = 0.0D+0
      tneigh = 0.0D+0
      tngets = 0.0D+0
      tnapps = 0.0D+0
      tnconv = 0.0D+0
      titref = 0.0D+0
      tgetv0 = 0.0D+0
      trvec  = 0.0D+0
c 
c     %----------------------------------------------------%
c     | User time including reverse communication overhead |
c     %----------------------------------------------------%
c
      tmvopx = 0.0D+0
      tmvbx  = 0.0D+0
c 
      return
c
c
c     %---------------%
c     | End of dstatn |
c     %---------------%
c
      end

编译和安装工具包

==> Rcmd.exe INSTALL --no-multiarch --with-keep.source AMA

* installing to library 'D:/Softwares/R-4.0.0/library'
* installing *source* package 'AMA' ...
** using staged installation
** libs
"C:/rtools40/mingw64/bin/"gcc  -I"D:/SOFTWA~1/R-40~1.0/include" -DNDEBUG          -O2 -Wall  -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c callSparseAim.c -o callSparseAim.o
callSparseAim.c: In function 'callSparseAim':
callSparseAim.c:49:9: warning: variable 'nnz' set but not used [-Wunused-but-set-variable]
  int i, nnz, ierr;
         ^~~
callSparseAim.c:43:20: warning: unused variable 'brows' [-Wunused-variable]
  int qrows, qcols, brows, bcols, sbrows, sbcols;
                    ^~~~~
In file included from callSparseAim.c:14:
callSparseAim.c: At top level:
sparseAim.h:137:12: warning: 'validCSRMatrix' declared 'static' but never defined [-Wunused-function]
 static int validCSRMatrix(int numRows,double * mata,int * matj,int *mati);
            ^~~~~~~~~~~~~~
sparseAim.h:138:12: warning: 'validVector' declared 'static' but never defined [-Wunused-function]
 static int validVector(int numRows,double * vec);
            ^~~~~~~~~~~
sparseAim.h:166:12: warning: 'autoRegression' declared 'static' but never defined [-Wunused-function]
 static int autoRegression() ;
            ^~~~~~~~~~~~~~
sparseAim.h:167:12: warning: 'shiftRightAndRecord' declared 'static' but never defined [-Wunused-function]
 static int shiftRightAndRecord() ;
            ^~~~~~~~~~~~~~~~~~~
sparseAim.h:168:12: warning: 'annihilateRows' declared 'static' but never defined [-Wunused-function]
 static int annihilateRows() ;
            ^~~~~~~~~~~~~~
sparseAim.h:169:12: warning: 'constructQRDecomposition' declared 'static' but never defined [-Wunused-function]
 static int constructQRDecomposition();
            ^~~~~~~~~~~~~~~~~~~~~~~~
sparseAim.h:170:12: warning: 'augmentQmatWithInvariantSpaceVectors' declared 'static' but never defined [-Wunused-function]
 static int augmentQmatWithInvariantSpaceVectors();
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sparseAim.h:171:12: warning: 'identifyEssential' declared 'static' but never defined [-Wunused-function]
 static int identifyEssential() ;
            ^~~~~~~~~~~~~~~~~
sparseAim.h:172:13: warning: 'constructA' declared 'static' but never defined [-Wunused-function]
 static void constructA() ;
             ^~~~~~~~~~
sparseAim.h:173:12: warning: 'useArpack' declared 'static' but never defined [-Wunused-function]
 static int useArpack() ;
            ^~~~~~~~~
sparseAim.h:176:12: warning: 'lineNumberToViolation' declared 'static' but never defined [-Wunused-function]
 static int lineNumberToViolation(int lineNo);
            ^~~~~~~~~~~~~~~~~~~~~
sparseAim.h:177:15: warning: 'lineNumberToString' declared 'static' but never defined [-Wunused-function]
 static char * lineNumberToString(int lineNo);
               ^~~~~~~~~~~~~~~~~~
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c dgetv0.f -o dgetv0.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c dlaqrb.f -o dlaqrb.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c dmout.f -o dmout.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c dnaitr.f -o dnaitr.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c dnapps.f -o dnapps.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c dnaup2.f -o dnaup2.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c dnaupd.f -o dnaupd.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c dnconv.f -o dnconv.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c dneigh.f -o dneigh.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c dneupd.f -o dneupd.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c dngets.f -o dngets.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c dsortc.f -o dsortc.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c dstatn.f -o dstatn.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c dvout.f -o dvout.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c ivout.f -o ivout.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c ma50ad.f -o ma50ad.o
"C:/rtools40/mingw64/bin/"gfortran  -fno-optimize-sibling-calls    -O2  -mfpmath=sse -msse2 -mstackrealign -c second.f -o second.o
"C:/rtools40/mingw64/bin/"gcc  -I"D:/SOFTWA~1/R-40~1.0/include" -DNDEBUG          -O2 -Wall  -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c sparseAim.c -o sparseAim.o
sparseAim.c: In function 'augmentQmatWithInvariantSpaceVectors':
sparseAim.c:1068:7: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
       if(js[i] != nxt)valid=FALSE;nxt=nxt+1;
       ^~
sparseAim.c:1068:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
       if(js[i] != nxt)valid=FALSE;nxt=nxt+1;
                                   ^~~
sparseAim.c:1037:32: warning: variable 'time_dgees' set but not used [-Wunused-but-set-variable]
  double time0, time_useArpack, time_dgees, time_constructA ;
                                ^~~~~~~~~~
sparseAim.c:1035:15: warning: unused variable 'ONE' [-Wunused-variable]
  unsigned int ONE=1 ;
               ^~~
sparseAim.c: In function 'constructA':
sparseAim.c:1410:6: warning: variable 'originalMaxHElements' set but not used [-Wunused-but-set-variable]
  int originalMaxHElements;
      ^~~~~~~~~~~~~~~~~~~~
sparseAim.c: In function 'useArpack':
sparseAim.c:1599:22: warning: unused variable 'TWO' [-Wunused-variable]
  unsigned int ONE=1, TWO=2 ;
                      ^~~
sparseAim.c:1599:15: warning: unused variable 'ONE' [-Wunused-variable]
  unsigned int ONE=1, TWO=2 ;
               ^~~
sparseAim.c: In function 'obtainSparseReducedForm':
sparseAim.c:1836:9: warning: variable 'time0' set but not used [-Wunused-but-set-variable]
  double time0 ;
         ^~~~~
sparseAim.c:1835:6: warning: variable 'originalMaxHElements' set but not used [-Wunused-but-set-variable]
  int originalMaxHElements;
      ^~~~~~~~~~~~~~~~~~~~
sparseAim.c: In function 'satisfiesLinearSystemQ':
sparseAim.c:2077:6: warning: variable 'originalMaxHElements' set but not used [-Wunused-but-set-variable]
  int originalMaxHElements;
      ^~~~~~~~~~~~~~~~~~~~
"C:/rtools40/mingw64/bin/"gcc  -I"D:/SOFTWA~1/R-40~1.0/include" -DNDEBUG          -O2 -Wall  -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c sparskit2.c -o sparskit2.o
sparskit2.c:51:16: warning: 'c__3' defined but not used [-Wunused-variable]
 static integer c__3 = 3;
                ^~~~
sparskit2.c:50:16: warning: 'c__9' defined but not used [-Wunused-variable]
 static integer c__9 = 9;
                ^~~~
sparskit2.c:49:16: warning: 'c__1' defined but not used [-Wunused-variable]
 static integer c__1 = 1;
                ^~~~
C:/rtools40/mingw64/bin/gcc -shared -s -static-libgcc -o AMA.dll tmp.def callSparseAim.o dgetv0.o dlaqrb.o dmout.o dnaitr.o dnapps.o dnaup2.o dnaupd.o dnconv.o dneigh.o dneupd.o dngets.o dsortc.o dstatn.o dvout.o ivout.o ma50ad.o second.o sparseAim.o sparskit2.o -LD:/SOFTWA~1/R-40~1.0/bin/x64 -lRlapack -LD:/SOFTWA~1/R-40~1.0/bin/x64 -lRblas -lgfortran -lm -lquadmath -lgfortran -lm -lquadmath -LD:/SOFTWA~1/R-40~1.0/bin/x64 -lR
installing to D:/Softwares/R-4.0.0/library/00LOCK-AMA/00new/AMA/libs/x64
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
  converting help for package 'AMA'
    finding HTML links ... ����
    Lcofbob2                                html  
    callAMA                                 html  
    example7                                html  
    example7params                          html  
    genBmat                                 html  
    genHmat                                 html  
    genHmatrixsc ripts                       html  
    genQmat                                 html  
    genScof                                 html  
    getFactorMatrices                       html  
    getReturnCode                           html  
    getStochTrans                           html  
** building package indices
** testing if installed package can be loaded from temporary location
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (AMA)

未来展望

这个是美联储的工具包,我不知道根据R的规则我是否可以修改这个代码公开发布以及提供给R官网。
For the details of MODELEZ syntax, see for example http://www.federalreserve.gov/pubs/
oss/oss4/papers/habitMatlab/habitMatlab.html
For details about the algorithm:

References

Gary S. Anderson. “A reliable and computationally efficient algorithm for imposing the saddle
point property in dynamic models.” Journal of Economic Dynamics & Control, 2010.
Gary Anderson and George Moore. “An Efficient Procedure for Solving Linear Perfect Foresight
Models.” Unpublished manusc ript, Board of Governors of the Federal Reserve System. 1983.

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2020-11-8 19:26:13
$$
\tag{1.1}
\hat{\beta^2}=\omiga
$$
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2020-11-8 19:27:37
$$
\tag{公式1.2} L(\beta,\rho|x) = \underbrace{\rho\sum_{i=1}^n \ln\beta_i - n\ln\Gamma(\rho) +
(\rho-1)\sum_{i=1}^n \ln y_i - \sum_{i=1}^n y_i\beta_i}_{\text{tulipsliu}}
$$
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2024-4-18 08:30:02
勘误,不能随意发布。

当年在测试 Markdown 的时候,也把这个工具箱的两个共同作者的参考文献写进取。 从手稿发布与1983 年来说,这是两位作者40余年,呕心沥血的脑力创作。 尊重原作者,这个工具箱是不能随意被发布,在没有作者的同意与授权(两位作者一生的脑力劳工贡献), 而且,可以预见的是,两位作者也不会同意。



Unpublished manuscript, Board of Governors of the Federal Reserve System. 1983.

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群