-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraph.m
907 lines (831 loc) · 41.5 KB
/
graph.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% %%%%
%%%% This static Matlab class contains MATLAB functions generate %%%%
%%%% graphs ofr the leapfrogging paper %%%%
%%%% %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% %%%%
%%%% BY: Fedor Iskhakov, University Technology Sidney %%%%
%%%% John Rust, University of Maryland %%%%
%%%% Bertel Schjerning, University of Copenhagen %%%%
%%%% %%%%
%%%% THIS VERSION: March 2011 %%%%
%%%% %%%%
%%%% %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
classdef graph
methods (Static)
% solution: Draw solution at stage, iC of the game
function []=solution(g,iC, par, plottype, fighandle)
nC=par.nC;
cmin=par.cmin;
cmax=par.cmax;
if plottype ==1;
idx=[7:10 11:12];
else
idx=[7:8 18:19 11 12];
end
a=g(iC).solution;
% set min and max for axes
zmax=[];
zmin=[];
for i=1:nC-1
zmin(i)=floor(0.1*min(min(g(i).solution(:,idx(1:4)))))*10;
zmax(i)=floor(0.1*max(max(g(i).solution(:,idx(1:4)))))*10+10;
end
zmax=max(zmax);
zmin=min(zmin);
C1=a((a(:,3)==0),4);
C2=a((a(:,3)==0),5);
V10=a((a(:,13)==1),idx(1));
V10=reshape(V10,nC-iC+1,nC-iC+1);
V11=a((a(:,13)==1),idx(2)); V11=reshape(V11,nC-iC+1,nC-iC+1);
V20=a((a(:,13)==1),idx(3)); V20=reshape(V20,nC-iC+1,nC-iC+1);
V21=a((a(:,13)==1),idx(4)); V21=reshape(V21,nC-iC+1,nC-iC+1);
p1=a((a(:,13)==1),idx(5)); p1=reshape(p1,nC-iC+1,nC-iC+1);
p2=a((a(:,13)==1),idx(6)); p2=reshape(p2,nC-iC+1,nC-iC+1);
if plottype ==2;
p2=p2*0;
end
C1=reshape(C1,nC-iC+1,nC-iC+1);
C2=reshape(C2,nC-iC+1,nC-iC+1);
for i=1:6
if i==1;
z=V10;
tit='Value function, V10';
elseif i==2;
z=V11;
tit='Value function, V11';
elseif i==3;
z=p1;
tit='Investment probability, firm 1';
elseif i==4;
z=V20;
if plottype==1
tit='Value function, V20';
else
tit='Value function, X20';
end
elseif i==5;
z=V21;
if plottype==1
tit='Value function, V21';
else
tit='Value function, X21';
end
elseif i==6;
z=p2;
tit='Investment probability, firm 2';
end
axes1=subplot(2,3,i,'Parent',fighandle);
surf(C1,C2,z,'Parent',axes1);
title(tit);
xlabel('Marginal cost, firm 1, c1');
ylabel('Marginal cost, firm 2, c2');
if (i==3) || (i==6);
axis([cmin cmax cmin cmax 0 1]);
else
axis([cmin cmax cmin cmax zmin zmax]);
end;
end
drawnow;
end
% edges: Draw edges of solution at stage, iC of the game
function []=edges(g,iC, par)
nC=par.nC;
cmin=par.cmin;
cmax=par.cmax;
a=g(iC).solution;
% set min and max for axes
ymax=[];
ymin=[];
for i=1:nC-1
ymin(i)=floor(0.1*min(min(g(i).solution(:,7:10))))*10;
ymax(i)=floor(0.1*max(max(g(i).solution(:,7:10))))*10+10;
end
ymax=max(ymax);
ymin=min(ymin);
a=g(iC).solution;
% C1=a((a(:,3)==0),4);
% C1=reshape(C1,nC-iC+1,nC-iC+1);
C1=a((a(:,2)==0),4);
V_c1cc=a((a(:,2)==iC-1),7:10);
Vcc2c=a((a(:,1)==iC-1),7:10);
figure2 = figure('Color',[1 1 1],'NextPlot','new');
axes1=subplot (1,3,1,'Parent',figure2), plot(C1',V_c1cc,'Parent',axes1);
legend('v10(c1,0,0)', 'v11(c1,0,0)', 'v20(c1,0,0)', 'v21(c1,0,0)');
axis([cmin cmax ymin ymax]);
xlabel('Cost of firm 1, c1');
ylabel('Value function')
title('Endgame value function');
axes2=subplot (1,3,2), plot(C1',Vcc2c,'Parent',axes2);
legend('v10(0,c2,0)', 'v11(0,c2,0)', 'v20(0,c2,0)', 'v11(0,c2,0)');
axis([cmin cmax ymin ymax]);
xlabel('Cost of firm 2, c2');
ylabel('Value function')
title('Endgame value function');
a=g(iC).solution;
p=a((a(:,2)==iC-1),11:12);
axes3=subplot (1,3,3), plot(C1',p,'Parent',axes3);
legend('p1(c1,0,0)', 'p2(c1,0,0)');
axis([cmin cmax 0 1]);
xlabel('Cost of firm 1, c1');
ylabel('Value function')
title('Investment porbabilities, p1, p2');
drawnow;
end
function []=br(br, g, eta, c1,c2, iC, spacing);
brc=br(iC).br;
c=g(iC).c;
i=find(brc(:,3) == c1 & brc(:,4) == c2);
brc=brc(i,:);
if numel(brc)==0
error('graph.br2: passed c1,c2 are not found in br().br');
end
j=[1 1];
pvec=0.0001:spacing:1;
np=length(pvec);
p_i_vec=nan(2*np,2);
p_i_invbr_vec=nan(2*np,2);
for p_i=pvec;
for iF=1:2;
[p_i_invbr(1), p_i_invbr(2)]=fun.invbr(brc, eta, p_i, iF);
r=find((p_i_invbr~=nan(1,1)) & (p_i_invbr>=0) & (p_i_invbr<=1));
for ir=1:length(r)
p_i_vec(j(iF), iF)=p_i;
p_i_invbr_vec(j(iF), iF)=p_i_invbr(r(ir));
j(iF)=j(iF)+1;
end
end
end
plot(p_i_invbr_vec(1:j(1)-1,1),p_i_vec(1:j(1)-1,1),'*r','Linewidth',1); % inv(P1(P2)
hold on
plot(p_i_vec(1:j(2)-1,2),p_i_invbr_vec(1:j(2)-1,2),'*b','Linewidth',1); % P2(P1)
title(sprintf('Best Response Functions for Firm 1 and Firm 2 at (c_1,c_2,c)=(%3.1f,%3.1f,%3.1f) and eta=%3.2f',c1,c2,c, eta));
xlabel('Probability Firm 2 Invests');
ylabel('Probability Firm 1 Invests');
axis([0 1 0 1]);
legend('P_1(P_2), Firm 1 coefs','P_2(P_1), Firm 2 coefs', 'Location','East');
hold off;
end
function num_equilibria=br0eta(br,g,ic1,ic2,iC,ax);
% This function makes the plot of two best response
% functions for the two firms on the unit square
% INPUTS: solution br, g
% where best responses should be plotted
% ic1,ic2,iC - indices of the point in the state, base1 (between 1=bottom and nC=apex)
%
% br - solution data for the interior points
if ic1<iC | ic2<iC
error 'Wrong call: expecting ic1>=iC, ic2>=iC'
end
% compute by firms
for iF=1:2
PiF{iF}=[];
PjF{iF}=[];
if ic1==iC | ic2==iC
%corners and edges
if iC==ic1 && iC==ic2
%corner
PjF{iF}(1)=0; % x
PjF{iF}(2)=1; % x
PiF{iF}(1)=0; % y
PiF{iF}(2)=0; % y
else
%edge
if iC==1
%bottom layer: 0 invesment
PjF{iF}(1)=0; % x
PjF{iF}(2)=1; % x
PiF{iF}(1)=0;
PiF{iF}(2)=0;
else
%higher layers: need to compare values for one of the firms
if (iF==1 & ic1==iC) | (iF==2 & ic2==iC)
PjF{iF}(1)=0; % x
PjF{iF}(2)=1; % x
PiF{iF}(1)=0;
PiF{iF}(2)=0;
else
i=find(g(iC).solution(:,1) == ic1-1 & g(iC).solution(:,2) == ic2-1); %base0
% columns in g: 7,8 firm 1 N,I, 9,10 firm 2 N,I values
if (iF==1 & g(iC).solution(i,7)>g(iC).solution(i,8)) | ...
(iF==2 & g(iC).solution(i,9)>g(iC).solution(i,10))
%non investing is better
PjF{iF}(1)=0; % x
PjF{iF}(2)=1; % x
PiF{iF}(1)=0;
PiF{iF}(2)=0;
else
PjF{iF}(1)=0; % x
PjF{iF}(2)=1; % x
PiF{iF}(1)=1;
PiF{iF}(2)=1;
end
end
end
end
else
%interior points
brc=br(iC).br;
c=g(iC).c;
i=find(brc(:,1) == ic1-1 & brc(:,2) == ic2-1); %base0 in br(iC).br
brc=brc(i,:);
%coefficients of the quadratic form of the best responce
a0=brc(14+iF-1); %constant
a1=brc(16+iF-1); %linear
a2=brc(18+iF-1); %quadratic in opponents investment probability
%start making the best correspondence correspondence
k=1;
PjF{iF}(k)=0; %x-axes value
if a2>0
PiF{iF}(k)=0; %with positive quadratic coef start at certain investment
else
PiF{iF}(k)=1; %otherwise with certain no investment
end
%solve for the roots for the best response correspondence
D=a1^2-4*a0*a2;
if D<0
%negative discriminant ==> no roots
%finish the line at the same value
PjF{iF}(k+1)=1;
PiF{iF}(k+1)=PiF{iF}(k);
k=k+1;%total points
else
%roots
r1 =-(1/(2*a2))*(a1-sqrt(D));
r2 =-(1/(2*a2))*(a1+sqrt(D));
%sorted roots
root(1)=min(r1,r2);
root(2)=max(r1,r2);
% combine the roots into the lines
for ir=1:2
if root(ir)<0
%root before unit interval --> replace the point from 1 to 0 and vise versa
PiF{iF}(k)=1-PiF{iF}(k);
elseif root(ir)>=0 & root(ir) <=1
%root inside of unit interval --> make vertical line
PjF{iF}(k+1:k+2)=[root(ir) root(ir)];
PiF{iF}(k+1:k+2)=[PiF{iF}(k) 1-PiF{iF}(k)];
k=k+2;
else
%root after unit interval --> ignore
end
end
%last point
PjF{iF}(k+1)=1;
PiF{iF}(k+1)=PiF{iF}(k);
k=k+1;%total points
end
end
end
% Onto the best response function overlay the computed equilibria
% In addition to making nice graph, this makes the visual check of correctness
i=find(g(iC).solution(:,1)==ic1-1 & g(iC).solution(:,2)==ic2-1);
eqbs=g(iC).solution(i,[11 12]);
% plot
if ~exist('ax')
fig1=figure('Color',[1 1 1],'NextPlot','new');
ax=axes('Parent',fig1);
linethick=5;
fontsize=24;
dotsize=120;
else
linethick=2;
fontsize=12;
dotsize=40;
end
plot(ax,PjF{1},PiF{1},'Linewidth',linethick,'Color','red');% best response of firm 1
hold(ax,'on');
plot(ax,PiF{2},PjF{2},'Linewidth',linethick,'Color','black','LineStyle','--');% best response of firm 2, swapping axes
hold(ax,'on');
scatter(eqbs(:,2),eqbs(:,1),dotsize,[0 0 1],'filled','Linewidth',linethick,'MarkerFaceColor','white','MarkerEdgeColor','black');
% title(ax,'Best response functions for firm 1 (solid) and firm 2 (dashed)', ...
% 'FontSize',fontsize);
set(ax,'XTick',[0 .5 1],'XTickLabel',{'0','','1'}, ...
'YTick',[0 .5 1],'YTickLabel',{'0','','1'}, ...
'XLim',[-.01 1.01],'YLim',[-.01,1.01],'box','off', ...
'TickDir','out','DataAspectRatio',[1 1 1], ...
'PlotBoxAspectRatio',[1 1 1],'FontSize',fontsize,...
'Linewidth',linethick/2);
xlabel(ax,'P2','FontSize',fontsize);
ylabel(ax,'P1','FontSize',fontsize);
hold(ax,'off');
end
function []=all_stage_equilibria(par,br,g)
%plot all stage equilibria
%top 5 layers in one subplot, other layers in their own
%map of subplots: ic, ic1, ic2 (base 1) = [raw, column]
spmap{5,5,5}=[1 1]; %apex in the left top corner
spmap{4,4,4}=[2 2];spmap{4,4,5}=[1 2];spmap{4,5,4}=[2 3];spmap{4,5,5}=[1 3];spmap{3,3,3}=[3 4];spmap{3,3,4}=[2 4];spmap{3,3,5}=[1 4];spmap{3,4,3}=[3 5];spmap{3,4,4}=[2 5];spmap{3,4,5}=[1 5];spmap{3,5,3}=[3 6];spmap{3,5,4}=[2 6];spmap{3,5,5}=[1 6];spmap{2,2,2}=[4 7];spmap{2,2,3}=[3 7];spmap{2,2,4}=[2 7];spmap{2,2,5}=[1 7];spmap{2,3,2}=[4 8];spmap{2,3,3}=[3 8];spmap{2,3,4}=[2 8];spmap{2,3,5}=[1 8];spmap{2,4,2}=[4 9];spmap{2,4,3}=[3 9];spmap{2,4,4}=[2 9];spmap{2,4,5}=[1 9];spmap{2,5,2}=[4 10];spmap{2,5,3}=[3 10];spmap{2,5,4}=[2 10];spmap{2,5,5}=[1 10];spmap{1,1,1}=[5 11];spmap{1,1,2}=[4 11];spmap{1,1,3}=[3 11];spmap{1,1,4}=[2 11];spmap{1,1,5}=[1 11];spmap{1,2,1}=[5 12];spmap{1,2,2}=[4 12];spmap{1,2,3}=[3 12];spmap{1,2,4}=[2 12];spmap{1,2,5}=[1 12];spmap{1,3,1}=[5 13];spmap{1,3,2}=[4 13];spmap{1,3,3}=[3 13];spmap{1,3,4}=[2 13];spmap{1,3,5}=[1 13];spmap{1,4,1}=[5 14];spmap{1,4,2}=[4 14];spmap{1,4,3}=[3 14];spmap{1,4,4}=[2 14];spmap{1,4,5}=[1 14];spmap{1,5,1}=[5 15];spmap{1,5,2}=[4 15];spmap{1,5,3}=[3 15];spmap{1,5,4}=[2 15];spmap{1,5,5}=[1 15];
crr=5-par.nC;%correction to allow for using the same spmap for smaller that nC=5 problems
sprows=min(5,par.nC); %rows equal to max number of c (for bottom layer)
spcols=sum(1:sprows); %cols to fit all layers
%loop through all stages
for ic=max(1,par.nC-4-5):par.nC %limit to 5 figures
if ic<=par.nC-5+1 | ~exist('fig1')
position=get(0,'ScreenSize');
position([1 2])=0;
fig1=figure('Color',[1 1 1],'Position',position);
end
for ic1=ic:par.nC
for ic2=ic:par.nC
if par.nC>5 & ic<par.nC-5+1
mic=par.nC-ic+1;
spindx=(mic-(ic2-ic)-1)*mic+(ic1-ic+1);
ax=subplot(mic,mic,spindx,'Parent',fig1);
graph.br0eta(br,g,ic1,ic2,ic,ax);
% title(ax,sprintf('ic=%d ic1=%d ic2=%d',ic,ic1,ic2));
title(ax,sprintf('%d %d%d',ic,ic1,ic2));
drawnow
else
spindx=(spmap{ic+crr,ic1+crr,ic2+crr}(1)-1)*spcols+spmap{ic+crr,ic1+crr,ic2+crr}(2);
ax=subplot(sprows,spcols,spindx,'Parent',fig1);
graph.br0eta(br,g,ic1,ic2,ic,ax);
% title(ax,sprintf('ic=%d ic1=%d ic2=%d',ic,ic1,ic2));
title(ax,sprintf('%d %d%d',ic,ic1,ic2));
drawnow
end
end
end
end
end
function []=br2(br, g, eta, c1,c2,iC,iF, spacing);
brc=br(iC).br;
c=g(iC).c;
i=find(brc(:,3) == c1 & brc(:,4) == c2);
brc=brc(i,:);
if numel(brc)==0
error('graph.br2: passed c1,c2 are not found in br().br');
end
[pmin, pmax]=fun.FindMinMaxBr(brc, eta, iF);
j=1;
pvec=pmin:spacing:pmax;
np=length(pvec);
p_i_vec=nan(4*np,1);
p_i_invbr2_vec=nan(4*np,1);
for p_i=pvec;
p_i_invbr2=fun.invbr2(brc, eta, p_i, iF);
r=find((imag(p_i_invbr2)==0) & (p_i_invbr2>=0) & (p_i_invbr2<=1));
for ir=1:length(r)
p_i_vec(j)=p_i;
p_i_invbr2_vec(j)=p_i_invbr2(r(ir));
j=j+1;
end
end
plot(p_i_invbr2_vec(1:j-1),p_i_vec(1:j-1),'*r','Linewidth',1);
hold on
plot(0:0.1:1,0:0.1:1,'--m','Linewidth',2);
title(sprintf('Second Order Best Response Function for Firm %d (c_1,c_2,c)=(%3.1f,%3.1f,%3.1f) and eta=%3.2f',iF, c1,c2,c, eta));
xlabel(sprintf('Probability Firm %d Invests', iF));
ylabel(sprintf('Probability Firm %d Invests', iF));
axis([0 1 0 1]);
hold off;
end
function []=br2byroots(br, g, eta, c1,c2,iC,iF, spacing);
r1=[1;-1;-1;1];
r2=[1;1;-1;-1];
brc=br(iC).br;
c=g(iC).c;
i=find(brc(:,3) == c1 & brc(:,4) == c2);
brc=brc(i,:);
[pmin, pmax]=fun.FindMinMaxBr(brc, eta, iF);
j=1;
pvec=pmin:spacing:pmax;
np=length(pvec);
invbr2_vec=nan(np,4);
for r=1:4;
for iP=1:np;
invbr2(iP,:)=fun.invbr2byroot(brc, eta, pvec(iP), iF, r);
end
% r=find((imag(invbr2(:,ir))==0) & (invbr2(:,ir)>=0) & (invbr2(:,ir)<=1));
i=find(isnan(invbr2)==0);
if r==1;
pi1=invbr2(i);
pj1=pvec(i);
elseif r==2;
pi2=invbr2(i);
pj2=pvec(i);
elseif r==3;
pi3=invbr2(i);
pj3=pvec(i);
elseif r==4;
pi4=invbr2(i);
pj4=pvec(i);
end
end
plot(pi1,pj1,'-*r','Linewidth',1);
hold on
plot(pi2,pj2,'-*b','Linewidth',1);
plot(pi3,pj3,'-*k','Linewidth',1);
plot(pi4,pj4,'-*m','Linewidth',1);
plot(0:0.1:1,0:0.1:1,'--g','Linewidth',2);
title(sprintf('Second Order Best Response Function for Firm %d (c_1,c_2,c)=(%3.1f,%3.1f,%3.1f) and eta=%3.2f',iF, c1,c2,c, eta));
xlabel(sprintf('Probability Firm %d Invests', iF));
ylabel(sprintf('Probability Firm %d Invests', iF));
axis([0 1 0 1]);
legend(sprintf('[%d,%d]',r1(1),r2(1)),sprintf('[%d,%d]',r1(2),r2(2)), ...
sprintf('[%d,%d]',r1(3),r2(3)),sprintf('[%d,%d]',r1(4),r2(4)), ...
'45 degree line','Location','SouthEast');
hold off;
end
function []=CostSequence(s, mp, tit,varargin)
fig=figure('name','Realized sequence: costs','NextPlot','new','Color',[1 1 1]);
axes1=axes('Parent',fig,'FontSize',14,'FontName','Times New Roman');
hold on;
%stairs(s.t,s.c1,'r','LineWidth',3);
%stairs(s.t,s.c2,'c','LineWidth',3);
%stairs(s.t,s.c,'--k','LineWidth',2);
if nargin <= 4
pl=plot(s.t,s.c1,s.t,s.c2,'Parent',axes1,'MarkerFaceColor',[1 1 1],'LineWidth',2);
else
c1=s.c2;
pl=plot(s.t,s.c1,s.t,s.c2,s.t,s.cmon,'Parent',axes1,'MarkerFaceColor',[1 1 1],'LineWidth',2);
end
set(pl(1),'MarkerSize',4,'Marker','o','Color',[0 0.5 0],'DisplayName','Firm 1 cost (c_1)');
set(pl(2),'MarkerSize',3,'Marker','square','Color',[1 0 0],'DisplayName','Firm 2 cost (c_2)');
stairs(s.t,s.c,'Parent',axes1,'LineWidth',2,'DisplayName','state of the art cost (c)','Color',[0 0 0]);
if nargin > 4
set(pl(3),'MarkerSize',3, 'Marker','square','Color',[0 0 1],'DisplayName','Monopolist');
lg=legend('c_1','c_2','c_{monopoly}','c', 'Location','NorthEast');
else
lg=legend('c_1','c_2','c', 'Location','NorthEast');
end
if nargin > 3
if strcmp(varargin(1),'');
title({tit});
else
title({tit varargin{1}});
end
else
title({tit sprintf('sigma=%g, eta=%g, k1=%g, k2=%g, dt=%g, beta=%g',mp.sigma, mp.eta, mp.k1, mp.k2,mp.dt,mp.df)});
end
xlabel('Time');
ylabel('Marginal Costs, Prices');
set(lg,'Box','off');
ylm=get(axes1,'Ylim');
set(axes1,'YLim',[ylm(1) ylm(2)+0.1]);
box(axes1,'on');
hold off;
end
function []=ProfitSequence(s, mp, tit)
fig=figure('name','Realized sequence: Prices');
hold on;
stairs(s.t,s.pf1,'r','LineWidth',3);
stairs(s.t,s.pf2,'c','LineWidth',3);
stairs(s.t,s.c,'--k','LineWidth',2);
title({tit sprintf('sigma=%g, eta=%g, k1=%g, k2=%g, beta=%g',mp.sigma, mp.eta, mp.k1, mp.k2, mp.df)});
xlabel('Time');
ylabel('Marginal Costs, Profits');
legend('pi_1','pi_2','c','Location','Best');
hold off;
end
function [statistics labels]=EqbstrCDFPlot(eqbstr,vars,dummystr,dummylbl, mp,sw,tit,xlow,varargin)
% Plots the CDF of distribution of efficiency of equilibria in eqbstrings
% Assumes: eqbstring(1) - lexicographical index
% eqbstring(2) - count of repeated equilibria
% eqbstring(3) - value for x axis
% eqbstring(4) - value for y axis
% eqbstring(5,6,..) - additional statistics
% Inputs: eqbstring - as outputed by solver
% vars = 1xn vector of indices of the columns of eqbstring to condition on.
% n CDFs are plotted - one for each element in vars
% if vars(i)= 0 , CDF of all equilibria are shown
% if vars(i)< 0 , CDF of equilibria where eqbstr(:,abs(vars(i)))=1
% if vars(i)> 0 , CDF of equilibria where eqbstr(:,vars(i))=0
% sw - parameters as input for solver
% mp - same
% tit - title for the graph
% optional handle for the figure where graphs should be
% Output: statistics on the found equilibria
% cell array of some labels for the statistics
%do statistics first
labels={'total number of equilibria';'number of distinct pay-offs';'pure strategy';'symmetric';'leapfrog';'efficiency';'underinvest'};
title3={'','number of repetitions','value of firm 1','value of firm 2',labels{3:end}};
labels_n={'total number of equilibria';'number of distinct pay-offs';'mixed strategy';'asymmetric';'non-leapfrogging';'efficiency'};
title3_n={'','number of repetitions','value of firm 1','value of firm 2',labels_n{3:end}};
n=numel(vars);
nd=size(dummylbl,2);
if nd>0;
dummy=eval(dummystr);
end
legendtxt='';
for i=1:n+nd;
if i<=n
if vars(i)==0;
mask=ones(size(eqbstr,1),1)==1;
else
if vars(i)<0
mask=eqbstr(:,abs(vars(i)))==0;
else
mask=eqbstr(:,vars(i))==1;
end
end
else
mask=dummy(:,i-n)==1;
end
seqbstr=sortrows(eqbstr(mask,:),8);
statistics(i,:) = [sum(seqbstr(:,2)) size(unique(seqbstr(:,3:4),'rows'),1) seqbstr(:,2)'*seqbstr(:,5:end)];
if sw.alternate==1
%account for symmetric points
statistics(i,:)=statistics(i,:)*2;
end
if ~isempty(seqbstr)
Ni=[0; seqbstr(:,2)];
Xi=[seqbstr(1,8); seqbstr(:,8)];
else
Ni=nan(1,1);
Xi=nan(1,1);
end
%plot
if i==1;
if nargin>8 & ishandle(varargin{1})
figure1=varargin{1};
else
scrsz = get(0,'ScreenSize');
%figure1 = figure('Color',[1 1 1],'NextPlot','new');
% figure1 =figure('Color',[1 1 1],'NextPlot','replacechildren','Position',[scrsz(3)*(1-1/1.5)/2 scrsz(4)*(1-1/1.5)/2 scrsz(3)/2 scrsz(4)/2]);
figure1 =figure('Color',[1 1 1],'NextPlot','replacechildren');
end
axes1 = axes('Parent',figure1,'FontSize',14,'FontName','Times New Roman',...
'YGrid','on',...
'YColor',[0.25 0.25 0.25],...
'XGrid','on',...
'XColor',[0.25 0.25 0.25],...
'PlotBoxAspectRatio',[1 1 1]);
colormap('jet');
box(axes1,'on');
hold(axes1,'all');
end
% stairs(Xi, Ni)
if i<=n;
if vars(i)==0
tmp=stairs(Xi, cumsum(Ni)/sum(Ni),'LineWidth',2);
set(tmp,'DisplayName', 'all equilibria');
else
if vars(i)<0
tmp=stairs(Xi, cumsum(Ni)/sum(Ni),'LineWidth',1);
set(tmp,'DisplayName', title3_n{abs(vars(i))});
else
tmp=stairs(Xi, cumsum(Ni)/sum(Ni),'LineWidth',1);
set(tmp,'DisplayName', title3{vars(i)});
end
end
else
tmp=stairs(Xi, cumsum(Ni)/sum(Ni),'LineWidth',1);
set(tmp,'DisplayName', dummylbl{i-n});
end
end
xlim=get(axes1,'XLim');
xlim(1)=min(xlow, xlim(1));
xlim(2)=1;
set(axes1,'XLim',xlim);
set(axes1,'YLim',[0 1]);
legend1=legend('show');
set(legend1, 'Location','West');
set(legend1, 'EdgeColor', [1 1 1]);
set(legend1, 'Color', [1 1 1])
title2='';
for i=1:n+nd;
if i<=n;
if vars(i)==0
lbl='equilibria'
elseif vars(i)<0
lbl=title3_n{abs(vars(i))};
else
lbl=title3{(vars(i))};
end
else
lbl=sprintf('%s', dummylbl{i-n});
end
if i<n+nd;
title2=sprintf('%s %1.0d %s, ',title2, statistics(i, 1), lbl );
else
title2=sprintf('%s %1.0d %s',title2, statistics(i, 1), lbl );
end
end
%2 title and labels
if sw.alternate==0
if isempty(mp)
title(axes1,{tit ...
title2 ...
});
else
title(axes1,{tit ...
title2 ...
sprintf('simultanious move, n=%d, sigma=%g, eta=%g, k1=%g, k2=%g, beta=%g, tr=%g, B=(%g;%g)',mp.nC,mp.sigma, mp.eta, mp.k1, mp.k2, mp.df,mp.c_tr,mp.beta_a,mp.beta_b) ...
});
end
else
if isempty(mp)
title(axes1,{tit ...
title2 ...
});
else
title(axes1,{tit ...
title2 ...
sprintf('alternating move, n=%d, sigma=%g, eta=%g, k1=%g, k2=%g, beta=%g, tr=%g, B=(%g;%g), tpm11=%g tpm22=%g',mp.nC,mp.sigma, mp.eta, mp.k1, mp.k2, mp.df,mp.c_tr,mp.beta_a,mp.beta_b, mp.tpm(1,1),mp.tpm(2,2)) ...
});
end
end
end
function [statistics labels]=EqbstrPlot(eqbstr,vars,monopoly,mp,sw,tit,varargin)
% Plots the scatterplot of the equilibrium realizations in eqbstrings
% Assumes: eqbstring(1) - lexicographical index
% eqbstring(2) - count of repeated equilibria
% eqbstring(3) - value for x axis
% eqbstring(4) - value for y axis
% eqbstring(5,6,..) - additional statistics
% Inputs: eqbstring - as outputed by solver
% vars = [i j] the columns of eqbstring to be used for size and color
% if only size is given, b/w coloring is produced
% sw - parameters as input for solver
% mp - same
% tit - title for the graph
% optional handle for the figure where graphs should be
% Output: statistics on the found equilibria
% cell array of some labels for the statistics
%do statistics first
labels={'total number of equilibria';'number of distinct pay-offs';'pure strategy equilibria';'symmetric equilibria';'leapfrogging equilibria';'efficiency'};
title3={'','number of repetitions','value of firm 1','value of firm 2',labels{3:end}};
statistics = [sum(eqbstr(:,2)) size(unique(eqbstr(:,3:4),'rows'),1) eqbstr(:,2)'*eqbstr(:,5:end)];
%do the graph
%1 prepare data and plot
if sw.alternate==1
%add symmetric points
eqbstr=[eqbstr;eqbstr(:,[1 2 4 3 5:end])];
statistics=statistics*2;
end
%make better look if number of point is not too large
if size(eqbstr,1)<100000 && ~isempty(vars)
%reduce some repetitions
if (numel(vars)>1 && vars(1)==2 && vars(2)==2)
%if size and color are nr of repetitions
eqbstr=groupdata(eqbstr,[3 4],2);
elseif (numel(vars)>1 && vars(1)==2 && vars(2)~=2)
%if size is nr of repetitions
eqbstr=groupdata(eqbstr,[3 4 vars(2)],2);
elseif (numel(vars)==1 && vars(1)==2)
%if size is nr of repetitions
eqbstr=groupdata(eqbstr,[3 4],2);
end
%sort descending by size and so that bubbles are overlaping from top to bottom
eqbstr=sortrows(eqbstr,-vars(1));
end
%color/BW
if isempty(vars) || (numel(vars)==1 && vars(1)<=size(eqbstr,2))
%BW case
if ~isempty(vars)
title3str=sprintf('Size: %s',title3{vars(1)});
else
title3str='';
end
if ~isempty(vars)
mn=min(eqbstr(:,vars(1)));
mx=max(eqbstr(:,vars(1)));
if mn==mx || isempty(vars)
warning 'Chosen data column does not have any variation, the size of dots is set to default'
if size(eqbstr,1)<100000
sizes=ones(size(eqbstr,1),1)*10;
else
sizes=ones(size(eqbstr,1),1)*2;
end
else
sizes=(eqbstr(:,vars(1))-mn)*790/(mx-mn)+8;%rescale to 10 to 700
end
else
sizes=ones(size(eqbstr,1),1)*3;
end
%plot
if nargin>6 && ishandle(varargin{1})
figure1=varargin{1};
else
figure1 = figure('Color',[1 1 1],'NextPlot','new');
end
axes1 = axes('Parent',figure1,'PlotBoxAspectRatio',[1 1 1],'FontSize',16,'FontName','Times New Roman');
hold(axes1,'all');
scatter(eqbstr(:,3),eqbstr(:,4),sizes,'MarkerFaceColor',[1 1 1],'MarkerEdgeColor',[0 0 0],'Parent',axes1);
if ~isempty(monopoly)
epsi=monopoly*0.003;%gap in the monopoly line
line([epsi monopoly-epsi],[monopoly-epsi epsi],'Parent',axes1,'LineWidth',1,'LineStyle','--','Color',[0.25 0.25 0.25]);
set(axes1,'XLim',[0 monopoly]);
set(axes1,'YLim',[0 monopoly]);
set(axes1,'XTick',[get(axes1,'XTick') monopoly]);
set(axes1,'YTick',[get(axes1,'YTick') monopoly]);
a=get(axes1,'XTickLabel');
a(end-1,:)=' ';
set(axes1,'XTickLabel',a);
else
maxx=max(get(axes1,'XLim'));
maxy=max(get(axes1,'YLim'));
set(axes1,'XLim',[0 min([maxx maxy])]);
set(axes1,'YLim',[0 min([maxx maxy])]);
end
elseif numel(vars)==2 && vars(1)<=size(eqbstr,2) && vars(2)<=size(eqbstr,2)
%color case
title3str=[sprintf('Size: %s',title3{vars(1)}) ' ' sprintf('Color: %s',title3{vars(2)})];
mn=min(eqbstr(:,vars(1)));
mx=max(eqbstr(:,vars(1)));
if mn==mx || isempty(vars)
warning 'First chosen column does not have any variation, the size of dots is set to default'
if size(eqbstr,1)<100000
sizes=ones(size(eqbstr,1),1)*25;
else
sizes=ones(size(eqbstr,1),1)*5;
end
else
sizes=(eqbstr(:,vars(1))-mn)*675/(mx-mn)+25;%rescale to 25 to 700
end
colors=eqbstr(:,vars(2));
%plot
if nargin>6 && ishandle(varargin{1})
figure1=varargin{1};
else
figure1 = figure('Color',[1 1 1],'NextPlot','new');
end
axes1 = axes('Parent',figure1,'FontSize',14,'FontName','Times New Roman',...
'YGrid','on',...
'YColor',[0.25 0.25 0.25],...
'XGrid','on',...
'XColor',[0.25 0.25 0.25],...
'PlotBoxAspectRatio',[1 1 1]);
if nargin>7 && isnumeric(varargin{2})
set(axes1,'CLim',[varargin{2}(1) varargin{2}(2)]);
end
colormap('jet');
box(axes1,'on');
hold(axes1,'all');
scatter(eqbstr(:,3),eqbstr(:,4),sizes,colors,'MarkerFaceColor','flat','MarkerEdgeColor',[0 0 0],'Parent',axes1);
colorbar('peer',axes1);
if ~isempty(monopoly)
epsi=0;%gap in the monopoly line
line([epsi monopoly-epsi],[monopoly-epsi epsi],'Parent',axes1,'LineWidth',1,'LineStyle','-','Color',[0.25 0.25 0.25]);
set(axes1,'XLim',[0 monopoly]);
ticks=cellstr(get(axes1,'XTickLabel'));
ticks{end}=' ';
ticks{end+1}=sprintf('%4.3f',monopoly);
set(axes1,'XTick',[get(axes1,'XTick') monopoly]);
set(axes1,'XTickLabel',ticks);
%copy this to Y
set(axes1,'YLim',get(axes1,'XLim'));
set(axes1,'YTick',get(axes1,'XTick'));
set(axes1,'YTickLabel',get(axes1,'XTickLabel'));
end
maxx=max(get(axes1,'XLim'));
maxy=max(get(axes1,'YLim'));
set(axes1,'XLim',[0 min(maxx,maxy)]);
set(axes1,'YLim',[0 min(maxx,maxy)]);
else
error 'Unknown value of second argument (vars), need column numbers in eqbstring to be used as size and color in scatter'
end
%2 title and labels
if sw.alternate==0
if isempty(mp)
title(axes1,{tit ...
sprintf('%1.0d equilibria, %1.0d distinct pay-off points',statistics(1),statistics(2)) ...
title3str ...
});
else
title(axes1,{tit ...
sprintf('%1.0d equilibria, %1.0d distinct pay-off points',statistics(1),statistics(2)) ...
sprintf('simultanious move, n=%d, sigma=%g, eta=%g, k1=%g, k2=%g, beta=%g, tr=%g, B=(%g;%g)',mp.nC,mp.sigma, mp.eta, mp.k1, mp.k2, mp.df,mp.c_tr,mp.beta_a,mp.beta_b) ...
title3str ...
});
end
else
if isempty(mp)
title(axes1,{tit ...
sprintf('%1.0d equilibria, %1.0d distinct pay-off points',statistics(1),statistics(2)) ...
title3str ...
});
else
title(axes1,{tit ...
sprintf('%1.0d equilibria, %1.0d distinct pay-off points',statistics(1),statistics(2)) ...
sprintf('alternating move, n=%d, sigma=%g, eta=%g, k1=%g, k2=%g, beta=%g, tr=%g, B=(%g;%g), tpm11=%g tpm22=%g',mp.nC,mp.sigma, mp.eta, mp.k1, mp.k2, mp.df,mp.c_tr,mp.beta_a,mp.beta_b, mp.tpm(1,1),mp.tpm(2,2)) ...
title3str ...
});
end
end
end
end
end
function res = groupdata (data, groupby, reduce)
% This function reduces the data in rows by grouping over given colums and perfoming operation on the given columns.
% Inputs: data - matrix with dara in rows, columns are variables, rows are observations
% groupby - vector of indeces of variables to be used for grouping
% reduce - vector of indeces of variables to be reduced using operation within each group
% operation - pointer to function operating on columns (like sum or max)
% 1 find unique combinations of groupby columns in the data
[out m n]=unique(data(:,groupby),'rows','first');
% m is res=data(m,:); n is data=res(n,:);
% 2 construct result
res=data(m,:);
% 3 perform operation
for i=1:size(res,1)
if sum((n==i))==1
res(i,reduce)=data((n==i),reduce);
else
res(i,reduce)=feval('sum',data((n==i),reduce));
end
end
end %function