-
Notifications
You must be signed in to change notification settings - Fork 4
/
payoff.m
106 lines (86 loc) · 4.85 KB
/
payoff.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
(* ::Package:: *)
(************************************************************************)
(* This file was generated automatically by the Mathematica front end. *)
(* It contains Initialization cells from a Notebook file, which *)
(* typically will have the same name as this file except ending in *)
(* ".nb" instead of ".m". *)
(* *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs. Doing so is equivalent *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end. *)
(* *)
(* DO NOT EDIT THIS FILE. This entire file is regenerated *)
(* automatically each time the parent Notebook file is saved in the *)
(* Mathematica front end. Any changes you make to this file will be *)
(* overwritten. *)
(************************************************************************)
(* ::Input::Initialization:: *)
Echo["Loaded payoff.m"];
(* ::Input::Initialization:: *)
ClearAll[Cx];
Cx::usage="Cx[n] creates a list of n variables named x1,x2,...,xn.";
Cx[n_]:=Table[Symbol["x"<>ToString[i]],{i,n}];
Information[Cx,LongForm->False]
(* ::Input::Initialization:: *)
ClearAll[payoff];
payoff::usage="payoff[m,i,j] returns the payoff of i-upstream and j-upstream in the m-market.
It is used when the streams of u and d agents are imported separately (i.e. not in the precomputed format). It is assumed that u , d , and noAttr have been already assigned.";
payoff[m_,i_,j_]:=(Prepend[Cx[noAttr-1],1]u[[m,i]]).d[[m,j]];
Information[payoff,LongForm->False]
(* ::Input::Initialization:: *)
ClearAll[payoffDM];(*DM from Distance Matrix*)
payoffDM::usage="payoffDM[m,i,j] returns the payoff of i-upstream and j-upstream in the m-market.\r
It is used in the case of precomputed data. It is assumed that noAttr and distanceMatrices have been already assigned.";
payoffDM[m_,i_,j_]:=Prepend[Cx[noAttr-1],1].distanceMatrices[[m,i,j]];
Information[payoffDM,LongForm->False]
(* ::Input::Initialization:: *)
(*C in front of the name means create*)
ClearAll[CpayoffMatrix];
CpayoffMatrix::usage="CpayoffMatrix[payoff(or payoffDM),noM_:noM,noU_:noU,noD_:noD,parallel_:False] calculates and assigns the payoffMatrix.\r
payoff is used when input data consist of separate u and d streams.\r
payoffDM is used for precomputed data.\r
CpayoffMatrix[solution_?VectorQ] substitutes the solution to all payoffMatrix's entries.";
CpayoffMatrix[payoff_,noM_:noM,noU_:noU,noD_:noD,p_:False]:=
payoffMatrix=
If[p==False,
Table[payoff[m,i,j]
,{m,1,noM},{i,noU[[m]]},{j,noD[[m]]}],
ParallelTable[payoff[m,i,j]
,{m,1,noM},{i,noU[[m]]},{j,noD[[m]]}]
]
CpayoffMatrix[solution_?VectorQ]:=
If[(Length@solution)===noAttr-1,
payoffMatrix=(payoffMatrix/.Thread[Cx[noAttr-1]->solution])
,
Print["There is some problem with your input. Couldn't calculate anything meaningful."]
];
Information[CpayoffMatrix,LongForm->False]
(* ::Input::Initialization:: *)
ClearAll[payoffMatrix2Positive];
SetAttributes[payoffMatrix2Positive,HoldFirst];
payoffMatrix2Positive::usage="payoffMatrix2Positive[offset,payoffMatrix,epsilon_:0,sameoffset_:False] translates the payoffMatrix subtracting an offset and adding an epsilon. This function's main purpose is to make all elements positive.
epsilon is the minimum element per market. It's default value is 0.
If the sameoffset flag is set to False (which is the default value) then each market will be handled separately related to its minimum.
If the sameoffset flag is set to True then the offset equals the minimum element in the entire payoffMatrix.";
payoffMatrix2Positive[offset_,payoffMatrix_?(And@@(NumericQ/@Flatten@#)&),epsilon_:0,sameoffset_:False]:=Block[{min},
If[sameoffset,
min=Min@Flatten@payoffMatrix;
offset=Table[min,{Length@payoffMatrix}]
,
offset=Min/@payoffMatrix
];
payoffMatrix-offset+epsilon
];
Information[payoffMatrix2Positive,LongForm->False]
(* ::Input::Initialization:: *)
(*C in front of the name means create*)
ClearAll[Ctotalpayoff];
Ctotalpayoff::usage="Ctotalpayoff[payoffobject,mates] calculates the total payoff (i.e. the sum of payoffs) across all markets for the specific mates arrangement. This function accepts as a first argument the \[OpenCurlyDoubleQuote]payffobject\[CloseCurlyDoubleQuote], which can either be the name of the payoff function or the payoffMatrix (in case we have already calculated all pair payoffs).";
Ctotalpayoff[payoffobject_,mates_]:=
totalpayoff=
Total/@Switch[Head@payoffobject,
(*payoff function*)Symbol,Map[payoffobject@@#&,mates,{2}],
(*payoffMatrix*)List,Map[Part@@Join[{payoffobject},#]&,mates,{2}]
];
Information[Ctotalpayoff,LongForm->False]