Skip to content

Commit 9f565da

Browse files
committed
Lint according to PEP8
1 parent 9d71b4f commit 9f565da

File tree

4 files changed

+37
-46
lines changed

4 files changed

+37
-46
lines changed

contract.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66

77

88
class Contract:
9-
def __init__(self, contract_partners, endtime = None):
9+
def __init__(self, contract_partners, endtime=None):
1010
self.contract_partners = contract_partners
1111
self.obligations = {}
1212
self.endtime = endtime
13-
self.valid = True
14-
13+
self.valid = True
1514

1615
@staticmethod
1716
def generated(contract_dict):
@@ -43,17 +42,17 @@ def fulfill_obligation(self, me, von, to, delivery):
4342
""" over delivery is handled quietly """
4443
for good, amount in delivery.items():
4544
try:
46-
#me.destroy(good, amount)
45+
# me.destroy(good, amount)
4746
me.give(self.contract_partners[to][0], self.contract_partners[to][1], good, amount)
4847
self.obligations[von][good] = max(0, self.obligations[von][good] - amount)
4948
except NotEnoughGoods:
5049
raise
5150

5251
def terminate(self):
5352
self.valid = False
54-
53+
5554
def is_valid(self):
5655
return self.valid
57-
56+
5857
def get_endtime(self):
5958
return self.endtime

expectedvaluemc.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,33 @@
1111
>>> getEV(GeneralizedExponential(33.33), 10, None, 40., None)
1212
>>> getEV(GeneralizedExponential(33.33), 10, 4., None, 4.)
1313
"""
14+
15+
1416
def getEV(dist, sampleSize=1000, min=None, max=None, defaultVal=None):
15-
rvs = populateArray(dist, sampleSize, min, max, defaultVal);
17+
rvs = populateArray(dist, sampleSize, min, max, defaultVal)
1618
return sum(rvs) / len(rvs)
1719

20+
1821
def populateArray(dist, sampleSize, min, max, defaultVal):
1922
""" Create new instance of RandomEngine with new seed, otherwise
2023
duplicates in the rvs of successive method calls are possible. """
21-
if defaultVal is None :
24+
if defaultVal is None:
2225
rvs = []
2326
while len(rvs) < sampleSize:
24-
# HELP IntStream.range(0, sampleSize - rvs.size()).forEach($ -> rvs.add(dist.random(randomE)));
25-
rvs = dist.rvs(sampleSize - len(rvs)) # inefficient
26-
# If the distribution has boundaries without default
27-
# value to fall back to, delete rvs outside the
28-
# boundaries, new ones will be drawn in the next iteration.
27+
# HELP IntStream.range(0, sampleSize - rvs.size()).forEach($ -> rvs.add(dist.random(randomE)));
28+
rvs = dist.rvs(sampleSize - len(rvs)) # inefficient
29+
# If the distribution has boundaries without default
30+
# value to fall back to, delete rvs outside the
31+
# boundaries, new ones will be drawn in the next iteration.
2932
if min is not None:
3033
rvs = filter(lambda rv: rv < min, rvs)
3134
if max is not None:
3235
rvs = filter(lambda rv: rv > max, rvs)
3336
else:
3437
rvs = dist.rvs(sampleSize)
3538
if min is not None:
36-
rvs = [rv if rv >= min else defaultVal for rv in rvs] # SHOULD THAT NOT BE 0?
39+
rvs = [rv if rv >= min else defaultVal for rv in rvs] # SHOULD THAT NOT BE 0?
3740
if max is not None:
3841
rvs = [rv if rv <= max else defaultVal for rv in rvs]
39-
40-
return rvs
41-
42-
43-
4442

43+
return rvs

insurancefirm.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,40 @@ def init(self, simulation_parameters, agent_parameters):
1717
self.create('money', simulation_parameters['start_cash_insurer'])
1818
self.contracts = []
1919

20-
2120
def quote(self):
2221
for request in self.get_messages('request_insurancequote'):
2322
self.message(request.sender_group, request.sender_id, 'insurancequotes', self.acceptInsuranceContract(request.content))
2423

2524
def acceptInsuranceContract(self, request):
26-
return self.riskmodel.evaluate(request['runtime'], request['excess'] , request['deductible'])
25+
return self.riskmodel.evaluate(request['runtime'], request['excess'], request['deductible'])
2726

2827
def add_contract(self):
2928
for contract in self.get_messages('addcontract'):
3029
self.contracts.append(InsuranceContract.generated(contract.content))
3130

3231
def filobl(self):
33-
insurance_payouts = 0
32+
insurance_payouts = 0
3433
for contract in self.contracts:
35-
3634
current_payout = contract.get_obligation('insurer', 'money')
37-
3835
if current_payout > 0:
3936
contract.fulfill_obligation(self,
4037
von='insurer',
4138
to='policyholder',
4239
delivery={'money': current_payout})
4340
insurance_payouts += current_payout
44-
#print("DEBUG: Booked claim payout ", current_payout)
45-
41+
# print("DEBUG: Booked claim payout ", current_payout)
4642
self.log('insurancepayouts', insurance_payouts)
4743
self.log('money', self.possession('money'))
4844
self.log('num_contracts', len(self.contracts))
4945

5046
def mature_contracts(self):
51-
#for contract in self.contracts:
47+
# for contract in self.contracts:
5248
# print(type(contract), contract)
5349
# #contract.is_valid()
54-
55-
#[contract.terminate() for contract in self.contracts if (contract.end_time < self.round)]
56-
#self.contracts = [contract for contract in self.contracts if (contract.end_time >= self.round)]
57-
50+
51+
# [contract.terminate() for contract in self.contracts if (contract.end_time < self.round)]
52+
# self.contracts = [contract for contract in self.contracts if (contract.end_time >= self.round)]
53+
5854
[contract.terminate() for contract in self.contracts if (contract.get_endtime() < self.round)]
5955
self.contracts = [contract for contract in self.contracts if (contract.is_valid())]
6056

riskmodel.py

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
#/**
21
# * Created by Torsten Heinrich
3-
# */
42
# Translated to python by Davoud Taghawi-Nejad
53
import expectedvaluemc
6-
#from generalizedpareto import GeneralizedPareto
7-
#from generalizedexponential import GeneralizedExponential
4+
# from generalizedpareto import GeneralizedPareto
5+
# from generalizedexponential import GeneralizedExponential
86
import scipy.stats
97

8+
109
class RiskModel:
1110
def __init__(self,
1211
riskDistribution=scipy.stats.pareto(2., 0., 10.),
13-
riskPeriod=scipy.stats.expon(0, 100./3.)):
12+
riskPeriod=scipy.stats.expon(0, 100. / 3.)):
1413
"""
1514
setting default distributions:
1615
Power Law with x_min = 10.
@@ -19,16 +18,16 @@ def __init__(self,
1918
Exponential with lambda = .03
2019
=> PDF(x) = 0.03 * e^(-0.03*x)
2120
"""
22-
self.riskDistribution = riskDistribution;
23-
self.riskPeriod = riskPeriod;
21+
self.riskDistribution = riskDistribution
22+
self.riskPeriod = riskPeriod
2423

2524
# def getDistributionPPF(self, tailSize):
2625
# """ allows to check for expected size of 1/x period (e.g. 1/200 year) events ... and to compute expected value (.5)"""
2726
# return self.riskDistribution.quantile(tailSize);
2827

2928
def getPPF(self, tailSize):
3029
"""alias of getDistributionPPF """
31-
return self.riskDistribution.ppf(tailSize); #TODO: correct syntax?
30+
return self.riskDistribution.ppf(tailSize) # TODO: correct syntax?
3231

3332
# def getPeriodPPF(self, tailSize):
3433
# """ allows to compute expected value (.5)"""
@@ -43,11 +42,9 @@ def evaluate(self, runtime, excess, deductible, expectedReturn=.15):
4342
and risk period /riskPeriod.mean * riskDistribution.mean * runtime -
4443
deductible = riskFreq.mean * riskDistribution.mean * runtime - deductible """
4544

45+
distributionExpectedValue = expectedvaluemc.getEV(self.riskDistribution, 1000, None, None, excess)
46+
periodExpectedValue = expectedvaluemc.getEV(self.riskPeriod, 1000, None, None, None)
4647

47-
distributionExpectedValue = expectedvaluemc.getEV(self.riskDistribution, 1000, None, None, excess);
48-
periodExpectedValue = expectedvaluemc.getEV(self.riskPeriod, 1000, None, None, None);
49-
50-
expectedLoss = distributionExpectedValue * (1./periodExpectedValue) * runtime - deductible;
51-
#print("DEBUG **RM", distributionExpectedValue, periodExpectedValue, expectedLoss, expectedReturn, expectedLoss * (1. + expectedReturn))
52-
return expectedLoss * (1. + expectedReturn);
53-
48+
expectedLoss = distributionExpectedValue * (1. / periodExpectedValue) * runtime - deductible
49+
# print("DEBUG **RM", distributionExpectedValue, periodExpectedValue, expectedLoss, expectedReturn, expectedLoss * (1. + expectedReturn))
50+
return expectedLoss * (1. + expectedReturn)

0 commit comments

Comments
 (0)