2 Star 10 Fork 20

kerrydu/gtfpch

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
gtfpch.mata 44.29 KB
一键复制 编辑 原始数据 按行查看 历史
kerrydu 提交于 2021-08-01 16:48 . autocompiled
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
version 16
// Versioning
_get_version gtfpch
assert("`package_version'" != "")
mata: string scalar gtfpch_version() return("`package_version'")
mata: string scalar gtfpch_stata_version() return("`c(stata_version)'")
mata: string scalar gtfpch_joint_version() return("`package_version'|`c(stata_version)'")
////////////////////////////////
cap mata mata drop _ddf()
cap mata mata drop _nddf()
cap mata mata drop _ddf2()
cap mata mata drop _nddf2()
cap mata mata drop _tenddf()
cap mata mata drop _tenddf2()
cap mata mata drop ddfcontemporaneous()
cap mata mata drop ddfcontemporaneousbt()
cap mata mata drop ddfbiennial()
cap mata mata drop ddfbiennialbt()
cap mata mata drop ddfsequential()
cap mata mata drop ddfsequentialbt()
cap mata mata drop ddfwindow()
cap mata mata drop ddfwindowbt()
cap mata mata drop ddfglobal()
cap mata mata drop ddfglobalbt()
cap mata mata drop nddfcontemporaneous()
cap mata mata drop nddfcontemporaneousbt()
cap mata mata drop nddfbiennial()
cap mata mata drop nddfbiennialbt()
cap mata mata drop nddfsequential()
cap mata mata drop nddfsequentialbt()
cap mata mata drop nddfwindow()
cap mata mata drop nddfwindowbt()
cap mata mata drop nddfglobal()
cap mata mata drop nddfglobalbt()
cap mata mata drop biasandciddf()
cap mata mata drop displaydots()
cap mata mata drop calddf()
cap mata mata drop calnddf()
cap mata mata drop bootdea()
mata:
//mata clear
void function displaydots(real scalar k)
{
if(mod(k,50)==0){
printf("..........%2.0f\n",k)
}
//else{
// printf(".")
//}
}
// define structure
struct bootdea {
real colvector tebc
real colvector LL
real colvector UU
real colvector vari
real colvector BovV
real colvector bias
}
real matrix function calddf(real matrix data,
real matrix dataref,
real scalar M,
real scalar N,
real matrix gmat,
real scalar rstype,
real scalar maxiter,
real scalar tol )
{
///nb=rows(data)-M-N
X=data[1::M,.]
Y=data[(M+1)::(M+N),.]
B=data[(M+N+1)::rows(data),.]
Xref=dataref[1::M,.]
Yref=dataref[(M+1)::(M+N),.]
Bref=dataref[(M+N+1)::rows(data),.]
gX=gmat[1::M,.]
gY=gmat[(M+1)::(M+N),.]
gB=gmat[(M+N+1)::rows(data),.]
class LinearProgram scalar q
q = LinearProgram()
c=(1,J(1,cols(dataref),0))
lowerbd=.,J(1,length(c)-1,0)
upperbd=J(1,length(c),.)
q.setCoefficients(c)
q.setBounds(lowerbd, upperbd)
if(maxiter!=-1){
q.setMaxiter(maxiter)
}
if (tol!=-1){
q.setTol(tol)
}
beta=J(cols(data),1,.)
for(i=1;i<=cols(data);i++){
Aie1=-gX[.,i],Xref
bie1=X[.,i]
Aie2=gY[.,i],-Yref
bie2=-Y[.,i]
Aec=-gB[.,i],Bref
bec=B[.,i]
if(rstype==0){
q.setEquality(Aec,bec)
}
else{
lsum=0,J(1,N,1)
q.setEquality(Aec \ lsum, bec \ 1)
}
q.setInequality((Aie1 \ Aie2 ), (bie1 \ bie2))
beta[i,1]=q.optimize()
}
return(beta)
}
real matrix function calnddf(real matrix data,
real matrix dataref,
real scalar M,
real scalar N,
real rowvector wmat,
real matrix gmat,
real scalar rstype,
real scalar maxiter,
real scalar tol )
{
///nb=rows(data)-M-N
X=data[1::M,.]
Y=data[(M+1)::(M+N),.]
B=data[(M+N+1)::rows(data),.]
Xref=dataref[1::M,.]
Yref=dataref[(M+1)::(M+N),.]
Bref=dataref[(M+N+1)::rows(data),.]
gX=gmat[1::M,.]
gY=gmat[(M+1)::(M+N),.]
gB=gmat[(M+N+1)::rows(data),.]
class LinearProgram scalar q
q = LinearProgram()
beta=J(cols(data),rows(data)+1,.)
c=(wmat,J(1,cols(dataref),0))
lowerbd=J(1,length(c),0)
upperbd=J(1,length(c),.)
q.setCoefficients(c)
q.setBounds(lowerbd, upperbd)
if(maxiter!=-1){
q.setMaxiter(maxiter)
}
if (tol!=-1){
q.setTol(tol)
}
for(i=1;i<=cols(data);i++){
Aie1=diag(-gX[.,i]),J(M,rows(data)-M,0),Xref
bie1=X[.,i]
Aie2=J(N,M,0),diag(gY[.,i]),J(N,rows(data)-M-N,0),-Yref
bie2=-Y[.,i]
Aec=J(rows(data)-M-N,M+N,0),diag(-gB[.,i]),Bref
bec=B[.,i]
q.setInequality((Aie1 \ Aie2 ), (bie1 \ bie2))
if(rstype==0){
q.setEquality(Aec,bec)
}
else{
lsum=J(1,length(wmat),0),J(1,cols(dataref),1)
q.setEquality(Aec \ lsum, bec \ 1)
}
b0=q.optimize()
if (q.converged()==1){
bslk=q.parameters()
beta[i,]=b0,bslk[1..rows(data)]
}
else{
beta[i,]=J(1,1+rows(data),.)
}
}
return(beta)
}
real matrix function nddfcontemporaneous( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),Mc-1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:==t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
return(D0)
}
void function nddfcontemporaneousbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots, ///
string scalar Dv)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,Dv)
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:==t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1) // require sorting data by id time
datastar=dataref[idx,.]
Dbooti=nddfcontemporaneous(data0,datastar,k1,k2,wmat,gmat,band,rstype,maxiter,tol)
Dboot[.,i]=Dbooti[.,1]
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],t1:==t0uniq[j]))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub)
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
//st_view(g1=.,.,"Dv")
//g1[.,.]=D0
//st_view(g2=.,.,"Dv_bc")
//g2[.,.]=Dbc
//st_view(g3=.,.,"Dv_lower")
//g3[.,.]=LL
//st_view(g4=.,.,"Dv_upper")
//g4[.,.]=UU
//st_view(g5=.,.,"bootvar")
//g5[.,.]=vari
}
real matrix function nddfbiennial( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),Mc-1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],(t1:>=t0uniq[j]):*(t1:<=t0uniq[j]+1))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
return(D0)
}
real matrix function nddfbiennialbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots, ///
string scalar Dv)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,Dv)
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],(t1:>=t0uniq[j]):* (t1:<=t0uniq[j]+1))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
Dbooti=nddfbiennial(data0,datastar,k1,k2,wmat,gmat,band,rstype,maxiter,tol)
Dboot[.,i]=Dbooti[.,1]
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],(t1:>=t0uniq[j]):* (t1:<=t0uniq[j]+1)))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub)
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
real matrix function nddfsequential( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),Mc-1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:<=t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
return(D0)
}
real matrix function nddfsequentialbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots, ///
string scalar Dv)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,Dv)
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:<=t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
mata:Dbooti=nddfsequential(data0,datastar,k1,k2,wmat,gmat,band,rstype,maxiter,tol)
Dboot[.,i]=Dbooti[.,1]
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],t1:<=t0uniq[j]))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub*sum(t1:<=t0uniq[j]))
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
real matrix function nddfglobal( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
XYB = data0[.,3..cols(data0)]
XYBref = dataref[.,3..cols(dataref)]
D0 = calnddf(XYB',XYBref',k1,k2,wmat,gmat',rstype,maxiter,tol)
//D0
return(D0)
}
real matrix function nddfglobalbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots, ///
string scalar Dv)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
//t1 = dataref[.,2]
//Mc=cols(data0)
t0uniq=uniqrows(t0)
Kr=rows(dataref)
//D0=J(rows(data0),1,.)
st_view(D0=.,.,Dv)
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
XYB = data0[.,3..cols(data0)]
XYBref = dataref[.,3..cols(dataref)]
D0[.,.] = calnddf(XYB',XYBref',k1,k2,wmat,gmat',rstype,maxiter,tol)
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
msubmat=J(1,brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
//rows(datastar)
msubmat[1,i]=rows(datastar)
Dbooti=nddfglobal(data0,datastar,k1,k2,wmat,gmat,band,rstype,maxiter,tol)
Dboot[.,i]=Dbooti[.,1]
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msubmat[1,j])
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
// bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
real matrix function nddfwindow( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real rowvector wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),Mc-1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:<=(t0uniq[j]+band):*(t1:>=t0uniq[j]-band))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
return(D0)
}
real matrix function nddfwindowbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real scalar wmat, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots, ///
string scalar Dv)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,Dv)
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],(t1:<=t0uniq[j]+band):*(t1:>=t0uniq[j]-band))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,.]= calnddf(XYB',XYBref',k1,k2,wmat,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
Dbooti=nddfwindow(data0,datastar,k1,k2,wmat,gmat,band,rstype,maxiter,tol)
Dboot[.,i]=Dbooti[.,1]
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],(t1:<=t0uniq[j]+band):*(t1:>=t0uniq[j]-band)))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub*sum((t1:<=t0uniq[j]+band):* (t1:>=t0uniq[j]-band)))
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
//////////////////////////////////////
void function _ddf( string scalar d, ///
real scalar k1, ///
real scalar k2, ///
string scalar touse1, ///
string scalar touse2, ///
string scalar gname, ///
string scalar bname, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
data=st_data(.,d,touse1)
data=data'
M=rows(data)
g=st_data(.,gname,touse1)
g=g'
dataref=st_data(.,d,touse2)
dataref=dataref'
Xref=dataref[1..k1,.]
Yref=dataref[k1+1..(k1+k2),.]
Bref=dataref[(k1+k2+1)..M,.]
X=data[1..k1,.]
Y=data[k1+1..(k1+k2),.]
B=data[(k1+k2+1)..M,.]
theta=J(cols(data),1,.)
for(j=1;j<=cols(X);j++){
theta[j]=_ddf2(X[.,j],Y[.,j],B[.,j],Xref,Yref,Bref,g[.,j],rstype,maxiter,tol)
}
st_view(gen=.,.,bname,touse1)
gen[.,.]=theta
}
real matrix function ddfcontemporaneous( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:==t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
return(D0)
}
void function ddfcontemporaneousbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,"Dv")
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:==t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
Dboot[.,i]=ddfcontemporaneous(data0,datastar,k1,k2,gmat,band,rstype,maxiter,tol)
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],t1:==t0uniq[j]))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub)
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
//st_view(g1=.,.,"Dv")
//g1[.,.]=D0
//st_view(g2=.,.,"Dv_bc")
//g2[.,.]=Dbc
//st_view(g3=.,.,"Dv_lower")
//g3[.,.]=LL
//st_view(g4=.,.,"Dv_upper")
//g4[.,.]=UU
//st_view(g5=.,.,"bootvar")
//g5[.,.]=vari
}
real matrix function ddfbiennial( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],(t1:>=t0uniq[j]):*(t1:<=t0uniq[j]+1))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
return(D0)
}
real matrix function ddfbiennialbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,"Dv")
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],(t1:>=t0uniq[j]):* (t1:<=t0uniq[j]+1))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
Dboot[.,i]=ddfbiennial(data0,datastar,k1,k2,gmat,band,rstype,maxiter,tol)
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],(t1:>=t0uniq[j]):* (t1:<=t0uniq[j]+1)))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub)
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
real matrix function ddfsequential( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:<=t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
return(D0)
}
real matrix function ddfsequentialbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,"Dv")
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:<=t0uniq[j])
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
Dboot[.,i]=ddfsequential(data0,datastar,k1,k2,gmat,band,rstype,maxiter,tol)
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],t1:<=t0uniq[j]))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub*sum(t1:<=t0uniq[j]))
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
real matrix function ddfglobal( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
XYB = data0[.,3..cols(data0)]
XYBref = dataref[.,3..cols(dataref)]
D0 = calddf(XYB',XYBref',k1,k2,gmat',rstype,maxiter,tol)
//D0
return(D0)
}
real matrix function ddfglobalbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
//t1 = dataref[.,2]
//Mc=cols(data0)
t0uniq=uniqrows(t0)
Kr=rows(dataref)
//D0=J(rows(data0),1,.)
st_view(D0=.,.,"Dv")
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
XYB = data0[.,3..cols(data0)]
XYBref = dataref[.,3..cols(dataref)]
D0[.,1] = calddf(XYB',XYBref',k1,k2,gmat',rstype,maxiter,tol)
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
msubmat=J(1,brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
//rows(datastar)
msubmat[1,i]=rows(datastar)
Dboot[.,i]=ddfglobal(data0,datastar,k1,k2,gmat,band,rstype,maxiter,tol)
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msubmat[1,j])
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
// bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
real matrix function ddfwindow( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
D0=J(rows(data0),1,.)
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],t1:<=(t0uniq[j]+band):*(t1:>=t0uniq[j]-band))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
return(D0)
}
real matrix function ddfwindowbt( real matrix data0, ///
real matrix dataref, ///
real scalar k1, ///
real scalar k2, ///
real matrix gmat, ///
real scalar band, ///
real scalar gamma, ///
real scalar level, ///
real scalar brep,
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol, ///
real scalar nodots)
{
// sort by id t
struct bootdea scalar cires
msub=ceil(length(uniqrows(dataref[.,1]))^gamma)
t0 = data0[.,2]
t1 = dataref[.,2]
Mc=cols(data0)
t0uniq=uniqrows(t0)
//Kr=length(uniqrows(dataref[.,1]))
//D0=J(rows(data0),1,.)
st_view(D0=.,.,"Dv")
//g1[.,.]=D0
st_view(Dbc=.,.,"Dv_bc")
//g2[.,.]=Dbc
st_view(LL=.,.,"Dv_lower")
//g3[.,.]=LL
st_view(UU=.,.,"Dv_upper")
//g4[.,.]=UU
st_view(vari=.,.,"bootvar")
//g5[.,.]=vari
for(j=1;j<=length(t0uniq);j++){
XYB = select(data0[.,3..Mc],t0:==t0uniq[j])
XYBref = select(dataref[.,3..Mc],(t1:<=t0uniq[j]+band) + (t1:>=t0uniq[j]-band))
gmat2 = select(gmat,t0:==t0uniq[j])
pos=select(1::rows(data0),t0:==t0uniq[j])
D0[pos,1]= calddf(XYB',XYBref',k1,k2,gmat2',rstype,maxiter,tol)
}
id = dataref[.,1]
mm_panels(id,idinfo=.)
Dboot=J(rows(data0),brep,.)
for(i=1;i<=brep;i++){
if(nodots!=1){
displaydots(i)
}
idx=mm_sample(msub,.,idinfo,.,1)
datastar=dataref[idx,.]
Dboot[.,i]=ddfwindow(data0,datastar,k1,k2,gmat,band,rstype,maxiter,tol)
}
for(j=1;j<=length(t0uniq);j++){
pos=select(1::rows(data0),t0:==t0uniq[j])
Kr=length(select(dataref[.,1],t1:<=t0uniq[j]))
cires=biasandciddf(D0[pos,1],Dboot[pos,.], k1, cols(data0)-k1-2, length(pos), Kr, level,0,msub*sum((t1:<=t0uniq[j]+band):* (t1:>=t0uniq[j]-band)))
Dbc[pos,1]=cires.tebc
LL[pos,1] =cires.LL
UU[pos,1] =cires.UU
//bias[pos,1]=cires.bias
vari[pos,1]=cires.vari
}
}
function biasandciddf( real colvector te,real matrix teboot, ///
real scalar M, real scalar N, real scalar K, real scalar Kr, ///
real scalar level,real scalar smoothed, real scalar msub)
{
if(smoothed){
con1=1
con2=1
}
else{
con1=msub^(2/(M+N+1))
con2=Kr^(-2/(M+N+1))
}
con3=con1*con2
bias=J(K,1,.)
vari=J(K,1,.)
BovV=J(K,1,.)
tebc=J(K,1,.)
UU=J(K,1,.)
LL=J(K,1,.)
for(i=1;i<=K;i++){
TEi=teboot[i,.]
TEi=select(TEi,TEi:!=.)
if(length(TEi)<100){
bias[i]=.
vari[i]=.
BovV[i]=.
tebc[i]=.
LL[i]=.
UU[i]=.
}
else{
bias[i]=con3*(mean(TEi')-te[i])
vari[i]=variance(TEi')
BovV[i]=(bias[i])^2 / vari[i] * 3
tebc[i] = te[i] - bias[i]
teOverTEhat = (TEi':- te[i] ) * con1
quans =mm_quantile(teOverTEhat,1, ((0.5-level/200)\(0.5+level/200) ))
//quans* con2
LL[i] = te[i] - quans[2] * con2
UU[i] = te[i] - quans[1] * con2
//quans =mm_quantile(TEi',1, ((0.5-level/200)\(0.5+level/200) ))
//LL[i] = quans[1]
//UU[i] = quans[2]
}
}
struct bootdea scalar res
res.tebc = tebc
res.vari = vari
res.BovV = BovV
res.bias = bias
res.LL = LL
res.UU = UU
return(res)
}
real scalar function _ddf2( real colvector X, ///
real colvector Y, ///
real colvector B, ///
real matrix Xref, ///
real matrix Yref, ///
real matrix Bref, ///
real colvector g, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
k1=length(X)
k2=length(Y)
M=length(B)+k1+k2
gX=g[1..k1,1]
gY=g[k1+1..(k1+k2),1]
gB=g[(k1+k2+1)..M,1]
N=cols(Xref)
class LinearProgram scalar q
q = LinearProgram()
c=(1,J(1,N,0))
lowerbd=.,J(1,length(c)-1,0)
upperbd=J(1,length(c),.)
q.setCoefficients(c)
q.setBounds(lowerbd, upperbd)
if(maxiter!=-1){
q.setMaxiter(maxiter)
}
if (tol!=-1){
q.setTol(tol)
}
Aie1=-gX,Xref
bie1=X
Aie2=gY,-Yref
bie2=-Y
Aec=-gB,Bref
bec=B
if(rstype==0){
q.setEquality(Aec,bec)
}
else{
lsum=0,J(1,N,1)
q.setEquality(Aec \ lsum, bec \ 1)
}
q.setInequality((Aie1 \ Aie2 ), (bie1 \ bie2))
beta=q.optimize()
return(beta)
}
void function _nddf( string scalar d, ///
real scalar nx, ///
real scalar ny, ///
string scalar touse1, ///
string scalar touse2, ///
string scalar gname, ///
string scalar wname, ///
string scalar bname, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
wmat=st_matrix(wname)
data=st_data(.,d,touse1)
data=data'
M=rows(data)
g=st_data(.,gname,touse1)
g=g'
dataref=st_data(.,d,touse2)
dataref=dataref'
Xref=dataref[1..nx,.]
Yref=dataref[nx+1..(nx+ny),.]
Bref=dataref[(nx+ny+1)..M,.]
X=data[1..nx,.]
Y=data[nx+1..(nx+ny),.]
B=data[(nx+ny+1)..M,.]
theta=J(cols(data),1,.)
for(j=1;j<=cols(data);j++){
theta[j]=_nddf2(X[.,j],Y[.,j],B[.,j],Xref,Yref,Bref,g[.,j],wmat,rstype,maxiter,tol)
}
st_view(gen=.,.,bname,touse1)
gen[.,.]=theta
}
real function _nddf2( real colvector X, ///
real colvector Y, ///
real colvector B, ///
real matrix Xref, ///
real matrix Yref, ///
real matrix Bref, ///
real colvector g, ////
real rowvector wmat, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
nx=length(X)
ny=length(Y)
nb=length(B)
M=nb+nx+ny
gX=g[1..nx,1]
gY=g[nx+1..(nx+ny),1]
gB=g[(nx+ny+1)..M,1]
N=cols(Xref)
class LinearProgram scalar q
q = LinearProgram()
c=(wmat,J(1,N,0))
lowerbd=J(1,length(c),0)
upperbd=J(1,length(c),.)
q.setCoefficients(c)
q.setBounds(lowerbd, upperbd)
if(maxiter!=-1){
q.setMaxiter(maxiter)
}
if (tol!=-1){
q.setTol(tol)
}
Aie1=diag(-gX),J(nx,ny+nb,0),Xref
bie1=X
Aie2=J(ny,nx,0),diag(gY),J(ny,nb,0),-Yref
bie2=-Y
Aec=J(nb,nx+ny,0),diag(-gB),Bref
bec=B
lsum=J(1,length(wmat),0),J(1,N,1)
if(rstype==0){
q.setEquality(Aec,bec)
}
else{
lsum=J(1,M,0),J(1,N,1)
q.setEquality(Aec \ lsum, bec \ 1)
}
q.setInequality((Aie1 \ Aie2 ), (bie1 \ bie2))
beta=q.optimize()
//q.getInequality()
return(beta)
}
void function _tenddf( string scalar d, ///
real scalar nx, ///
real scalar ny, ///
string scalar touse1, ///
string scalar touse2, ///
string scalar gname, ///
string scalar wname, ///
string scalar bname, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
wmat=st_matrix(wname)
data=st_data(.,d,touse1)
data=data'
M=rows(data)
g=st_data(.,gname,touse1)
g=g'
dataref=st_data(.,d,touse2)
dataref=dataref'
Xref=dataref[1..nx,.]
Yref=dataref[nx+1..(nx+ny),.]
Bref=dataref[(nx+ny+1)..M,.]
X=data[1..nx,.]
Y=data[nx+1..(nx+ny),.]
B=data[(nx+ny+1)..M,.]
theta=J(cols(data),rows(data)+1,.)
for(j=1;j<=cols(data);j++){
theta[j,.]=_tenddf2(X[.,j],Y[.,j],B[.,j],Xref,Yref,Bref,g[.,j],wmat,rstype,maxiter,tol)
//q.getInequality()
}
st_view(gen=.,.,bname,touse1)
gen[.,.]=theta
}
real function _tenddf2( real colvector X, ///
real colvector Y, ///
real colvector B, ///
real matrix Xref, ///
real matrix Yref, ///
real matrix Bref, ///
real colvector g, ////
real rowvector wmat, ///
real scalar rstype, ///
real scalar maxiter, ///
real scalar tol)
{
nx=length(X)
ny=length(Y)
nb=length(B)
M=nb+nx+ny
gX=g[1..nx,1]
gY=g[nx+1..(nx+ny),1]
gB=g[(nx+ny+1)..M,1]
N=cols(Xref)
class LinearProgram scalar q
q = LinearProgram()
c=(wmat,J(1,N,0))
lowerbd=J(1,length(c),0)
upperbd=J(1,length(c),.)
q.setCoefficients(c)
q.setBounds(lowerbd, upperbd)
if(maxiter!=-1){
q.setMaxiter(maxiter)
}
if (tol!=-1){
q.setTol(tol)
}
Aie1=diag(-gX),J(nx,ny+nb,0),Xref
bie1=X
Aie2=J(ny,nx,0),diag(gY),J(ny,nb,0),-Yref
bie2=-Y
Aec=J(nb,nx+ny,0),diag(-gB),Bref
bec=B
lsum=J(1,length(wmat),0),J(1,N,1)
if(rstype==0){
q.setEquality(Aec,bec)
}
else{
lsum=J(1,M,0),J(1,N,1)
q.setEquality(Aec \ lsum, bec \ 1)
}
q.setInequality((Aie1 \ Aie2 ), (bie1 \ bie2))
dval=q.optimize()
if (q.converged()==1){
bslk=q.parameters()
beta=dval,bslk[1..M]
}
else{
beta=J(1,1+M,.)
}
//q.getInequality()
return(beta)
}
end
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kerrydu/gtfpch.git
git@gitee.com:kerrydu/gtfpch.git
kerrydu
gtfpch
gtfpch
master

搜索帮助