SGSolve
sgrisksharing.hpp
1 // This file is part of the SGSolve library for stochastic games
2 // Copyright (C) 2019 Benjamin A. Brooks
3 //
4 // SGSolve free software: you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // SGSolve is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see
16 // <http://www.gnu.org/licenses/>.
17 //
18 // Benjamin A. Brooks
19 // ben@benjaminbrooks.net
20 // Chicago, IL
21 
22 #ifndef _SGRISKSHARING_HPP
23 #define _SGRISKSHARING_HPP
24 
25 #include "sg.hpp"
26 
29 {
30 public:
31  enum EndowmentMode { Consumption, Endowment};
32 private:
33  int numEndowments;
34  int c2e;
35  double persistence;
36  vector<double> E;
37  int midPoint;
38  double eIncr;
39  double cIncr;
40  vector< vector<double> > stateProbSum;
41  vector<int> numActions_total;
42  EndowmentMode endowmentMode;
43 
44  double consumption(int e, int t) const
45  {
46  assert(E[e]+t*cIncr>=-1e-6);
47  assert(E[e]+t*cIncr<=1+1e-6);
48  double cons = E[e]+t*cIncr;
49  if (cons < 0)
50  return 0;
51  if (cons > 1)
52  return 1;
53  return cons;
54  }
55 
56  double cdf(double x) const
57  {
58  if (persistence>0)
59  return 1-exp(-persistence*max(0.0,x));
60  else
61  return x;
62  }
63 
64  double probHelper(int e, int t, int ep) const
65  {
66  double mode;
67  switch (endowmentMode)
68  {
69  case Consumption:
70  mode = consumption(e,t);
71  break;
72  case Endowment:
73  mode = E[e];
74  break;
75  }
76 
77  return ( cdf(E[ep]-mode+eIncr) - cdf(E[ep]-mode-eIncr)
78  + cdf(mode-E[ep]+eIncr) - cdf(mode-E[ep]-eIncr) );
79  } // probHelper
80 
81 public:
82  RiskSharingGame(double delta,
83  int _numEndowments,
84  int _c2e,
85  double _persistence,
86  EndowmentMode _mode):
87  numEndowments(_numEndowments),
88  c2e(_c2e),
89  persistence(_persistence),
90  E(numEndowments,0),
91  midPoint((_numEndowments-1)/2),
92  stateProbSum(_numEndowments),
93  numActions_total(_numEndowments,1),
95  _numEndowments,
96  vector< vector<int> > (_numEndowments,vector<int>(2,1))),
97  endowmentMode(_mode)
98  {
99  // // A couple of checks on the number of endowments
100  // assert( (numEndowments%2) == 1 );
101  // assert( numEndowments>2 );
102 
103  // Set up the endowment grid and the numbers of actions
104  for (int e = 0; e < numEndowments; e++)
105  {
106  E[e] = (1.0*e)/(numEndowments-1); // player 0's endowment
107 
108  numActions[e][0] = e*c2e+1;
109  numActions[e][1] = (numEndowments-e-1)*c2e+1;
110  numActions_total[e] = numActions[e][0]*numActions[e][1];
111  } // for e
112 
113  eIncr = (E[1]-E[0])/2.0;
114  cIncr = 1.0/( (numEndowments-1)*c2e );
115 
116  // Sum the pseudo probabilities for each state and action
117  for (int e = 0; e < numEndowments; e++)
118  {
119  stateProbSum[e] = vector<double> (numActions_total[e],0);
120  for (int a = 0; a < numActions_total[e]; a++)
121  {
122  int t = (a/numActions[e][0])-(a%numActions[e][0]);
123  for (int ep = 0; ep < numStates; ep++)
124  stateProbSum[e][a] += probHelper(e,t,ep);
125  } // for t
126  } // for e
127  } // constructor
128 
129  virtual SGPoint payoffs(int state, const vector<int> & actions) const
130  {
131  int t = actions[1]-actions[0]; // net transfer from 0 to 1
132  double c = consumption(state,t);
133 
134  return SGPoint(sqrt(c),sqrt(1-c));
135  } // payoffs
136 
137  virtual double probability(int e, const vector<int> & actions, int ep) const
138  {
139  int t = actions[1]-actions[0];
140  int a = actions[0]+actions[1]*numActions[e][0];
141  return probHelper(e,t,ep)/stateProbSum[e][a];
142  } // probability
143 
144  virtual bool isEquilibriumAction(int state, const vector<int> & actions) const
145  {
146  // Return true iff one of the players' actions is zero.
147  if ( (actions[0] == 0) || (actions[1] == 0) )
148  return true;
149  return false;
150  } // isEquilibriumAction
151 
152 };
153 
154 #endif
SGAbstractGame::numActions
vector< vector< int > > numActions
The numbers of actions in each state.
Definition: sgabstractgame.hpp:52
SGAbstractGame::numStates
int numStates
The number of states.
Definition: sgabstractgame.hpp:48
RiskSharingGame::isEquilibriumAction
virtual bool isEquilibriumAction(int state, const vector< int > &actions) const
Definition: sgrisksharing.hpp:144
SGAbstractGame
A virtual class for constructing games.
Definition: sgabstractgame.hpp:41
RiskSharingGame::probability
virtual double probability(int e, const vector< int > &actions, int ep) const
Transition probabilities.
Definition: sgrisksharing.hpp:137
SGPoint
A vector in .
Definition: sgpoint.hpp:35
RiskSharingGame
Two player version of the Kocherlakota (1996) risk sharing game.
Definition: sgrisksharing.hpp:29
SGAbstractGame::SGAbstractGame
SGAbstractGame(int _numPlayers, double _delta, int _numStates, vector< vector< int > > _numActions)
Constructor for the pure virtual class.
Definition: sgabstractgame.hpp:70
RiskSharingGame::payoffs
virtual SGPoint payoffs(int state, const vector< int > &actions) const
The payoff function.
Definition: sgrisksharing.hpp:129
SGAbstractGame::delta
double delta
The discount factor.
Definition: sgabstractgame.hpp:46