Skip to content

Commit 94939d0

Browse files
committed
do not solve demand mass balance when observed demand or availability is zero
1 parent 572345a commit 94939d0

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

revpy/mfrm.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ def estimate_host_level(observed, utilities, availability, market_share):
3737
Estimated demand, spill and recapture for H
3838
"""
3939

40-
if len(observed) != len(utilities) or len(observed) != len(availability):
41-
raise InvalidInputParameters('`observed`, `utilities` and'
42-
'`availability` must be of the same size')
43-
4440
demand = spill = recapture = 0
4541

4642
if observed and utilities and availability:
@@ -84,26 +80,35 @@ def estimate_class_level(observed, utilities, availability, market_share):
8480
Estimated demand, spill and recapture for H
8581
"""
8682

87-
if len(observed) != len(utilities) or len(observed) != len(availability):
88-
raise InvalidInputParameters('`observed`, `utilities` and'
89-
'`availability` must be of the same size')
90-
9183
_, hspill, hrecapture = estimate_host_level(observed, utilities,
9284
availability,
9385
market_share)
9486

9587
estimates = {}
9688
total_odemand = sum(observed.values())
9789

98-
for product, odemand in observed.items():
90+
for product in utilities.keys():
91+
odemand = observed.get(product, 0)
9992
avail = availability.get(product, 0)
100-
estimate = demand_mass_balance_c(total_odemand, odemand, avail,
101-
hrecapture)
102-
estimates[product] = {
103-
'demand': estimate[0],
104-
'spill': estimate[1],
105-
'recapture': estimate[2]
106-
}
93+
94+
if avail == 0 and odemand > 0:
95+
raise InvalidInputParameters('Non zero observed demand with '
96+
'zero availability')
97+
98+
if avail and odemand:
99+
estimate = demand_mass_balance_c(total_odemand, odemand, avail,
100+
hrecapture)
101+
estimates[product] = {
102+
'demand': estimate[0],
103+
'spill': estimate[1],
104+
'recapture': estimate[2]
105+
}
106+
else:
107+
estimates[product] = {
108+
'demand': 0,
109+
'spill': 0,
110+
'recapture': 0
111+
}
107112

108113
return calibrate_no_booking(estimates, observed, utilities, availability,
109114
market_share, hspill)

tests/test_mfrm.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ def test_demand_mass_balance_h(self):
3131
self.assertTupleEqual(round_tuple(estimations), (2.14, 0., 0.86))
3232

3333
def test_invalid_parameters(self):
34-
with self.assertRaises(InvalidInputParameters):
35-
mfrm.estimate_host_level({'fare1': 3},
36-
{'fare1': -2.8564, 'fare2': -2.5684},
37-
{'fare1': 1, 'fare2': 0.}, 0.5)
34+
"""Should not rise any exception
35+
"""
36+
37+
mfrm.estimate_host_level({'fare2': 3},
38+
{'fare1': -2.8564, 'fare2': -2.5684},
39+
{'fare2': 1.}, 0.5)
3840

3941

4042
class MFRMTestClass(unittest.TestCase):
@@ -86,10 +88,18 @@ def test_estimate_class_level_ex1(self):
8688
self.assertAlmostEqual(estimations['fare2']['demand'], 2.86, 2)
8789

8890
def test_invalid_parameters(self):
91+
"""Should not rise any exception
92+
"""
93+
94+
mfrm.estimate_class_level({'fare2': 3},
95+
{'fare1': -2.8564, 'fare2': -2.5684},
96+
{'fare2': 1.}, 0.5)
97+
98+
def test_non_zero_demand_zero_availability(self):
8999
with self.assertRaises(InvalidInputParameters):
90-
mfrm.estimate_class_level({'fare1': 3},
100+
mfrm.estimate_class_level({'fare1': 3, 'fare2': 1},
91101
{'fare1': -2.8564, 'fare2': -2.5684},
92-
{'fare1': 1, 'fare2': 0.}, 0.5)
102+
{'fare2': 1.}, 0.5)
93103

94104

95105
def round_tuple(tlp, level=2):

0 commit comments

Comments
 (0)