经管之家:Do the best economic and management education!
Tht best econometrics and Computational economy
for example,DSGE Statial econometrics
再次测试使用Markdown 制作论坛文章但是还没掌握好LATEX,其中彩色代码部分已经书写进去,就差LATEX的公式书写正确。这是第二次使用这个新功能,明年在人大经济论坛公开免费讲课DSGE将会使用这个Mardown编辑器生成脚好看的文件。如果可以可以考虑使用英文,在google 或者 baidu 能搜到到我的文章。
我以后会考虑原创DSGE 动态随机一般均衡模型建模吧,包括写自己的程序。今天上传的是李向阳老师的代码。其中插入的LATEX 公式又不是李向阳老师的程序,是Dynare 研究团队里的论文的程序生成的 .tex 文件。原本在我的电脑里的 Texworks 是可以生成公式的。但是插入这个页面是无法显示正确的公式,大概明年二月可以完全掌握使用方法。
选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片.
def get_abs(x):
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x >= 0:
return x
else:
return -x
library(rugarch)
spec=ugarchspec() #spection garch model
%% Simple SPBC Model // Read Me First
%
% This tutorial is a collection of example files for a simple sticky-price
% business cycle model (SPBC). The model file and m-files included in the
% tutorial describe a typical workflow in building and operating a
% small-scale DSGE models for practical policy analysis and forecasting.
%
%% How to Best Run This Tutorial?
%
% Each m-file in this tutorial is split into what is called "code sections"
% in Matlab. A code cell is a shorter block of code performing a specific
% task, separated from other code cells by a double percent sign, `%%`
% (usually with a title and brief introduction added). By default, the
% cells are visually separated from each other by a horizontal rule in the
% Matlab editor.
%
% Instead of running each m-file from the command window, or executing this
% `read_me_first` as a whole, do the following. Open one tutorial m-file in
% the Matlab editor. Arrange the editor window and the command window next
% to each other so that you can see both of them at the same time. Then run
% the m-file cell by cell. This will help you watch closely what exactly
% is going on.
%
% To execute one particular cell, place the cursor in that cell (the
% respective block of code will get highlighted), and select "Run Current
% Section" from a contextual menu (upon a right click on the mouse), or
% pressing a keyboard shortcut (which differ on different systems and
% Matlab versions). To learn more on code sections, search Matlab
% documentation for "code section".
clc;
%% Simple Sticky Price Business Cycle Model File
%
% This is a model file for a simple sticky-price business model. The model
% file describes variables, parameters and equations. Note that the model
% file does not specifies the tasks that will be performed with the model.
% The tasks will be set up in separate m-files, using standard Matlab
% functions and IRIS functions.
%
% You cannot run the model file itself. Instead, load the file into Matlab
% using the function |model| function. See the m-file |read_model| for more
% details.
%
% The IRIS model files can be syntax-highlighted in the Matlab editor; this
% makes the files easier to read. To this end, associate the model file
% extension(s) (which can be anything) with the editor. Open the menu File
% - Preferences, and click on the Editor/Debuger - Language tab. Use the
% Add button in the File extensions section to add new extensions, e.g.
% 'model'. Then restart the editor, and that's it.
edit simple_SPBC.model;
%% Read and Solve Model
%
% Create a model ob ject by loading the model file `Simple_SPBC.model`,
% assign parameters to the model ob ject, find its steady state, and compute
% the first-order accurate solution. The model ob ject is then saved to a
% mat file, and ready for further experiments.
% edit read_model.m;
read_model;
%% Get Information About Model ob ject
%
% Use the function `get` (and a few others) to access various pieces of
% information about the model and its properties, such as variable names,
% parameter values, equations, lag structure, or the model eigenvalues. Two
% related topics are furthermore covered in separate files:
% assigning/changing parameters and steady-state values, and accessing
% model solution matrices.
% edit get_info_about_model.m;
get_info_about_model;
%% Assign and Change Parameters and Steady States
%
% Assign or change the values of parameters and/or steady states of
% variables in a model ob ject using a number of different ways. Under
% different circumstances, different methods of assigning parameters may be
% more convenient (but they, of course, all do the same job).
% edit change_parameters_and_sstates.m;
change_parameters_and_sstates;
%% Model Solution Matrices
%
% Describe and retrieve the state-space form of a solved model. IRIS uses a
% state-space form with two modifications. First, the state-space system is
% transformed so that the transition matrix is upper triangular
% (quasi-triangular). Second, the effect of future anticipated shocks can
% be directly computed upon request, and added to the system stored in the
% model ob ject.
% edit know_all_about_solution.m;
know_all_about_solution;
%% Find and Describe Balanced Growth Path
%
% The SPBC.model is a BGP model: It does not have a stationary long run.
% Instead, it has two unit roots, introduced through the productivity
% process, and the general nominal price level. To deal with BGP models,
% there is absolutely no need to stationarise them. They can be worked with
% directly in their non-stationary forms.
% edit play_with_bgp.m;
play_with_bgp;
%% Simulate Simple Shock Responses
%
% Simulate a simple shock both as deviations from control and in full
% levels, and report the simulation results.
% edit simulate_simple_shock.m;
simulate_simple_shock;
%% More Complex Simulation Experiments
%
% Simulate the differences between anticipated and unanticipated future
% shocks, run experiments with temporarily exogenised variables, and show
% how easy it is to examine simulations with mutliple different
% parameterisations.
% edit simulate_complex_shocks.m;
simulate_complex_shocks;
%% Simulate Permanent Change in Inflation Target
%
% Simulate a permanent change in the inflation target, calculate the
% sacrifice ratio, and run a simple parameter sensitivity exercise using
% model ob jects with multiple parameterizations.
% edit simulate_disinflation.m;
simulate_disinflation;
%% Monte-Carlo Stochatic Simulations
%
% Draw random time series from the model distribution, and compare their
% sample properties against the unconditional model-implied models. Keep in
% mind that this is a purely simulation exercise, and no observed data
% whatsoever are involved.
% edit resample_from_model.m;
resample_from_model;
%% Import CSV Data Files and Prepare Data
%
% Load basic data from CSV data files into databases where each series is
% represented by a tseries (time series) ob ject. Prepare the data to be
% used later with the model: seasonally adjust, convert to quaterly
% periodicity, and create model-consistent variable names.
% edit read_data.m;
read_data;
%% Simulate Fisher Info Matrix and Test Parameter Identification
%
% Calculate estimates of the Fisher information matrix. The Fisher matrix
% is a property of the model itself, and is independent of any data. It
% represents the maximum amount of information one can hope for to find in
% the data in case the data are really generated by the model DGP.
%
% Compare two approaches: a time-domain approach, and a frequency-domain
% approach. Use the singular value decomposition to learn more about which
% parameters (or combinations of them) are identified the best or the
% worst.
% edit fisher_information_matrix.m;
fisher_information_matrix;
%% Run Bayesian Parameter Estimation
%
% Use bayesian methods to estimate some of the parameters. First, set up
% our priors about the individual parameters, and locate the posterior
% mode. Then, run a posterior simulator (adaptive random-walk Metropolis)
% to obtain the whole distributions of the parameters.
% edit estimate_params.m;
estimate_params;
%% Posterior Simulator with 'saveEvery=' Option
%
% In this file, we show two features of convenience when running larger
% posterior simulations. First, the posterior simulator can be run with the
% option `'saveEvery='` to split the simulated posterior chain into smaller
% bits and saving them each in a separate data file. This is a way to get
% around possible out-of-memory problems when simulating larger models
% and/or longer chains. Second, a large posterior simulation can executed
% incrementally in smaller chunks, with the final state of one simulation
% being used as the initial state for the next one.
% edit more_on_poster_simulator.m;
more_on_poster_simulator;
%% Kalman Filtering and Historical Simulations
%
% Run the Kalman filter on the historical data to back out unobservable
% variables (such as the productivity process) and shocks, and perform a
% number of analytical exercises that help understand the inner workings of
% the model.
% edit filter_hist_data.m;
filter_hist_data;
%% More on Kalman Filter
%
% Run more advanced Kalman filter exercises. Split the data sample into two
% sub-samples, and pass information from one to the other. Run the filter
% with time-varying std deviations of some shocks. Evaluate the likelihood
% function and the contributions of individual time periods to the overall
% likelihood.
% edit more_on_kalman_filter.m;
more_on_kalman_filter;
%% Forecasts with Judgmental Adjustments
%
% Use the Kalman filtered data as the starting point for forecasts, both
% unconditional and conditional, i.e. with various types of judgmental
% adjustments.
% edit forecasts_with_judgment.m;
forecasts_with_judgment;
%% Compare Second Moment Properties in Model and Data
%
% Compute and compare several second-moment properties of the estimated
% model and the data. Describe the data using an estimated VAR; this also
% allows to evaluate sampling uncertainty of the empirical estimates using
% bootstrap methods.
% edit compare_model_and_data.m;
compare_model_and_data;
%Anticipated shock
%adapted by Xiangyang Li@SCC, 2017-3-4
close all; //close all existing figures
var
K, //capital stock
Y, //output
N, // labour
C, //consumption
A, // level technology
a, // log technology
STerm//simplifying variables
;
%usually, exogenous variables has steady state zero
varexo epsA;
parameters beta, theta, tau, alpha, psi, delta, rho, Astar;
beta = 0.99; //discount factor
theta = 0.35; //weight of consumption in utility
tau = 2; //risk aversion
alpha = 0.35; //share of capital in production
psi = -0.1; //elasticity of substitution of captial and labor
delta = 0.025; //depreciation rate
rho = 0.85; //autocorrelation of productivity, i.e. technology persisitence
Astar = 1;// steady state of level of productivity or technology;
model(block,bytecode,cutoff=0);
//1: Production Technology
Y = A*(alpha*(K(-1)^psi)+(1-alpha)*(N^psi))^(1/psi);
// 2: Level Technology shock
A = Astar*exp(a);
// 3: Log-level Technology
a = rho*a(-1) + epsA;
// 4. Resource Constraint
K = Y-C + (1-delta)*K(-1);
// 5: Labor supply equation
((1-theta)/theta)*(C/(1-N)) - (1-alpha)*(Y/N)^(1-psi);
// 6: Euler Equation
(((C^theta)*((1-N)^(1-theta)))^(1-tau))/C = STerm(1);
// 7: Simplifying variable
STerm = beta*((((C^theta)*((1-N)^(1-theta)))^(1-tau))/C)*(alpha*((Y/K(-1))^(1-psi))+(1-delta));
end;
steady_state_model;
a = epsA/(1-rho);
A = Astar*exp(a);
Y_K=((1/beta-1+delta)/alpha)^(1/(1-psi));
C_K=Y_K-delta;
N_K=(((Y_K/A)^psi-alpha)/(1-alpha))^(1/psi);
Y_N=Y_K/N_K;
C_N=C_K/N_K;
% Compute steady state of the endogenous variables.
N=1/(1+C_N/((1-alpha)*theta/(1-theta)*Y_N^(1-psi)));
C=C_N*N;
K=N/N_K;
Y=Y_K*K;
STerm=beta*((((C^theta)*((1-N)^(1-theta)))^(1-tau))/C)
*(alpha*((Y/K)^(1-psi))+1-delta);
end;
%the economy starts from the inital steady states
%immediately followed by a steady command and this is equvilent to
%initval block with endogenous variables are equal to steady states;
initval;
epsA = 0;
end;
steady;
%in period 5, technology increases by 8% permanently
%and this is anticipated;
%followed immediately by a steady command and this is equvilent to
%endval block with endogenous variables are equal to steady states;
endval;
epsA = (1-rho)*log(1.08);
end;
steady;
%a shocks block is used to maintain technology remains at
%its initial level during periods 1-4;
shocks;
var epsA;
periods 1:4;
values 0;
end;
simul(periods=98);
%built-in Dynare function
%Plots the simulated trajectory of one or several variables.
rplot C ;
rplot K;
// Latex file produce
//r.write();
//r.compile();
%% Assign and Change Parameters and Steady States
%
% Assign or change the values of parameters and/or steady states of
% variables in a model ob ject using a number of different ways. Under
% different circumstances, different methods of assigning parameters may be
% more convenient (while being all equivalent).
%% Clear Workspace
%
% Clear workspace, close all graphics figures, clear command window, and
% check the IRIS version.
clear
close all
clc
irisrequired 20180131
%#ok<*NOPTS>
%#ok<*NASGU>
%% Read Model File and Assign Parameters to Model ob ject
%
% The easiest way to assign or change parameters is simply by using the
% dot-reference, i.e. the name of the model ob ject dot the name of the
% parameter.
m = model('simple_SPBC.model');
m.alpha = 1.03^(1/4);
m.beta = 0.985^(1/4);
m.gamma = 0.60;
m.delta = 0.03;
m.pi = 1.025^(1/4);
m.eta = 6;
m.k = 10;
m.psi = 0.5;
m.chi = 0.80;
m.xiw = 60;
m.xip = 80;
m.rhoa = 0.90;
m.rhor = 0.8;
m.kappap = 2.5;
m.kappan = 0.1;
m.Short_ = 0;
m.Wage_ = 0;
m.std_Mp = 0;
m.std_Mw = 0;
m.std_Ea = 0.1/100;
%% Assign Parameter Database When Reading Model File
%
% Create first a database with the desired parameter values
% (or use an existing one, for example), and assign the database when
% reading the model file, i.e. when calling the function |model( )|,
% by using the option |Assign=|.
P = struct( );
P.alpha = 1.03^(1/4);
P.beta = 0.985^(1/4);
P.gamma = 0.60;
P.delta = 0.03;
P.pi = 1.025^(1/4);
P.eta = 6;
P.k = 10;
P.psi = 0.5;
P.chi = 0.80;
P.xiw = 60;
P.xip = 80;
P.rhoa = 0.90;
P.rhor = 0.8;
P.kappap = 2.5;
P.kappan = 0.1;
P.Short_ = 0;
P.Wage_ = 0;
P.std_Mp = 0;
P.std_Mw = 0;
P.std_Ea = 0.1/100;
m = model('simple_SPBC.model', ...
'assign=', P);
%% Assign Parameter Database After Reading Model File
%
% Here, use again a parameter database, but assign the database after
% reading the model file, in a separate call to the function |assign( )|.
P = struct( );
P.alpha = 1.03^(1/4);
P.beta = 0.985^(1/4);
P.gamma = 0.60;
P.delta = 0.03;
P.pi = 1.025^(1/4);
P.eta = 6;
P.k = 10;
P.psi = 0.5;
P.chi = 0.80;
P.xiw = 60;
P.xip = 80;
P.rhoa = 0.90;
P.rhor = 0.8;
P.kappap = 2.5;
P.kappan = 0.1;
P.Short_ = 0;
P.Wage_ = 0;
P.std_Mp = 0;
P.std_Mw = 0;
P.std_Ea = 0.1/100;
m = model('simple_SPBC.model');
m = assign(m, P);
%% Change Parameters in Model ob ject
%
% There are several ways how to change some of the parameters. All the
% following three blocks of code do exactly the same.
%
% * Refer directly to the model ob ject using a model-dot-name notation.
m.chi = 0.9;
m.xip = 100;
%%%
%
% * Use the |assign( )| function and specify name-value pairs.
m = assign(m, 'chi=', 0.9, 'xip=', 100);
%%%
%
% * Create a database with somenew values, and run |assign( )|.
P = struct( );
P.chi = 0.9;
P.xip = 100;
m = assign(m, P);
%%%
%
% Reset the parameters to their original values.
m.chi = 0.8;
m.xip = 80;
%% Fast Way to Repeatedly Change Parameters
%
% If you need to iterate over a number of different parameterisations, use
% the fast version of the function |assign( )|. First, initialise the fast
% |assign( )| by specifying the list of parameters (and nothing else).
% Then, use |assign( )| repeatedly to pass different sets of values (in the
% same order) to the model ob ject. Compare the time needed to assign 1,000
% different pairs of values for two parameters.
load MAT/read_model.mat m;
chis = linspace(0.5, 0.95, 1000);
xips = linspace(60, 200, 1000);
assign(m, {'chi', 'xip'});
tic
for i = 1 : 1000
m = assign(m, [chis(i), xips(i)]);
end
toc
tic
for i = 1 : 1000
m.chi = chis(i);
m.xip = xips(i);
end
toc
%% Assign or Change Steady State Manually
%
% If you wish to manually change some of the steady-state values (or, for
% instance, assign all of them because they have been computed outside the
% model), treat the steady-state values the same way as parameters.
%
m = sstate(m, 'Growth=', true, 'Solver=', {'IRIS', 'Display=', 'Off'});
chksstate(m)
disp('Steady-state database')
sstate_database = get(m, 'sstate')
%%%
%
% Change both the levels and growth rates of |Y| and |C| using the
% model-dot-name notation.
m.Y = 2 + 1.01i;
m.Pk = 10 + 1.05i;
%%%
%
% Change the steady states for |Y| and |C| using the function |assign| with
% name-pair values.
m = assign(m, 'Y', 2+1.01i, 'Pk', 10+1.05i);
%%%
%
% Do the same as above but separately for the levels and growth rates.
m = assign(m, '-level', 'Y', 2, 'Pk', 10);
m = assign(m, '-growth', 'Y', 1.01, 'Pk', 1.05);
%%%
%
% Change the steady states by creating a database with the new values, and
% passing the database in |assign|.
P = struct();
P.Y = 2 + 1.01i;
P.Pk = 10 + 1.05i;
m = assign(m, P);
%%%
%
% Note that the newly assigned steady states are, of course, not consistent
% with the model.
disp('Check steady state -- it does not hold');
[flag, listOfEquations] = chksstate(m, 'Error=', false);
flag
listOfEquations
%%%
%
% Reset the steady state to the original values.
m = assign(m, sstate_database);
disp('Check steady state; it holds');
chksstate(m)
一个具有注脚的文本。1
tulipsliu的注释。2
您可以使用渲染LaTeX数学表达式 KaTeX:
Gamma公式展示 Γ(n)=(n−1)!∀n∈N 是通过欧拉积分
Γ(z)=∫0∞tz−1e−tdt.
Equation 1
0=PWtCt(−σ)ϵw(1+τc)Ntφ+(1−ϵw)τws(1−τnt)−NtPWt(Πˉ)Πtϕw((Πˉ)Πwt−1)Yt+NtPWtCt(−σ)ZtβZt+1Ct+1(−σ)(Πˉ)Πwt+1ϕw((Πˉ)Πwt+1−1)Yt+1
Eqation 2
Ndt=Nt
Equation 3
Πwt=ΠtPWt−1PWt
Equation 4
Qt=Πt+1β(CtCt+1)(−σ)ZtZt+1
Equation 5
Rnt=Qt1
Equation 6
Yt=At(StNdt)1−α
Equation 7
Rnt=Πt+1Rrt
Equation 8
Rnt=β1(Πtλw+λpλwΠwtλw+λpλp)ϕπ
Equation 9
Ct=Yt(1−2ϕw((Πˉ)Πwt−1)2)
Equation 10
log(At)=ρalog(At−1)+εat
Equation 11
log(Zt)=ρzlog(Zt−1)−εzt
Equation 12
νt=ρννt−1+ενt
Equation 13
mct=StNdtYt(1−α)PWtτps
Equation 14
1=θpΠtϵp−1+(1−θp)Π∗t1−ϵp
Equation 15
St=(1−θp)Π∗t1−α(−ϵp)+θpΠt1−αϵpSt−1
Equation 16
Π∗t1+ϵp1−αα=ϵp−1ϵpx2tx1t
Equation 17
x1t=mctYtCt(−σ)Zt+βθpΠt+1ϵp+1−ααϵpx1t+1
Equation 18
x2t=YtCt(−σ)Zt+βθpΠt+1ϵp−1x2t+1
Equation 19
log(Y)t=log(Yt)
Equation 20
log(W/P)t=log(PWt)
Equation 21
log(N)t=log(Nt)
Equation 22
πannt=4log(Πt)
Equation 23
$4
{{\pi^{w,ann}}{t}}=4, log\left({{\Pi^w}{t}}\right)
$$
Equation 24
}
iannt=4log(Rnt)
Equation 25
rr,annt=4log(Rrt)
Equation 26
M/Pt=RntηYt
Equation 27
log(A)t=log(At)
Equation 28
log(Π)t=log(Πt)
Equation 29
log(Πw)t=log(Πwt)
Equation 30
log(Z)t=log(Zt)
Equation 31
τnt=(1−ρτn)τnˉ+ρτnτnt−1+ετnt
Equation 32
Wt=Zt(log(Ct)−1+φNt1+φ)
% Equation 33
XWt=1
Equation 34
Vt=Wt+βVt+1
Equation 35
Vgapnatt=Zt(log((1−λ)Ynatt)−1+φNnatt1+φ)+βVgapnatt+1
Equation 36
Vgapt=Vt−Vgapnatt
Equation 37
MPNt=Ndt(StNdt)1−αAt(1−α)
Equation 38
MRSt=Ct(−σ)Ntφ1+τnt1+τc
Equation 39
τwsϵw−1ϵw(AtNnatt1−α)(−σ)1+τnt1+τcNnattφ=τpsϵp(ϵp−1)At(1−α)Nnatt(−α)
Equation 40
Ynatt=AtNnatt1−α
Equation 41
logYgapt=log(Yt)−log(Ynatt)
扫码加好友,拉您进群



收藏
