CS代写 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Chapter 1: Transaction costs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Copyright By PowCoder代写 加微信 powcoder

%Let us clean the environment
clear variables

%Let’s load the data
d=xlsread(‘../Data/spreads_microstructure.xls’);

quest = 1; %This needs to be chosen by the user: quest 1 refers to the first question, quest 2 to the second and so on

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% First question – histogram of the spreads and some descriptive statistics
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if quest == 1

table_estimates = table(mean(d(:,1)),median(d(:,1)),max(d(:,1)),min(d(:,1)),std(d(:,1)),skewness(d(:,1)),kurtosis(d(:,1)), ‘VariableNames’,…
[“mean” “median” “maximum” “minimum” “standard deviation” “skewness” “kurtosis”]);
disp(‘table of descriptive statistics’);
disp(table_estimates);

histogram(d(:,1),20)
title(‘histogram of the bid-ask spreads’)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Second question – log transformation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

elseif quest == 2

y = log(d(:,1));

table_estimates1 = table(mean(y),median(y),max(y),min(y),std(y),skewness(y),kurtosis(y), ‘VariableNames’,…
[“mean” “median” “maximum” “minimum” “standard deviation” “skewness” “kurtosis”]);
disp(‘table of descriptive statistics’);
disp(table_estimates1);

histogram(y,20)
title(‘histogram of the log bid-ask spreads’)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Third question – regression with t-statistics
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

elseif quest == 3

y = log(d(:,1));
x = [ones(100,1),d(:,2:6)];

% (1)regress using matrices

beta = inv(x’*x)*(x’*y);
u = y – x*beta;

Var_u = (u’*u)/(100 – 6);
Var_beta = Var_u * inv(x’*x);

t_stat_int = beta(1)/sqrt(Var_beta(1,1));
t_stat_vol = beta(2)/sqrt(Var_beta(2,2));
t_stat_size = beta(3)/sqrt(Var_beta(3,3));
t_stat_trades = beta(4)/sqrt(Var_beta(4,4));
t_stat_turn = beta(5)/sqrt(Var_beta(5,5));
t_stat_numb = beta(6)/sqrt(Var_beta(6,6));

se = sqrt(diag(Var_beta));
t_stat = cat(1,t_stat_int,t_stat_vol,t_stat_size,t_stat_trades,t_stat_turn,t_stat_numb);

table_estimates3 = table(beta,se,t_stat,’VariableNames’,…
[“Estimate” “SE” “tStat”],…
‘RowNames’,[“(Intercept)” “vol” “size” “trades” “turn” “anal”]);
disp(‘Table of estimates: manual’);
disp(table_estimates3);

% (2) Regress using the pre-programmed fitlm command in Matlab

mod = fitlm(d(:,2:6),y);
disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
disp(‘Table of estimates: fitlm command’);
disp(mod);

% (3) Regress using the ols command – you need the file ols.m in
% your matlab path

[a,b]=size(y);
int = ones(a,1);
regressors = cat(2,int,d(:,2:6));
out = ols(y,regressors);

table_estimates4 = table(out.beta,out.bstd,out.tstat,’VariableNames’,…
[“Estimate” “SE” “tStat”],…
‘RowNames’,[“(Intercept)” “vol” “size” “trades” “turn” “anal”]);
disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
disp(‘Table of estimates: ols function’);
disp(table_estimates4);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fourth question – testing (does volatility matter?)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

elseif quest == 4

y = log(d(:,1));
x = [ones(100,1),d(:,2:6)];

beta = inv(x’*x)*(x’*y);
u = y – x*beta;

Var_u = (u’*u)/(100 – 6);
Var_beta = Var_u * inv(x’*x);

% Test of single restriction (H^0:beta(2)=0): notice that the result coincides with
% what we found in the previous question

c= [0; 1; 0; 0; 0; 0];

t_stat_vol = (c’*beta)/sqrt((Var_u)*c’*inv(x’*x)*c);
p_value_t = 2*(1 – cdf(‘Noncentral t’,t_stat_vol,94,0));

disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
fprintf(‘The t-stat is %4.3f and the p-value is %4.3f \n’,t_stat_vol,p_value_t);

% Using a test for multiple restrictions to test a single restriction ((H^0:beta(2)=0)

R = [0 1 0 0 0 0];

F_test = Var_u^(-1)*(R*beta)’*inv(R*(inv(x’*x)*R’))*(R*beta);
p_value_F = 1 – cdf(‘F’,F_test,1,94);

disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
fprintf(‘The F test is %4.3f and the p-value is %4.3f \n’,F_test,p_value_F);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fifth question – testing, again
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

elseif quest == 5

y = log(d(:,1));
x = [ones(100,1),d(:,2:6)];

beta = inv(x’*x)*(x’*y);
u = y – x*beta;

Var_u = (u’*u)/(100 – 6);
Var_beta = Var_u * inv(x’*x);

% Test of single restriction (H^0:beta(2)=1):

c= [0; 1; 0; 0; 0; 0];

t_stat_vol = (c’*beta-1)/sqrt((Var_u)*c’*inv(x’*x)*c);
p_value_t = 2*(1 – cdf(‘Noncentral t’,t_stat_vol,94,0));

disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
fprintf(‘The t-stat is %4.3f and the p-value is %4.3f \n’,t_stat_vol,p_value_t);

% Using a (one-sided) test for multiple restrictions to test a single restriction (H^0:beta(2)=1)

R = [0 1 0 0 0 0];

F_test = Var_u^(-1)*(R*beta-gam)’*inv(R*(inv(x’*x)*R’))*(R*beta-gam);
p_value_F = 1 – cdf(‘F’,F_test,1,94);

disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
fprintf(‘The F test is %4.3f and the p-value is %4.3f \n’,F_test,p_value_F);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sixth question – testing, again (single restriction on two parameters)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

elseif quest == 6

y = log(d(:,1));
x = [ones(100,1),d(:,2:6)];

beta = inv(x’*x)*(x’*y);
u = y – x*beta;

Var_u = (u’*u)/(100 – 6);
Var_beta = Var_u * inv(x’*x);

% Test of single restriction (H^0:beta(3)=beta(4)):

c= [0; 0; 1; -1; 0; 0];

t_stat = (c’*beta)/sqrt((Var_u)*c’*inv(x’*x)*c);
p_value_t = 2*(1 – cdf(‘Noncentral t’,t_stat,94,0));

disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
fprintf(‘The t-stat is %4.3f and the p-value is %4.3f \n’,t_stat,p_value_t);

% Using a (one-sided) test for multiple restrictions to test a single restriction
% (H^0:beta(3)=beta(4)):

R = [0 0 1 -1 0 0];

F_test = Var_u^(-1)*(R*beta-gam)’*inv(R*(inv(x’*x)*R’))*(R*beta-gam);
p_value_F = 1 – cdf(‘F’,F_test,1,94);

disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
fprintf(‘The F test is %4.3f and the p-value is %4.3f \n’,F_test,p_value_F);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Seventh question – testing, again (multiple restrictions)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

elseif quest == 7

y = log(d(:,1));
x = [ones(100,1),d(:,2:6)];

beta = inv(x’*x)*(x’*y);
u = y – x*beta;

Var_u = (u’*u)/(100 – 6);
Var_beta = Var_u * inv(x’*x);

%Multiple restrictions (H^0:beta(5)=0 and beta(6)=0):

R = [0 0 0 0 1 0; 0 0 0 0 0 1];
gam = [0;0];

F_test = Var_u^(-1)*(R*beta-gam)’*inv(R*(inv(x’*x)*R’))*(R*beta-gam)/2;
p_value_F = 1 – cdf(‘F’,F_test,2,94);

disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
fprintf(‘The F-test is %4.3f and the p-value is %4.4f \n’,F_test,p_value_F);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Eight question – prediction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

elseif quest == 8

y = log(d(:,1));
x = [ones(100,1),d(:,2:5)];

beta = inv(x’*x)*(x’*y);
u = y – x*beta;

x_pred = [1 -3.5 10.5 7.6 -1.1];

y_pred = x_pred*beta;

y_pred = exp(y_pred);

disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
fprintf(‘The prediction is %4.7f\n’,y_pred);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Regression, liquidity and asymmetric information
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

elseif quest == 9

y = log(d(:,1));
x = [ones(100,1),d(:,2:6)];

beta = inv(x’*x)*(x’*y);
u = y – x*beta;

Var_u = (u’*u)/(100 – 6);
Var_beta = Var_u * inv(x’*x);

% Multiple restrictions using F test and Chisquare test (H^0:beta(5)=0 and beta(6)=0):

R = [0 0 0 0 1 0; 0 0 0 0 0 1];
gam = [0;0];

F_test = Var_u^(-1)*(R*beta-gam)’*inv(R*(inv(x’*x)*R’))*(R*beta-gam)/2;
Wald_test = Var_u^(-1)*(R*beta-gam)’*inv(R*(inv(x’*x)*R’))*(R*beta-gam);

p_value_F = 1 – cdf(‘F’,F_test,2,94);
p_value_W = 1 – cdf(‘Chisquare’,Wald_test,2);

disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
fprintf(‘The F test is %4.3f and the p-value is %4.4f \n’,F_test,p_value_F);
disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
fprintf(‘The Wald test is %4.3f and the p-value is %4.4f \n’,Wald_test,p_value_W);

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com