%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Program "chapter4splines" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab program for spline-based decomposition of energy intensity in three effects (structural, intensity, activity).
% Written by Paula F. Gonzalez, Manuel Landajo & Maria J. Presno (2014).
% NOTE: It requires the splines toolbox of Matlab.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
clear;
%
[n,N,E,Y,dates]=dataread('c:\datachapter4.csv');
%
%
% Type of decomposition ('additiv' = additive), ('multipl' = multiplicative):
tipo='multipl';
%tipo='additiv';
%
%
% Numeric integration parameter (no. of bins per year):
bins=2000;
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=E;
y=Y;
clear E;
clear Y;
%
T=n-1;
numk=N;
t=[0:(1/T):1]';
TTT=T*bins;
h = 1/TTT;
tt =[0:h:1];
largo0=TTT+1;
% Spline interpolants for sectoral energy consumptions (x) and productions (y):
xt=[zeros(largo0,1)];
yt=[zeros(largo0,1)];
for k=1:numk
xin=spline(t,x(:,k),tt);
yin=spline(t,y(:,k),tt);
xt=[xt,xin'];
yt=[yt,yin'];
end
xt=[xt(:,2:(numk+1))];
yt=[yt(:,2:(numk+1))];
% Interpolated totals, and sector intensities and shares:
xtot=sum(xt')';
ytot=sum(yt')';
Itot=[xtot./ytot];
for j=1:numk
I(:,j)=xt(:,j)./yt(:,j);
S(:,j)=yt(:,j)./ytot;
difI(:,j)=diff(I(:,j))/h;
difS(:,j)=diff(S(:,j))/h;
end
difytot(:,1)=diff(ytot)/h;
%
largoderi=length(difI(:,1));
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ADDITIVE DECOMPOSITION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if tipo=='additiv'
%
disp('ADDITIVE DECOMPOSITION:')
E0=xtot(1)
E1=xtot(largo0)
Variation=E1-E0
%
% 1) Intensity effect:
sumainte=0;
for i=1:largoderi
sumainte=sumainte+(ytot(i)+ytot(i+1))/2*difI(i,:)*(S(i,:)'+S(i+1,:)')/2*h;
intenseffect(i,1)=sumainte;
end
intenseffect=[0;intenseffect];
Intensity_effect=sumainte
%
% 2) Structural effect:
sumainte2=0;
for i=1:largoderi
sumainte2=sumainte2+(ytot(i)+ytot(i+1))/2*difS(i,:)*(I(i,:)'+I(i+1,:)')/2*h;
structeffect(i,1)=sumainte2;
end
structeffect=[0;structeffect];
Structural_effect=sumainte2
%
% 3) Activity effect:
sumainte3=0;
for i=1:largoderi
sumainte3=sumainte3+difytot(i)*(S(i,:)+S(i+1,:))/2*(I(i,:)'+I(i+1,:)')/2*h;
activeffect(i,1)=sumainte3;
end
activeffect=[0;activeffect];
%
Activity_effect=sumainte3
%
totaleffect=intenseffect+structeffect+activeffect;
end
%
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MULTIPLICATIVE DECOMPOSITION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
if tipo=='multipl'
%
disp('MULTIPLICATIVE DECOMPOSITION:')
E0=xtot(1)
E1=xtot(largo0)
Ratio=E1/E0
%
% 1) Intensity effect:
sumainte1=0;
for i=1:largoderi
sumainte1=sumainte1+difI(i,:)*(S(i,:)'+S(i+1,:)')/2*(ytot(i)+ytot(i+1))/(xtot(i)+xtot(i+1))*h;
intenseffect(i,1)=sumainte1;
end
intenseffect=exp([0;intenseffect]);
Intensity_effect=exp(sumainte1)
% 2) Structural effect:
sumainte3=0;
for i=1:largoderi
sumainte3=sumainte3+difS(i,:)*(I(i,:)'+I(i+1,:)')/2*(ytot(i)+ytot(i+1))/(xtot(i)+xtot(i+1))*h;
structeffect(i,1)=sumainte3;
end
structeffect=exp([0;structeffect]);
%
Structural_effect=exp(sumainte3)
%
% 3) Activity effect:
sumainte4=0;
for i=1:largoderi
sumainte4=sumainte4+difytot(i)*(S(i,:)+S(i+1,:))/2*(I(i,:)'+I(i+1,:)')/(xtot(i)+xtot(i+1))*h;
activeffect(i,1)=sumainte4;
end
activeffect=exp([0;activeffect]);
%
Activity_effect=exp(sumainte4)
%
totaleffect=intenseffect.*structeffect.*activeffect;
%
Total_effect=Intensity_effect*Structural_effect*Activity_effect
end
%
%%%%
% PLOTS:
t=dates;
TTT=T*bins;
tt =[dates(1):(dates(n)-dates(1))/TTT:dates(n)];
%
SUBPLOT(2,4,1), plot(t,x,'*',tt,xt,'-')
title('Country energy consumptions')
axis tight
SUBPLOT(2,4,2), plot(t,y,'*',tt,yt,'-')
title('Country productions')
axis tight
legend('B','DK','D','EL','E','F','IRL','I','L','NL','A','P','FIN','S','UK')
lll=length(structeffect);
tt=dates(1):((dates(n)-dates(1))/(lll-1)):dates(n);
SUBPLOT(2,4,5), plot(tt,activeffect)
legend('Activity effect')
axis tight
SUBPLOT(2,4,6), plot(tt,structeffect)
title('Structural effect')
axis tight
SUBPLOT(2,4,7), plot(tt,intenseffect)
title('Intensity effect')
axis tight
SUBPLOT(2,4,8),
plot(tt,totaleffect)
title('Total effect')
axis tight
'COUNTRY ANALYSIS'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
t=[0:(1/T):1];
h = 1/TTT;
tt =[0:h:1];
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%DESCOMPOSICIÓN ADITIVA %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if tipo=='additiv'
%
disp('ADDITIVE DECOMPOSITION:')
E0=xtot(1)
E1=xtot(largo0)
variation=E1-E0
%
% 1) Intensity effect:
for j=1:numk
sumainte=0;
for i=1:largoderi
sumainte=sumainte+(ytot(i)+ytot(i+1))/2*difI(i,j)*(S(i,j)'+S(i+1,j)')/2*h;
end
Intensitysect(j)=sumainte;
end
%
% 2) Structural effect:
for j=1:numk
sumainte2=0;
for i=1:largoderi
sumainte2=sumainte2+(ytot(i)+ytot(i+1))/2*difS(i,j)*(I(i,j)'+I(i+1,j)')/2*h;
end
Structuralsect(j)=sumainte2;
end
%
% 3) Activity effect:
for j=1:numk
sumainte3=0;
for i=1:largoderi
sumainte3=sumainte3+difytot(i)*(S(i,j)+S(i+1,j))/2*(I(i,j)'+I(i+1,j)')/2*h;
end
Activitysect(j)=sumainte3;
end
%
Intensity_effect=sum(Intensitysect);
Structural_effect=sum(Structuralsect);
Activity_effect=sum(Activitysect);
Total_effect=Intensity_effect+Structural_effect+Activity_effect;
end
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MULTIPLICATIVE DECOMPOSITION DIS%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
if tipo=='multipl'
%
disp('MULTIPLICATIVE DECOMPOSITION:')
E0=xtot(1)
E1=xtot(largo0)
Ratio=E1/E0
%
% 1) Intensity effect:
for j=1:numk
sumainte1=0;
for i=1:largoderi
sumainte1=sumainte1+difI(i,j)*(S(i,j)'+S(i+1,j)')/2*(ytot(i)+ytot(i+1))/(xtot(i)+xtot(i+1))*h;
end
Intensitysect(j)=exp(sumainte1);
end
%
% 2) Structural effect:
for j=1:numk
sumainte3=0;
for i=1:largoderi
sumainte3=sumainte3+difS(i,j)*(I(i,j)'+I(i+1,j)')/2*(ytot(i)+ytot(i+1))/(xtot(i)+xtot(i+1))*h;
end
Structuralsect(j)=exp(sumainte3);
end
%
% 3) Activity effect:
for j=1:numk
sumainte4=0;
for i=1:largoderi
sumainte4=sumainte4+difytot(i)*(S(i,j)+S(i+1,j))/2*(I(i,j)'+I(i+1,j)')/(xtot(i)+xtot(i+1))*h;
end
Activitysect(j)=exp(sumainte4);
end
%
Intensity_effect=prod(Intensitysect);
Structural_effect=prod(Structuralsect);
Activity_effect=prod(Activitysect);
Total_effect=Intensity_effect*Structural_effect*Activity_effect;
end
%
disp('COUNTRY INTENSITY EFFECTS:')
disp('B DK D EL E F IRL I L NL A P FIN S UK')
disp(Intensitysect)
disp('COUNTRY STRUCTURAL EFFECTS:')
disp('B DK D EL E F IRL I L NL A P FIN S UK')
disp(Structuralsect)
disp('COUNTRY ACTIVITY EFFECTS:')
disp('B DK D EL E F IRL I L NL A P FIN S UK')
disp(Activitysect)
disp('OVERALL EFFECTS:')
Intensity_effect
Structural_effect
Activity_effect
Total_effect