Econometric Model Implementation

Overview


Given a set of simultaneous equations of an econometric model, an analyst can solve the models equations using the simultaneous equations library.

Map Object


The simultaneous library will create an object, referred to here as map, that provides functions for each variable in the model such that the function returns the currently estimated value of that variable. For example, retrieving the model value for consumption, {% C %} is calcualted as


let consumption = map.C();
					

Specify the Variables


Variables that are computed through a structural equation, are simply provided by giving them a name, and a guess for an initial value. The initial value will be changed by the algorithm, so the value provided is typically irrelevant, other than a value that is close to the final value will help the algorithm converge faster.


{
  name:'C',
  value:200
},
					

Specifying identities (as well as exogenous variables) requires providing a function which computes the value of the variable. For and exogenous variable, you just provide a function that returns a constant value. For an identity, you provide a function that computes the variable from the other variables in the provided map object.


let variables = [
  {name:'Y', identity:map=>{
    return map.C()+map.I()+map.G()
  }},  
  {
    name:'G',
    identity:()=>1000
  },
];
					

For the sample model, the following provides a complete listing of the variables.


let variables = [
    {name:'Y', identity:map=>{
        return map.C()+map.I()+map.G()
    }},
    {
        name:'C',
        value:200
    },
    {
        name:'G',
        identity:()=>1000
    },
    {
        name:'I',
        value:100
    },
    {
        name:'Ylast', 
        identity:()=>10000
    }, 
    {
        name:'R',
        identity:()=>0.1
    }
];
					

Specify the Losses


The algorithm proceeds by utilizing the gradient descent algorithm to adjust the values of the variables from structural equations until the model is fit. This is done by providing a loss or error function that specifies how much error is present in the current value.

The following uses mean squared error loss to compute an error for each structural equation.


let losses = [
    map=>{
        let loss = map.C() - (220 + 0.6*map.Y());
        return loss*loss
    },
    map=>{
        let loss = map.I() - (150 - 40*map.R()+0.2*map.Ylast())
        return loss*loss
    }
];
					

Fitting


The final step is to run the algorithm to fit the variables. This is done by first creating the map object (see above) and then running the gradient descent algorithm on the map object.


let map = sm.mapVariables(variables);

sm.gradientDescent(map, losses);
					

Full Script





(async ()=>{

let sm = await import('/lib/simultaneous/v1.0.0/simultaneous.mjs')	

let variables = [
    {name:'Y', identity:map=>{
        return map.C()+map.I()+map.G()
    }},
    {
        name:'C',
        value:200
    },
    {
        name:'G',
        identity:()=>1000
    },
    {
        name:'I',
        value:100
    },
    {
        name:'Ylast', 
        identity:()=>10000
    }, 
    {
        name:'R',
        identity:()=>0.1
    }
];

let losses = [
    map=>{
        let loss = map.C() - (220 + 0.6*map.Y());
        return loss*loss
    },
    map=>{
        let loss = map.I() - (150 - 40*map.R()+0.2*map.Ylast())
        return loss*loss
    }
];

let map = sm.mapVariables(variables);

sm.gradientDescent(map, losses);
$console.log('C is '+map.C());
$console.log('I is '+map.I());
$console.log('G is '+map.G());
$console.log('Y is '+map.Y());

})();
					
Try it!