



%% Warming up
clear
close all
clc

%% Parameters of the model
% fun parameters mm=200, sigma= 0.03 rho = o.7 rhop = o.3 steady = 09
mm = 1000;    %learning parameter gamma

T = 1000;
sigma = 0.03;      %standard deviation shocks price growth
rho=0.95;           %rho in mean squares errors
rhop=0.3;           %rho in shocks to price growth
steady=0.9;

p = zeros(T,1); % %price increase
plagt = zeros(T,1); %lagged % price increase
pr = ones(T,1); % price
lnpr = zeros(T,1); % log price

epf = zeros(T,1);
epc = zeros(T,1);
crp = zeros(T,1);
frp = zeros(T,1);
alfapt = zeros(T,1);
epsilont = zeros(T,1);

epsv = zeros(T,1); 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%heuristic model
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
    alfap=0.5;
  
for t=2:T
    
    epsilont(t) = rhop*epsilont(t-1) + sigma*randn;    %shocks to price growth
    
    epsilon = epsilont(t);
  
  
    epfs = 0;  %fundamentalists  price growth
   
    epcs=p(t-1); %extrapolative price expectation
  
    eps=alfap*epcs+(1-alfap)*epfs; %market price expectation
    p(t) = eps + epsilon;
    pr(t) = pr(t-1)*exp(p(t));
    lnpr(t) = lnpr(t-1)+p(t);
    
    
    %So far for time t variables, now we need to construct the alphas and
    %betas for next period: individuals revise their decision rules based on the
    %discrepancies between period t variables and their previous period
    %expectations of them
    
    crp(t) = rho*crp(t-1) - (1-rho)*(epcs-p(t))^2;
    frp(t) = rho*frp(t-1) - (1-rho)*(epfs-p(t))^2;
  
    alfap =(exp(mm*crp(t)))/(exp(mm * crp(t)) + exp(mm*frp(t))); %more generally, with persistence in alpha: rhoBH*alfapt(t-1)+(1-rhoBH)*exp(mm*CRp(t))/(exp(mm * CRp(t)) + exp(mm * FRp(t)));
    
    alfap = steady*alfap
   
    alfapt(t) = alfap;
   
    
    

end



'mean, median, max, min, & standard deviation of the price chanbe'

Mean    = mean(p)
Stadev  = std(p)
Median  = median(p)
Max     = max(p)
Min     = min(p)


% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

tstart= 1001; %drop the first 1000 observations
pl = p(2:T-1);

%alfapt = alfapt(tstart:T);

figure(100);
hist(p,50); %bins the elements of p into 50 equally spaced containers
title({'histogram price change'},'FontWeight','bold');

figure(2); clf; hold on;
plot(1:T,p);
grid on;
xlabel('Time');
ylabel('Level');
title({'price increase'},'FontWeight','bold');


figure(3); clf; hold on;
plot(1:T,alfapt);
grid on;
xlabel('Time');
ylabel('Level');
title({'alpha'},'FontWeight','bold');

figure(5); clf; hold on;
plot(1:T,lnpr);
grid on;
xlabel('Time');
ylabel('Level');
title({'log price'},'FontWeight','bold');

figure(4); clf; hold on;
plot(1:T,pr);
grid on;
xlabel('Time');
ylabel('Level');
title({'price'},'FontWeight','bold');
