楼主: tulipsliu
1816 14

[学科前沿] [DSGE] NEWS AND NOISE SHOCKS IN HOUSING MARKET [推广有奖]

经济学论述自由撰稿人!

已卖:2751份资源

学科带头人

45%

还不是VIP/贵宾

-

威望
0
论坛币
386043 个
通用积分
526.9298
学术水平
127 点
热心指数
140 点
信用等级
103 点
经验
46986 点
帖子
1773
精华
0
在线时间
2509 小时
注册时间
2007-11-5
最后登录
2024-8-16

初级热心勋章

楼主
tulipsliu 在职认证  发表于 2021-4-4 19:57:02 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

do it best , economy and management.
Author : Daniel tulips liu .
University : Renmin Universtiy of China.
Copyright © Andrea Gazzani (based on the original codes of FORNI, GAMBETTI, LIPPI AND SALA)

A DSGE Model use Python

我了显示的格式更美观,必须要打字一部分,占用帖子的前面部分。
很久没有能发 MARKDOWN 格式的帖子了,
没想到今天又能发;

挺不错的。

嘿嘿。

NEWS AND NOISE SHOCKS IN HOUSING MARKET

codes

%% Baseline results in "NEWS AND NOISE SHOCKS IN HOUSING MARKET" by Andrea Gazzani (based on the original codes of 
%% FORNI, GAMBETTI, LIPPI AND SALA)

clc; close all; clear all;
tic
set(0,'defaultUicontrolFontName','Palatina');set(0,'defaultUitableFontName','Palatina');
set(0,'defaultAxesFontName','Palatina'); set(0,'defaultTextFontName','Palatina');
set(0,'defaultUipanelFontName','Palatina'); set(0,'defaultlinelinewidth',2);

addpath(genpath('Auxiliaries'))

%% Load and prepare data

VARvnames_long = {'Rents';'Housing Prices';'GDP';'Residential Investment';...
    'Bba Corp Bond Yield';'S&P Composite Index';}; % define var names
VARvnames = {'rents_fred_raw_rentonly';'hp_av_census';'rgdp'; 'resinv'; 'BAA_yield'; 'sp'};
VARnvar = length(VARvnames);

[xlsdata, xlstext] = xlsread('\DataSet_NewsNoise.xlsx'); % load data
data   = Num2NaN(xlsdata(65:end-1,1:end));
dates = xlstext(70:end-1,1);
vnames = xlstext(3,2:end);
for ii=1:length(vnames)
    DATA.(vnames{ii}) = data(:,ii);
end

DATA.rents_fred_raw_rentonly = log(100*DATA.rents_fred_raw_rentonly./(DATA.pce_ipd.*DATA.pop)); % real rents
DATA.rents_cpi = log(100*DATA.rents_cpi./(DATA.pce_ipd)); % real rents
DATA.shiller_hp = log(100*DATA.shiller_hp); % real HP - Shiller
DATA.hp_med_census = log(100*DATA.hp_med_census./DATA.ipd); % real HP - Census
DATA.hp_av_census = log(100*DATA.hp_av_census./DATA.ipd); % real HP - Census
DATA.sp = log(100*DATA.sp./DATA.ipd); % real SP
DATA.rgdp = log(100*DATA.rgdp./DATA.pop); % real per capita GDP
DATA.resinv = log(100*DATA.resinv./(DATA.ipd_resinv.*DATA.pop)); % real per capita Res Inv
DATA.hstarts = log(100*DATA.hstarts);
DATA.hpermits = log(100*DATA.hpermits);

nobs = size(data,1);
ENDO = nan(nobs,VARnvar);
for ii=1:VARnvar
    ENDO(:,ii) = DATA.(VARvnames{ii});
end

Data = ENDO;

%% Parameters

ll= 40;
npc=0;
nrepli=2000;
maxlags=8;
col2 = [.65 .65 .65];
col1 = [.85 .85 .85];


%storeFEV_surprise = varu./varseries; storeFEV_signal = varalpha./varseries;

varnews0 = squeeze(cumsum(irfs(:,1,:).^2,3)); % variance by news
varnoise0 = squeeze(cumsum(irfs(:,2,:).^2,3)); % variance by noise

varnewsnoise = varnews0 + varnoise0; % news + noise
totvar = totvarnonstruc - varualfa + varnewsnoise; % total var
varnews1 = varnews0./totvar; % variance explained by news
varnoise1 = varnoise0./totvar; % variance explained by noise

tab7= varnews1(varseries,[1 5 9 17 41]);
tab8= varnoise1(varseries,[1 5 9 17 41]);
tab9= varnews1(varseries,[1 5 9 17 41])+varnoise1(varseries,[1 5 9 17 41]);

matrix2lyx(tab7*100, 'tableNEWS.lyx', '%.1f');
matrix2lyx(tab8*100, 'tableNOISE.lyx', '%.1f');
matrix2lyx(tab9*100, 'tableNEWS+NOISE.lyx', '%.1f');
matrix2lyx(tab5*100, 'tableLEARNING.lyx', '%.1f');
matrix2lyx(tab6*100, 'tableSIGNAL.lyx', '%.1f');

%% Bootstrap standard errors

X = Data(:,varseries); % load data

[redAR,sigma,sidui,R2,constant,Yhat,ex]=varestimy(lags,X,1); % estimate VAR
ciao = chol(cov(sidui));
[T N] = size(X);
AA = [];
for t=2:lags+1
    AA = [AA squeeze(-redAR(:,:,t))];
end

hor = 40;
jota = [AA ; [eye(N*(lags-1),N*(lags-1)) zeros(N*(lags-1),N)]];
for s = 1:hor
    IRF_j = jota^(s-1);
    IRF_lev(:,:,s) = IRF_j(1:N,1:N,:)*ciao';
end
temp = sidui*inv(ciao);

standard_shocks = temp;

p = lags;
[A,SIGMA,U,V]=olsvarc(X,p); % estimate VAR in levels

h = 40;
[CI,CI_chol]=boot_luca_MINE2(A,U,X,V,p,h,nrepli,varseries,ind1,ind); % bootstrap

irfold = prctile(CI_chol,[50],4);
irf_ls = irfold;

%% Cumulative FEVD

totvarnonstruc = squeeze(sum(cumsum(irfold.^2,3),2)); % total variance

varu = squeeze(cumsum(irf_ls(:,ind1,:).^2,3)); % variance by learning
varalfa = squeeze(cumsum(irf_ls(:,ind,:).^2,3)); % variance by signal
varualfa = varu + varalfa;
 
varsignal = varalfa./totvarnonstruc; varsurprise = varu./totvarnonstruc;

irf_nn = prctile(CI,[50],4);

varnews0 = squeeze(cumsum(irf_nn(:,1,:).^2,3)); % variance by news
varnoise0 = squeeze(cumsum(irf_nn(:,2,:).^2,3)); % variance by noise

varnewsnoise = varnews0 + varnoise0; % news + noise
totvar = totvarnonstruc - varualfa + varnewsnoise; % total var
varnews1 = varnews0./totvar; % variance explained by news
varnoise1 = varnoise0./totvar; % variance explained by noise

varnews1(1,1) = 0; varnoise1(1,1) = 0; % implied by theoretical definitions in FGLS
tab_varSIGNAL_CumSum = varsignal(:,[1 5 9 17 41]); tab_varSURPRISE_CumSum = varsurprise(:,[1 5 9 17 41]);
tab_varNEWS_CumSum = varnews1(:,[1 5 9 17 41]); tab_varNOISE_CumSum=varnoise1(:,[1 5 9 17 41]);


ff=1;
figure(ff);
FigSize(30,21)
for t=1:size(varnews1,1)
        subplot(size(varnews1,1)/2,2,t);
        a=bar([(100*varnews1(t,1:ll))' 100*varnoise1(t,1:ll)'],'stacked');grid('on'); %axis tight;
        set(a(1),'facecolor','b');
        set(a(2),'facecolor','r');
        tit(t)=title(ti{t});
        xlab(t)=xlabel('Horizon','FontSize',12);
        ylab(t)=ylabel('% FEV');
        axis tight;
        %axis([0 ll-1 0 100])
        optfont = FigFontOption_Typewriter; optfont.title_weight = 'bold'; FigFont(optfont)
end
leg=legend('News','Noise');
set(ylab,'FontSize',12); set(leg,'FontSize',14); set(tit,'FontSize',14); set(xlab,'FontSize',12);
spaceplots([0.01 0.01 0.01 0.01], [.02 .02]);
set(gcf, 'Color', 'w');


varnews0_p = squeeze(irf_nn(:,1,:).^2); % variance by news
varnoise0_p = squeeze(irf_nn(:,2,:).^2); % variance by noise

varnewsnoise_p = varnews0_p + varnoise0_p; % news + noise
totvar_p = totvarnonstruc_p - varualfa_p + varnewsnoise_p; % total var
varnews1_p = varnews0_p./totvar_p; % variance explained by news
varnoise1_p = varnoise0_p./totvar_p; % variance explained by noise

varlearning_p= varu_p./totvarnonstruc_p; % variance explained by learnig instantaneously
varsignal_p= varalfa_p./totvarnonstruc_p; % variance explained by signal instantaneously, 1 year, 2 year,...
varnews1_p(1,1) = 0; varnoise1_p(1,1) = 0; % implied by theoretical definitions of FGLS

ff=1;
figure(ff);
FigSize(30,21)
for t=1:size(varnews1_p,1)
        subplot(size(varnews1_p,1)/2,2,t);
        a=bar([(100*varnews1_p(t,1:ll))' 100*varnoise1_p(t,1:ll)'],'stacked');grid('on'); %axis tight;
        set(a(1),'facecolor','b');
        set(a(2),'facecolor','r');
        tit(t)=title(ti{t});
        xlab(t)=xlabel('Horizon','FontSize',12);
        ylab(t)=ylabel('% FEV');
        axis tight;
        %axis([0 ll-1 0 100])
        optfont = FigFontOption_Typewriter; optfont.title_weight = 'bold'; FigFont(optfont)
end
leg=legend('News','Noise');
set(ylab,'FontSize',12); set(leg,'FontSize',14); set(tit,'FontSize',14); set(xlab,'FontSize',12);
spaceplots([0.01 0.01 0.01 0.01], [.02 .02]);
set(gcf, 'Color', 'w');

% Store Tables

tab_varSIGNAL = varsignal_p(:,[1 5 9 17 41]); tab_varSURPRISE = varlearning_p(:,[1 5 9 17 41]);
tab_varNEWS = varnews1_p(:,[1 5 9 17 41]); tab_varNOISE=varnoise1_p(:,[1 5 9 17 41]);

matrix2lyx(tab_varNEWS*100, 'NEWS_HbyH.lyx', '%.1f');
matrix2lyx(tab_varNOISE*100, 'NOISE_HbyH.lyx', '%.1f');
matrix2lyx((tab_varNOISE+tab_varNEWS)*100, 'NEWS+NOISE_HbyH.lyx', '%.1f');
matrix2lyx(tab_varSURPRISE*100, 'SURPRISE_HbyH.lyx', '%.1f');
matrix2lyx(tab_varSIGNAL*100, 'SIGNAL_HbyH.lyx', '%.1f');


%% Second ordering

varseries2 = [3 4 5 1 2 6];
 
% Define indexes for variables

ind1 = 4; ind = 5;

codeData=ones(1,6);codeData(varseries2(:)) = 0;
nr = 10;

% Computation-identification

[irfs2,irfsb2,irf2,irfb2,sigma_a2,sigma_e2,sigma_aboot2,bidielle2,sh2,ssh2,wr2,shocks2] = FAVARNewsNoiseChol_allshocks(Data,...
    [Data(:,varseries2) pc(:,1:npc)],codeData,lags,ll,nr,[varseries2 15:15+npc-1],ind1,ind);

[r2,p2,rlo2,rup2]=corrcoef([ssh2 shocks2]);

% Variance Decomposition

irfold2 = irf2;
totvarnonstruc2 = squeeze(sum(cumsum(irfold2.^2,3),2)); % total variance

varu2= squeeze(cumsum(irf2(:,ind1,:).^2,3)); % variance by learning
varalfa2 = squeeze(cumsum(irf2(:,ind,:).^2,3)); % variance by signal
varualfa2 = varu2 + varalfa2;

tab52= varu2(varseries2,[1 5 9 17 41])./totvarnonstruc2(varseries2,[1 5 9 25 41]); % variance explained by learnig instantaneously, 1 year, 2 year,...
tab62= varalfa2(varseries2,[1 5 9 17 41])./totvarnonstruc2(varseries2,[1 5 9 25 41]); % variance explained by signal instantaneously, 1 year, 2 year,...

varnews02 = squeeze(cumsum(irfs2(:,1,:).^2,3)); % variance by news
varnoise02 = squeeze(cumsum(irfs2(:,2,:).^2,3)); % variance by noise

varnewsnoise2 = varnews02 + varnoise02; % news + noise
totvar2 = totvarnonstruc2 - varualfa2 + varnewsnoise2; % total var
varnews12= varnews02./totvar2; % variance explained by news
varnoise12 = varnoise02./totvar2; % variance explained by noise

tab72= varnews12(varseries2,[1 5 9 17 41 ll+1]);
tab82= varnoise12(varseries2,[1 5 9 17 41 ll+1]);
tab92 = varnews12(varseries2,[1 5 9 17 41 ll+1])+varnoise12(varseries2,[1 5 9 17 41 ll+1]);

matrix2lyx(tab72*100, 'NEWS_2ord.lyx', '%.1f');
matrix2lyx(tab82*100, 'NOISE_2ord.lyx', '%.1f');
matrix2lyx((tab92)*100, 'NEWS+NOISE_2ord.lyx', '%.1f');
matrix2lyx(tab52*100, 'SURPRISE_2ord.lyx', '%.1f');
matrix2lyx(tab62*100, 'SIGNAL_2ord.lyx', '%.1f');

ind1 = 1;
ind = 2;
indGDP =3;
indC= 7;
indI= 4;
indFFR=5;
indpi=6;
indD = 8;

ff=2;
DoFigures_Hist


Enjoy

Yours Daniel Tulips Liu

Renmin University of China , Beijing.

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Housing market Shocks marke Using

劳动经济学

沙发
tulipsliu 在职认证  发表于 2021-4-4 20:48:53
Python 那边的 MARKDOWN 格式的文件出现乱码, 悲剧。

不过说实话,我不喜欢 python ,不好用。

MATLAB 和 R 才顺手。

藤椅
tulipsliu 在职认证  发表于 2021-4-17 06:21:25
%%% Copy and paste the following output in your LaTeX file
$$
\left(\begin{array}{c}
dx1\\ dx2
\end{array}\right)
=
\left(\begin{array}{c}
-3 \cdot x1 \\
-x1 - 2 \cdot x2 \\
\end{array}\right) dt
+
\left[\begin{array}{ccc}
1&0&x2 \\
x1&3&0 \\
\end{array}\right]
'
\left(\begin{array}{c}
dW1\\ dW2\\ dW3
\end{array}\right)
$$
$$
\left(\begin{array}{c}
x1(0)=0 \\
x2(0)=0 \\
\end{array}\right)
$$

板凳
tulipsliu 在职认证  发表于 2021-5-8 18:35:17
We way the statistics reported in the library are generated is spelt out in Shephard and Sheppard (2009). Here we give a brief summary.

The simplest realised measure is realised variance

$$RM_{t}= \sum x_{j,t}^2$$
where

$$x_{j,t}=X_{t_{j,t}}-X_{t_{j-1,t}}$$
and \(t_{j,t}\) are the times of trades or quotes (or a subset of them) on the t-th day. The theoretical justification of this measure is that if prices are observed without noise then as \(min_{j}|t_{j,t}-t_{j-1,t}| \downarrow 0\) it consistently estimates the quadratic variation of the price process on the \(t\)-th day. It was formalised econometrically by Andersen, Bollerslev, Diebold and Labys (2001) and Barndorff-Nielsen and Shephard (2002).

In practice market microstructure noise plays an important part and the above authors use 1-5 minute return data or a subset of trades or quotes (e.g. every 15th trade) to mitigate the effect of the noise. Hansen and Lunde (2006) systematically study the impact of noise on realised variance. If a subset of the data is used with the realised variance, then it is possible to average across many such estimators each using different subsets. This is called subsampling. When we report RV estimators we always subsample them to the maximum degree possible from the data as this averaging is always theoretically beneficial especially in the presence of modest amounts of noise.

Three classes of estimators which are somewhat robust to noise have been suggested in the literature: preaveraging (Jacod, Li, Mykland, Podolskij and Vetter(2007)), multiscale Zhang (2007) and Zhang, Mykland and Ait-Sahalia (2005)) and realised kernel (Barndorff-Nielsen, Hansen, Lunde and Shephard (2008)).

Here we focus on the realised kernel in the case where we use a Parzen weight function. It has the familiar form of a HAC type estimator (except there is no adjustment for mean and the sums are not scaled by their sample size)

$$RM_{t}=\sum_{h=-H}^{H} k(h/(H+1))\gamma_{h}$$
where

$$\gamma_{h}=\sum_{j=|h|+1}^n x_{j,t}x_{j-|h|,t}$$
and \(k(x)\) is the Parzen kernel function. It is necessary for \(H\) to increase with the sample size in order to consistently estimate the increments of quadratic variation in the presence of noise. We follow precisely the bandwidth choice of \(H\) spelt out in Barndorff-Nielsen, Hansen, Lunde and Shephard (2009), to which we refer the reader for details. This realised kernel is guaranteed to be non-negative, which is quite important as some of our time series methods rely on this property.

报纸
tulipsliu 在职认证  发表于 2021-5-15 20:28:08
$$ \lambda_1(t) = \mu_1 + \lambda_{11}(t) + \lambda_{12}(t) $$

地板
tulipsliu 在职认证  发表于 2021-5-15 20:28:31
$$ \lambda_2(t) = \mu_2 + \lambda_{21}(t) + \lambda_{22}(t) $$

7
tulipsliu 在职认证  发表于 2021-5-15 22:14:52
$$
\left[
  q_{rs}(t, z(t))  =  \lim_{\delta t \rightarrow 0} \Prob (S(t+\delta t) = s | S(t) = r) / \delta t.
\right]
$$

8
tulipsliu 在职认证  发表于 2021-5-17 15:00:38
  1. ar1_sd(ϕ, ω) = 1 ./ (1 .- 2 * ϕ * cos.(ω) .+ ϕ.^2)
  2. ω_s = range(0, π, length = 180)
  3. plt_1=plot()
  4. plt_2=plot()
  5. plots=[plt_1, plt_2]
  6. for (i, ϕ) in enumerate((0.8, -0.8))
  7. sd = ar1_sd(ϕ, ω_s)
  8. label = "spectral density, phi = $ϕ"
  9. plot!(plots[i], ω_s, sd, color=:blue, alpha=0.6, lw=2, label=label)
  10. plot!(plots[i], legend=:top, xlabel="frequency", xlim=(0,π))
  11. end
  12. plot(plots[1], plots[2], layout=(2,1), size=(700,500))
复制代码

9
tulipsliu 在职认证  发表于 2021-5-17 15:03:23
  1. function quad_plot(arma)
  2.        plt_1 = plot()
  3.        plt_2 = plot()
  4.        plt_3 = plot()
  5.        plt_4 = plot()
  6.        plots = [plt_1, plt_2, plt_3, plt_4]
  7.        plot_functions = [plot_spectral_density,
  8.        plot_impulse_response,
  9.        plot_autocovariance,
  10.        plot_simulation]
  11.        for (i, plt, plot_func) in zip(1:1:4, plots, plot_functions)
  12.        plots[i] = plot_func(arma, plt)
  13.        end
  14.        return plot(plots[1], plots[2], plots[3], plots[4], layout=(2,2),size=(800,800))
  15.        end
复制代码

10
tulipsliu 在职认证  发表于 2021-5-17 15:09:06
  1. n = 400
  2. ϕ = 0.5
  3. θ = [0, -0.8]
  4. σ = 1.0
  5. lp = ARMA(ϕ, θ, 1.0)
  6. X = simulation(lp, ts_length = n)
  7. xs = []
  8. x_sds = []
  9. x_sms = []
  10. ys = []
  11. y_sds = []
  12. y_sms = []
  13. titles = []
  14. for (i, wl) in enumerate([15, 55, 175]) # window lengths
  15. x, y = periodogram(X)
  16. push!(xs, x)
  17. push!(ys, y)
  18. x_sd, y_sd = spectral_density(lp, two_pi=false, res=120)
  19. push!(x_sds, x_sd)
  20. push!(y_sds, y_sd)
  21. x, y_smoothed = periodogram(X, "hamming", wl)
  22. push!(x_sms, x)
  23. push!(y_sms, y_smoothed)
  24. t = "window length = $wl"
  25. push!(titles, t)
  26. end
复制代码


part two
  1. plot(xs, ys, layout=(3,1), color=:blue, alpha=0.5,
  2. linewidth=2, label=["periodogram" "" ""])
  3. plot!(x_sds, y_sds, layout=(3,1), color=:red, alpha=0.8,
  4. linewidth=2, label=["spectral density" "" ""])
  5. plot!(x_sms, y_sms, layout=(3,1), color=:black,
  6. linewidth=2, label=["smoothed periodogram" "" ""])
  7. plot!(title=reshape(titles,1,length(titles)))
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-27 10:17