forked from INET-Complexity/Core-ESL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLedger.java
More file actions
340 lines (277 loc) · 12 KB
/
Ledger.java
File metadata and controls
340 lines (277 loc) · 12 KB
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
package accounting;
import agents.Agent;
import actions.Action;
import contracts.Asset;
import contracts.Contract;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
/**
* This is the main class implementing double entry accounting. All public operations provided by this class
* are performed as a double entry operation, i.e. a pair of (dr, cr) operations.
*
* A Ledger contains a set of accounts, and is the interface between an agent and its accounts. Agents cannot
* directly interact with accounts other than via a Ledger.
*
* At the moment, a Ledger contains an account for each type of contract, plus an equity account and a cash account.
*
* A simple economic agent will usually have a single Ledger, whereas complex firms and banks can have several books
* (as in branch banking for example).
*
* @author rafa
*/
public class Ledger implements LedgerAPI {
public Ledger(Agent owner) {
// A Ledger is a list of accounts (for quicker searching)
assetAccounts = new HashSet<>();
liabilityAccounts = new HashSet<>();
equityAccounts = new HashSet<>();
allAssets = new HashSet<>();
allLiabilities = new HashSet<>();
// Each Account includes an inventory to hold one type of contract.
// These hashmaps are used to access the correct account for a given type of contract.
// Note that separate hashmaps are needed for asset accounts and liability accounts: the same contract
// type (such as Loan) can sometimes be an asset and sometimes a liability.
contractsToAssetAccounts = new HashMap<>();
contractsToLiabilityAccounts = new HashMap<>();
// Not sure whether I should be passing the owner...
this.owner = owner;
// A book is initially created with a cash account and an equityAccounts account (it's the simplest possible book)
cashAccount = new Account("cash", AccountType.ASSET);
equityAccount = new Account("equityAccounts", AccountType.EQUITY);
addAccount(cashAccount, null);
addAccount(equityAccount, null);
}
private HashSet<Contract> allAssets;
private HashSet<Contract> allLiabilities;
private Agent owner;
private HashSet<Account> assetAccounts;
private HashSet<Account> liabilityAccounts;
private HashSet<Account> equityAccounts;
private HashMap<Class<? extends contracts.Contract>, Account> contractsToAssetAccounts;
private HashMap<Class<? extends contracts.Contract>, Account> contractsToLiabilityAccounts;
private Account cashAccount;
private Account equityAccount;
public double getAssetValue() {
double assetTotal = 0;
for (Account assetAccount : assetAccounts) {
assetTotal+=assetAccount.getBalance();
}
return assetTotal;
}
public double getLiabilityValue() {
double liabilityTotal = 0;
for (Account liabilityAccount : liabilityAccounts) {
liabilityTotal+=liabilityAccount.getBalance();
}
return liabilityTotal;
}
public double getEquityValue() {
double equityTotal = 0;
for (Account equityAccount : equityAccounts) {
equityTotal += equityAccount.getBalance();
}
return equityTotal;
}
public double getAssetValueOf(Class<?> contractType) {
// return contractsToAssetAccounts.get(contractType).getBalance();
return allAssets.stream()
.filter(contractType::isInstance)
.mapToDouble(Contract::getValue)
.sum();
}
public double getLiabilityValueOf(Class<?> contractType) {
// return contractsToLiabilityAccounts.get(contractType).getBalance();
return allLiabilities.stream()
.filter(contractType::isInstance)
.mapToDouble(Contract::getValue)
.sum();
}
public HashSet<Contract> getAssetsOfType(Class<?> contractType) {
return allAssets.stream()
.filter(contractType::isInstance)
.collect(Collectors.toCollection(HashSet::new));
}
public HashSet<Contract> getLiabilitiesOfType(Class<?> contractType) {
return allLiabilities.stream()
.filter(contractType::isInstance)
.collect(Collectors.toCollection(HashSet::new));
}
public double getCash() {
return cashAccount.getBalance();
}
private void addAccount(Account account, Class<? extends Contract> contractType) {
switch(account.getAccountType()) {
case ASSET:
assetAccounts.add(account);
contractsToAssetAccounts.put(contractType, account);
break;
case LIABILITY:
liabilityAccounts.add(account);
contractsToLiabilityAccounts.put(contractType, account);
break;
case EQUITY:
equityAccounts.add(account);
// Not sure what to do with INCOME, EXPENSES
}
}
/**
* Adding an asset means debiting the account relevant to that type of contract
* and crediting equity.
* @param contract an Asset contract to add
*/
public void addAsset(Contract contract) {
Account assetAccount = contractsToAssetAccounts.get(contract.getClass());
if (assetAccount==null) {
// If there doesn't exist an Account to hold this type of contract, we create it
assetAccount = new Account(contract.getClass().getName(), AccountType.ASSET);
addAccount(assetAccount, contract.getClass());
}
// (dr asset, cr equity)
Account.doubleEntry(assetAccount, equityAccount, contract.getValue());
// Add to the general inventory?
allAssets.add(contract);
}
/**
* Adding a liability means debiting equity and crediting the account
* relevant to that type of contract.
* @param contract a Liability contract to add
*/
public void addLiability(Contract contract) {
Account liabilityAccount = contractsToLiabilityAccounts.get(contract.getClass());
if (liabilityAccount==null) {
// If there doesn't exist an Account to hold this type of contract, we create it
liabilityAccount = new Account(contract.getClass().getName(), AccountType.LIABILITY);
addAccount(liabilityAccount, contract.getClass());
}
// (dr equity, cr liability)
Account.doubleEntry(equityAccount, liabilityAccount, contract.getValue());
// Add to the general inventory?
allLiabilities.add(contract);
}
public void addCash(double amount) {
// (dr cash, cr equity)
Account.doubleEntry(cashAccount, equityAccount, amount);
}
/**
* Operation to cancel a Loan to someone (i.e. cash in a Loan in the Assets side).
*
* I'm using this for simplicity but note that this is equivalent to selling an asset.
* @param amount the amount of loan that is cancelled
*/
public void pullFunding(double amount, Contract loan) {
Account loanAccount = contractsToAssetAccounts.get(loan.getClass());
// (dr cash, cr asset )
Account.doubleEntry(cashAccount, loanAccount, amount);
}
/**
* Operation to pay back a liability loan; debit liability and credit cash
* @param amount amount to pay back
*/
public void payLiability(double amount, Contract loan) {
Account liabilityAccount = contractsToLiabilityAccounts.get(loan.getClass());
//Todo: What do we do if we can't pay??!! At the moment I'm calling my owner to raise liquidity
if (cashAccount.getBalance() < amount) {
System.out.println();
System.out.println("***");
System.out.println(owner.getName()+" must raise liquidity immediately.");
owner.raiseLiquidity(amount * (1 - cashAccount.getBalance()/getAssetValue()));
System.out.println("***");
System.out.println();
}
// (dr liability, cr cash )
Account.doubleEntry(liabilityAccount, cashAccount, amount);
}
/**
* If I've sold an asset, debit cash and credit asset
* @param amount the *value* of the asset
*/
public void sellAsset(double amount, Class<? extends Contract> assetType) {
Account assetAccount = contractsToAssetAccounts.get(assetType);
// (dr cash, cr asset)
Account.doubleEntry(cashAccount, assetAccount, amount);
}
/**
* Behavioral stuff; not sure if it should be here
* @param me the owner of the Ledger
* @return an ArrayList of Actions that are available to me at this moment
*/
public ArrayList<Action> getAvailableActions(Agent me) {
ArrayList<Action> availableActions = new ArrayList<>();
for (Contract contract : allAssets) {
if (contract.getAvailableActions(me)!=null) {
availableActions.addAll(contract.getAvailableActions(me));
}
}
for (Contract contract : allLiabilities) {
if (contract.getAvailableActions(me)!= null) {
availableActions.addAll(contract.getAvailableActions(me));
}
}
return availableActions;
}
/**
* Stress-testing specific.
*/
public void updateAssetPrices() {
List<Contract> allAssets = this.allAssets.stream()
.filter(contract -> contract instanceof Asset)
.collect(Collectors.toList());
for (Contract contract : allAssets) {
Asset asset = (Asset) contract;
if (asset.priceFell()) {
devalueAsset(asset.valueLost(), asset);
asset.updatePrice();
}
}
}
/**
* if an Asset loses value, I must debit equity and credit asset
* @param amount
*/
private void devalueAsset(double amount, Contract asset) {
Account assetAccount = contractsToAssetAccounts.get(asset.getClass());
// (dr equityAccounts, cr assetAccounts)
Account.doubleEntry(equityAccount, assetAccount, amount);
}
/**
* This mimics the default on a loan. If I lend money to someone and they default on me, at the moment
* I assume that I lose a 'valueFraction' of its value. There are two double-entry operations:
*
* First I take a hit on equity for the lost value of the loan (dr equity, cr asset)
* Then I cash in the loan (dr cash, cr asset)
*
* @param initialValue the original value of the loan
* @param valueFraction the fraction of the loan that will be lost due to the default
*/
public void liquidateLoan(double initialValue, double valueFraction, Contract loan) {
Account assetLoanAccount = contractsToAssetAccounts.get(loan.getClass());
double valueLost = (1 - valueFraction) * initialValue;
// First, we devalue the loan :(
// (dr equity, cr asset)
Account.doubleEntry(equityAccount, assetLoanAccount, valueLost);
// Then, we liquidate it
// (dr cash, cr asset)
Account.doubleEntry(cashAccount, assetLoanAccount, initialValue - valueLost);
}
public void printBalanceSheet() {
System.out.println("Asset accounts:");
System.out.println("---------------");
for (Account account : assetAccounts) {
System.out.println(account.getName()+" -> "+ String.format( "%.2f", account.getBalance()));
}
System.out.println("TOTAL ASSETS: "+ String.format( "%.2f", getAssetValue()));
System.out.println();
System.out.println("Liability accounts:");
System.out.println("---------------");
for (Account account : liabilityAccounts) {
System.out.println(account.getName()+" -> "+ String.format( "%.2f", account.getBalance()));
}
System.out.println("TOTAL LIABILITIES: "+ String.format( "%.2f", getLiabilityValue()));
System.out.println();
System.out.println("TOTAL EQUITY: "+String.format("%.2f", getEquityValue()));
System.out.println();
}
}