阅读权限 255 威望 0 级论坛币 386043 个 通用积分 526.9298 学术水平 127 点 热心指数 140 点 信用等级 103 点 经验 46986 点 帖子 1773 精华 0 在线时间 2509 小时 注册时间 2007-11-5 最后登录 2024-8-16
一段 MATLAB 可以编译的高级程序语言代码;
文件的后缀名是 .F90 格式。文件名 : policies_on_itc_mex.F90
!
! myinterp3_mex.F90
!
!
! Created by Volker Tjaden on 23.08.2013.
! Copyright 2013 __MyCompanyName__. All rights reserved.
!
#include "fintrf.h"
!-----------------------------------------------------------------------
! Gateway routine
!-----------------------------------------------------------------------
subroutine mexFunction(nlhs,plhs,nrhs,prhs)
implicit none
! Arguments in mexFunction
mwPointer :: plhs(*), prhs(*)
integer :: nlhs, nrhs
! Function declarations
mwPointer :: mxGetPr, mxCreateNumericMatrix
integer(4) :: mxClassIDFromClassName
integer(4) :: mxGetString
mwPointer :: mxGetN , mxGetM
! Pointers to input/output mxArrays:
! Input
mwPointer :: prices,EV,EV_down,EV_up,kgrid,q,rho,w,gam,del,b,beta,xibar
mwPointer :: tcred,itc
! Output
mwPointer :: kopt,xihat,up,down,uncont,kopt_ind
! Array size information
mwSize :: nks, neps, mwOne
integer(4) :: classid
integer(4) :: complexflag
!-----------------------------------------------------------------------
! Copy pointers
prices = mxGetPr(prhs(1))
EV = mxGetPr(prhs(2))
EV_down = mxGetPr(prhs(3))
EV_up = mxGetPr(prhs(4))
kgrid = mxGetPr(prhs(5))
q = mxGetPr(prhs(6))
rho = mxGetPr(prhs(7))
w = mxGetPr(prhs(8))
gam = mxGetPr(prhs(9))
del = mxGetPr(prhs(10))
b = mxGetPr(prhs(11))
beta = mxGetPr(prhs(12))
xibar = mxGetPr(prhs(13))
tcred = mxGetPr(prhs(14))
itc = mxGetPr(prhs(15))
! Retrieve size information
nks = mxGetM(prhs(2))
neps = mxGetN(prhs(2))
! Create return arguments
complexflag = 0
mwOne=1
classid = mxClassIDFromClassName('double')
plhs(1) = mxCreateNumericMatrix(mwOne,neps,classid, complexflag)
kopt = mxGetPr(plhs(1))
plhs(2) = mxCreateNumericMatrix(nks,neps,classid, complexflag)
xihat = mxGetPr(plhs(2))
classid = mxClassIDFromClassName('logical')
plhs(3) = mxCreateNumericMatrix(nks,neps,classid, complexflag)
up = mxGetPr(plhs(3))
plhs(4) = mxCreateNumericMatrix(nks,neps,classid, complexflag)
down = mxGetPr(plhs(4))
plhs(5) = mxCreateNumericMatrix(nks,neps,classid, complexflag)
uncont = mxGetPr(plhs(5))
classid = mxClassIDFromClassName('double')
plhs(6) = mxCreateNumericMatrix(mwOne,neps,classid, complexflag)
kopt_ind = mxGetPr(plhs(6))
! call subroutine
call policies_on(%val(kopt),%val(xihat),%val(up),%val(down),&
%val(uncont),%val(kopt_ind),%val(EV),%val(EV_down),&
%val(EV_up),%val(kgrid),%val(q),%val(rho),%val(w),&
%val(gam),%val(del),%val(b),%val(beta),%val(xibar),&
%val(tcred),%val(itc),nks,neps)
return
end subroutine
subroutine policies_on(kopt,xihat,up,down,uncont,kopt_ind,&
EV,EV_down,EV_up,kgrid,q,rho,w,&
gam,del,b,beta,xibar,tcred,tstate,nks,neps)
implicit none
real(8), intent(out) :: kopt(neps)
real(8), intent(out) :: xihat(nks,neps)
!logical*1, intent(out) :: up(nks,neps)
!logical*1, intent(out) :: uncont(nks,neps)
!logical*1, intent(out) :: down(nks,neps)
integer*1, intent(out) :: up(nks,neps)
integer*1, intent(out) :: uncont(nks,neps)
integer*1, intent(out) :: down(nks,neps)
real(8), intent(out) :: kopt_ind(neps)
real(8), intent(in) :: EV(nks,neps)
real(8), intent(in) :: EV_down(nks,neps)
real(8), intent(in) :: EV_up(nks,neps)
real(8), intent(in) :: kgrid(nks)
real(8), intent(in) :: q,rho,w,gam,del,b,beta,xibar
real(8), intent(in) :: tcred,tstate
mwSize, intent(in) :: nks,neps
! local variables
integer(4) :: i1,ind,pos,i2
real(8) :: E,EC,vbest,vcand
! Loop over idiosyncratic productivity states
ind=1
do i1=1,neps
! 1. Find capital choice
pos=0
vbest=-huge(vbest)
if (i1>1) then
ind=int(kopt_ind(i1-1),4)
end if
do while (ind<=nks)
vcand=-gam*kgrid(ind)*q*rho*(1.d0-tstate*tcred)&
+beta*EV(ind,i1)
if (vcand>vbest) then
vbest=vcand
pos=ind
else
exit
end if
ind=ind+1
end do
E=vbest
kopt_ind(i1)=real(pos,8)
kopt(i1)=kgrid(pos)
! 2. Check whether optimal choice is within constrained
! set and if not, in what direction constrain
do i2=1,nks
! (EC-repmat(E,mpar.nksim,1))./(rho*w);
if (((1.d0-del+b)/gam)*kgrid(i2)<kopt(i1)) then
up(i2,i1)=1 !.true.
EC=-(1.d0-del+b)*kgrid(i2)*q*rho*(1.d0-tstate*tcred)+&
beta*EV_up(i2,i1)
xihat(i2,i1)=-(EC-E)/(rho*w)
xihat(i2,i1)=min(xibar,max(0.d0,xihat(i2,i1)))
elseif(((1.d0-del-b)/gam)*kgrid(i2)>kopt(i1)) then
down(i2,i1)=1 !.true.
EC=-(1.d0-del-b)*kgrid(i2)*q*rho*(1.d0-tstate*tcred)+&
beta*EV_down(i2,i1)
xihat(i2,i1)=-(EC-E)/(rho*w)
xihat(i2,i1)=min(xibar,max(0.d0,xihat(i2,i1)))
else
uncont(i2,i1)=1!.true.
xihat(i2,i1)=0.d0
end if
end do
end do
end subroutine 复制代码