Fix Ox implementation

Remove .h from Ox, fix `lex` typo, and add samples for Ox.
This commit is contained in:
Christian Bundy
2014-05-30 15:47:42 -07:00
parent 8cde6d2e8f
commit 72a6186f08
5 changed files with 457 additions and 6 deletions

View File

@@ -1479,10 +1479,9 @@ Org:
Ox: Ox:
type: programming type: programming
lex: oxl lexer: Text only
extensions: extensions:
- .ox - .ox
- .h
- .oxh - .oxh
- .oxo - .oxo
@@ -1796,7 +1795,7 @@ Red:
extensions: extensions:
- .red - .red
- .reds - .reds
Redcode: Redcode:
extensions: extensions:
- .cw - .cw

View File

@@ -382,6 +382,11 @@
"Org": [ "Org": [
".org" ".org"
], ],
"Ox": [
".ox",
".oxh",
".oxo"
],
"Oxygene": [ "Oxygene": [
".oxygene" ".oxygene"
], ],
@@ -712,8 +717,8 @@
".gemrc" ".gemrc"
] ]
}, },
"tokens_total": 609975, "tokens_total": 610981,
"languages_total": 750, "languages_total": 753,
"tokens": { "tokens": {
"ABAP": { "ABAP": {
"*/**": 1, "*/**": 1,
@@ -47560,6 +47565,278 @@
"there": 1, "there": 1,
"it": 1 "it": 1
}, },
"Ox": {
"#include": 2,
"Kapital": 4,
"(": 119,
"L": 2,
"const": 4,
"N": 5,
"entrant": 8,
"exit": 2,
"KP": 14,
")": 119,
"{": 22,
"StateVariable": 1,
";": 91,
"this.entrant": 1,
"this.exit": 1,
"this.KP": 1,
"actual": 2,
"Kbar*vals/": 1,
"-": 31,
"upper": 3,
"log": 2,
".Inf": 2,
"}": 22,
"Transit": 1,
"FeasA": 2,
"decl": 3,
"ent": 5,
"CV": 7,
"stayout": 3,
"[": 25,
"]": 25,
"exit.pos": 1,
"tprob": 5,
"sigu": 2,
"SigU": 2,
"if": 5,
"v": 2,
"&&": 1,
"return": 10,
"<0>": 1,
"ones": 1,
"probn": 2,
"Kbe": 2,
"/sigu": 1,
"Kb0": 2,
"+": 14,
"Kb2": 2,
"*upper": 1,
"/": 1,
"vals": 1,
"tprob.*": 1,
"zeros": 4,
".*stayout": 1,
"FirmEntry": 6,
"Run": 1,
"Initialize": 3,
"GenerateSample": 2,
"BDP": 2,
"BayesianDP": 1,
"Rust": 1,
"Reachable": 2,
"sige": 2,
"new": 19,
"StDeviations": 1,
"<0.3,0.3>": 1,
"LaggedAction": 1,
"d": 2,
"array": 1,
"Kparams": 1,
"Positive": 4,
"Free": 1,
"Kb1": 1,
"Determined": 1,
"EndogenousStates": 1,
"K": 3,
"KN": 1,
"SetDelta": 1,
"Probability": 1,
"kcoef": 3,
"ecost": 3,
"Negative": 1,
"CreateSpaces": 1,
"Volume": 3,
"LOUD": 1,
"EM": 4,
"ValueIteration": 1,
"//": 17,
"Solve": 1,
"data": 4,
"DataSet": 1,
"Simulate": 1,
"DataN": 1,
"DataT": 1,
"FALSE": 1,
"Print": 1,
"ImaiJainChing": 1,
"delta": 1,
"*CV": 2,
"Utility": 1,
"u": 2,
"ent*CV": 1,
"*AV": 1,
"|": 1,
"ParallelObjective": 1,
"obj": 18,
"DONOTUSECLIENT": 2,
"isclass": 1,
"obj.p2p": 2,
"oxwarning": 1,
"obj.L": 1,
"P2P": 2,
"ObjClient": 4,
"ObjServer": 7,
"this.obj": 2,
"Execute": 4,
"basetag": 2,
"STOP_TAG": 1,
"iml": 1,
"obj.NvfuncTerms": 2,
"Nparams": 6,
"obj.nstruct": 2,
"Loop": 2,
"nxtmsgsz": 2,
"//free": 1,
"param": 1,
"length": 1,
"is": 1,
"no": 2,
"greater": 1,
"than": 1,
"QUIET": 2,
"println": 2,
"ID": 2,
"Server": 1,
"Recv": 1,
"ANY_TAG": 1,
"//receive": 1,
"the": 1,
"ending": 1,
"parameter": 1,
"vector": 1,
"Encode": 3,
"Buffer": 8,
"//encode": 1,
"it.": 1,
"Decode": 1,
"obj.nfree": 1,
"obj.cur.V": 1,
"vfunc": 2,
"CstrServer": 3,
"SepServer": 3,
"Lagrangian": 1,
"rows": 1,
"obj.cur": 1,
"Vec": 1,
"obj.Kvar.v": 1,
"imod": 1,
"Tag": 1,
"obj.K": 1,
"TRUE": 1,
"obj.Kvar": 1,
"PDF": 1,
"*": 5,
"nldge": 1,
"ParticleLogLikeli": 1,
"it": 5,
"ip": 1,
"mss": 3,
"mbas": 1,
"ms": 8,
"my": 4,
"mx": 7,
"vw": 7,
"vwi": 4,
"dws": 3,
"mhi": 3,
"mhdet": 2,
"loglikeli": 4,
"mData": 4,
"vxm": 1,
"vxs": 1,
"mxm": 1,
"<": 4,
"mxsu": 1,
"mxsl": 1,
"time": 2,
"timeall": 1,
"timeran": 1,
"timelik": 1,
"timefun": 1,
"timeint": 1,
"timeres": 1,
"GetData": 1,
"m_asY": 1,
"sqrt": 1,
"*M_PI": 1,
"m_cY": 1,
"determinant": 2,
"m_mMSbE.": 2,
"covariance": 2,
"invert": 2,
"of": 2,
"measurement": 1,
"shocks": 1,
"m_vSss": 1,
"m_cPar": 4,
"m_cS": 1,
"start": 1,
"particles": 2,
"m_vXss": 1,
"m_cX": 1,
"steady": 1,
"state": 3,
"and": 1,
"policy": 2,
"init": 1,
"likelihood": 1,
"//timeall": 1,
"timer": 3,
"for": 2,
"sizer": 1,
"rann": 1,
"m_cSS": 1,
"m_mSSbE": 1,
"noise": 1,
"fg": 1,
"&": 2,
"transition": 1,
"prior": 1,
"as": 1,
"proposal": 1,
"m_oApprox.FastInterpolate": 1,
"interpolate": 1,
"fy": 1,
"m_cMS": 1,
"evaluate": 1,
"importance": 1,
"weights": 2,
"observation": 1,
"error": 1,
"exp": 2,
"outer": 1,
"/mhdet": 2,
"sumr": 1,
"my*mhi": 1,
".*my": 1,
".": 3,
".NaN": 1,
"can": 1,
"happen": 1,
"extrem": 1,
"sumc": 1,
"or": 1,
"extremely": 1,
"wrong": 1,
"parameters": 1,
"dws/m_cPar": 1,
"loglikelihood": 1,
"contribution": 1,
"//timelik": 1,
"/100": 1,
"//time": 1,
"resample": 1,
"vw/dws": 1,
"selection": 1,
"step": 1,
"in": 1,
"c": 1,
"on": 1,
"normalized": 1
},
"Oxygene": { "Oxygene": {
"<Project>": 1, "<Project>": 1,
"DefaultTargets=": 1, "DefaultTargets=": 1,
@@ -65923,6 +66200,7 @@
"OpenCL": 144, "OpenCL": 144,
"OpenEdge ABL": 762, "OpenEdge ABL": 762,
"Org": 358, "Org": 358,
"Ox": 1006,
"Oxygene": 157, "Oxygene": 157,
"Parrot Assembly": 6, "Parrot Assembly": 6,
"Parrot Internal Representation": 5, "Parrot Internal Representation": 5,
@@ -66105,6 +66383,7 @@
"OpenCL": 2, "OpenCL": 2,
"OpenEdge ABL": 5, "OpenEdge ABL": 5,
"Org": 1, "Org": 1,
"Ox": 3,
"Oxygene": 1, "Oxygene": 1,
"Parrot Assembly": 1, "Parrot Assembly": 1,
"Parrot Internal Representation": 1, "Parrot Internal Representation": 1,
@@ -66175,5 +66454,5 @@
"YAML": 2, "YAML": 2,
"Zephir": 2 "Zephir": 2
}, },
"md5": "ef1795e3585ee02f479767c6a071eb2f" "md5": "a80808140f91c6a5539bdca53d5e114a"
} }

View File

@@ -0,0 +1,72 @@
/** Replicate Imai, Jain and Ching Econometrica 2009 (incomplete).
**/
#include "IJCEmet2009.h"
Kapital::Kapital(L,const N,const entrant,const exit,const KP){
StateVariable(L,N);
this.entrant = entrant;
this.exit = exit;
this.KP = KP;
actual = Kbar*vals/(N-1);
upper = log(actual~.Inf);
}
Kapital::Transit(FeasA) {
decl ent =CV(entrant), stayout = FeasA[][exit.pos], tprob, sigu = CV(KP[SigU]);
if (!v && !ent) return { <0>, ones(stayout) };
tprob = ent ? probn( (upper-CV(KP[Kbe]))/sigu )
: probn( (upper-(CV(KP[Kb0])+CV(KP[Kb2])*upper[v])) / sigu );
tprob = tprob[1:] - tprob[:N-1];
return { vals, tprob.*(1-stayout)+(1.0~zeros(1,N-1)).*stayout };
}
FirmEntry::Run() {
Initialize();
GenerateSample();
BDP->BayesianDP();
}
FirmEntry::Initialize() {
Rust::Initialize(Reachable,0);
sige = new StDeviations("sige",<0.3,0.3>,0);
entrant = new LaggedAction("entrant",d);
KP = new array[Kparams];
KP[Kbe] = new Positive("be",0.5);
KP[Kb0] = new Free("b0",0.0);
KP[Kb1] = new Determined("b1",0.0);
KP[Kb2] = new Positive("b2",0.4);
KP[SigU] = new Positive("sigu",0.4);
EndogenousStates(K = new Kapital("K",KN,entrant,d,KP),entrant);
SetDelta(new Probability("delta",0.85));
kcoef = new Positive("kcoef",0.1);
ecost = new Negative("ec",-0.4);
CreateSpaces();
}
FirmEntry::GenerateSample() {
Volume = LOUD;
EM = new ValueIteration(0);
// EM -> Solve(0,0);
data = new DataSet(0,EM);
data->Simulate(DataN,DataT,0,FALSE);
data->Print("firmentry.xls");
BDP = new ImaiJainChing("FMH",data,EM,ecost,sige,kcoef,KP,delta);
}
/** Capital stock can be positive only for incumbents.
**/
FirmEntry::Reachable() { return CV(entrant)*CV(K) ? 0 : new FirmEntry() ; }
/** The one period return.
<DD>
<pre>U = </pre>
</DD>
**/
FirmEntry::Utility() {
decl ent = CV(entrant),
u =
ent*CV(ecost)+(1-ent)*CV(kcoef)*AV(K)
| 0.0;
return u;
}

View File

@@ -0,0 +1,63 @@
/** Client and Server classes for parallel optimization using CFMPI.**/
#include "ParallelObjective.h"
/** Set up MPI Client-Server support for objective optimization.
@param obj `Objective' to parallelize
@param DONOTUSECLIENT TRUE (default): client node does no object evaluation<br>FALSE after putting servers to work Client node does one evaluation.
**/
ParallelObjective(obj,DONOTUSECLIENT) {
if (isclass(obj.p2p)) {oxwarning("P2P object already exists for "+obj.L+". Nothing changed"); return;}
obj.p2p = new P2P(DONOTUSECLIENT,new ObjClient(obj),new ObjServer(obj));
}
ObjClient::ObjClient(obj) { this.obj = obj; }
ObjClient::Execute() { }
ObjServer::ObjServer(obj) {
this.obj = obj;
basetag = P2P::STOP_TAG+1;
iml = obj.NvfuncTerms;
Nparams = obj.nstruct;
}
/** Wait on the objective client.
**/
ObjServer::Loop(nxtmsgsz) {
Nparams = nxtmsgsz; //free param length is no greater than Nparams
if (Volume>QUIET) println("ObjServer server ",ID," Nparams ",Nparams);
Server::Loop(Nparams);
Recv(ANY_TAG); //receive the ending parameter vector
obj->Encode(Buffer[:Nparams-1]); //encode it.
}
/** Do the objective evaluation.
Receive structural parameter vector and `Objective::Encode`() it.
Call `Objective::vfunc`().
@return Nparams (max. length of next expected message);
**/
ObjServer::Execute() {
obj->Decode(Buffer[:obj.nfree-1]);
Buffer = obj.cur.V[] = obj->vfunc();
if (Volume>QUIET) println("Server Executive: ",ID," vfunc[0]= ",Buffer[0]);
return obj.nstruct;
}
CstrServer::CstrServer(obj) { ObjServer(obj); }
SepServer::SepServer(obj) { ObjServer(obj); }
CstrServer::Execute() {
obj->Encode(Buffer);
obj->Lagrangian(0);
return rows(Buffer = obj.cur->Vec());
}
/** Separable objective evaluations.
**/
SepServer::Execute() {
obj.Kvar.v = imod(Tag-basetag,obj.K);
obj->Encode(Buffer,TRUE);
Buffer = obj.Kvar->PDF() * obj->vfunc();
return obj.NvfuncTerms;
}

38
samples/Ox/particle.oxo Normal file
View File

@@ -0,0 +1,38 @@
nldge::ParticleLogLikeli()
{ decl it, ip,
mss, mbas, ms, my, mx, vw, vwi, dws,
mhi, mhdet, loglikeli, mData,
vxm, vxs, mxm=<>, mxsu=<>, mxsl=<>,
time, timeall, timeran=0, timelik=0, timefun=0, timeint=0, timeres=0;
mData = GetData(m_asY);
mhdet = sqrt((2*M_PI)^m_cY * determinant(m_mMSbE.^2)); // covariance determinant
mhi = invert(m_mMSbE.^2); // invert covariance of measurement shocks
ms = m_vSss + zeros(m_cPar, m_cS); // start particles
mx = m_vXss + zeros(m_cPar, m_cX); // steady state of state and policy
loglikeli = 0; // init likelihood
//timeall=timer();
for(it = 0; it < sizer(mData); it++)
{
mss = rann(m_cPar, m_cSS) * m_mSSbE; // state noise
fg(&ms, ms, mx, mss); // transition prior as proposal
mx = m_oApprox.FastInterpolate(ms); // interpolate
fy(&my, ms, mx, zeros(m_cPar, m_cMS)); // evaluate importance weights
my -= mData[it][]; // observation error
vw = exp(-0.5 * outer(my,mhi,'d')' )/mhdet; // vw = exp(-0.5 * sumr(my*mhi .*my ) )/mhdet;
vw = vw .== .NaN .? 0 .: vw; // no policy can happen for extrem particles
dws = sumc(vw);
if(dws==0) return -.Inf; // or extremely wrong parameters
loglikeli += log(dws/m_cPar) ; // loglikelihood contribution
//timelik += (timer()-time)/100;
//time=timer();
vwi = resample(vw/dws)-1; // selection step in c++
ms = ms[vwi][]; // on normalized weights
mx = mx[vwi][];
}
return loglikeli;
}