% MATLAB HEAD
clc, clear,
randn('seed', 5678) % reset normal random number generator
global N
N = 1000; % # of Observations
a=40; b=2;
%%%%%%%%%%%%%%%%%%%%%
% SIMULATE DATA
%%%%%%%%%%%%%%%%%%%%%
Q =a+b*randn(N,1); % Quantity
%%%%%%%%%%%%%%%%%%%%%%%%%%
% COMPUTING FOR ANALYSIS %
%%%%%%%%%%%%%%%%%%%%%%%%%%
%Let's create a couple of dummies
ud1 = randn(N,1);
% Brand 1 and 2
BRAND= 1+(ud1>0.15);
% Eleven Weeks!
% R = unidrnd(T) generates random numbers for the discrete uniform distribution with maximum T.
T=11;
WEEK= unidrnd( T*ones(N,1));
% Construct the Market Shares of X1
mean([ WEEK, BRAND]);
hist(WEEK)
hist(BRAND)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute a Matrix to Show how many observations you have per brand and per
% week with total column and row, i.e., (T+1 times 3) matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data1=zeros(T+1,3);
for n=1:N
for j=1:2
for t=1:T;
if BRAND(n,1)==j && WEEK(n,1)==t
data1(t,j)=data1(t,j)+1;
end
end
end
end
data1(1:T,3)= data1(1:T,1)+data1(1:T,2);
data1(T+1,:)= sum(data1(1:T,:));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data2=zeros(T+1,3);
for j=1:2
for i=1:T;
index= (((WEEK==i).*(BRAND==j)) ==1);
data2(i,j)= sum(index);
clear index
end
end
data2(:,3)= sum( data2(:,1:2),2);
data2(T+1,:)= sum( data2(1:T,:) ,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% From Quantities to Market Shares
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Q_T=sum(Q);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Market Shares per Brand
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Q_B1=sum(Q.*(BRAND==1));
Q_B2=sum(Q.*(BRAND==2));
MKS_1= Q_B1/Q_T;
MKS_2= Q_B2/Q_T;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Market Shares per Week
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Q_B1=sum(Q.*(BRAND==1));
Q_B2=sum(Q.*(BRAND==2));
MKS_1_weekly=zeros(T,1);
MKS_2_weekly=zeros(T,1);
for t=1:T
i1=(WEEK==t).*(BRAND==1);
i2=(WEEK==t).*(BRAND==2);
MKS_1_weekly(t,1)= sum(Q((i1==1)))./sum(Q((WEEK==t)));
MKS_2_weekly(t,1)= sum(Q((i2==1)))./sum(Q((WEEK==t)));
end
plot(MKS_1_weekly, '-diamond');
hold on;
plot(MKS_2_weekly ,'--o');
hold on;
h = legend('Market Share Brand 1','Market Share Brand 2',2);
set(gca,'XTick',1:5:T);
set(gca,'XTickLabel',{'Week1','Mid Week','Final Week'})
ylabel('[%]');
xlabel('Weeks from 1 to T');
title('Weekly Market Share by Brands.','FontSize',12);
hold off
filename = 'MKS_Weekly';
print('-deps', filename);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% WEIGHTED PRICES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Suppose Liner Demand
P=100-Q;
hist(P)
%%% CONSTRUCT WEEKLY PRICE INDEX USING WEEKLY MARKET SHARES AS WEIGHTS
P_Weekly = zeros(T,1);
for t=1:T
i =(WEEK==t);
weight=Q(i)/sum(Q(i));
Pwei = P(i).*weight;
P_Weekly(t,1)= sum( Pwei);
clear weight
end
% CONSTRUCT WEEKLY PRICE INDEX PER BRAND
% USING WEEKLY MARKET SHARES AS WEIGHTS
P_Weekly_byBrand = zeros(T,2);
for j=1:2
for t=1:T
k =( (WEEK==t).*(BRAND==j) )==1;
weight=Q(k)/sum(Q(k));
Pwei_bybrand = P(k).*weight;
P_Weekly_byBrand(t,j)= sum( Pwei_bybrand);
clear weight k Pwei_bybrand
end
end
plot(P_Weekly ,'-square');
hold on;
plot(P_Weekly_byBrand(:,1),'--diamond');
hold on;
plot(P_Weekly_byBrand(:,2) ,'--o');
hold on;
h = legend('Weekly weighted Price','Weekly weighted Price for Brand 1','Weekly weighted Price for Brand 2' ,4);
set(gca,'XTick',1:5:T);
set(gca,'XTickLabel',{'Week1','Mid Week','Final Week'})
ylabel('Price [$]');
xlabel('Weeks from 1 to T');
title('Weekly Weighted Price per Week and Brand','FontSize',12);
hold off
filename = 'Weighted_Price_Weekly';
print('-deps', filename);