1
1
# coding=utf-8
2
- # Copyright (c) 2010-2012 , GEM Foundation.
2
+ # Copyright (c) 2010-2013 , GEM Foundation.
3
3
#
4
4
# OpenQuake Risklib is free software: you can redistribute it and/or
5
5
# modify it under the terms of the GNU Affero General Public License
19
19
import collections
20
20
import unittest
21
21
import numpy
22
+ import random
23
+ import itertools
22
24
23
25
from risklib import api
24
26
from risklib import scientific
110
112
],
111
113
112
114
expected_poes = [
113
- [1. , 0.947368 , 0.894737 , 0.842105 , 0.789474 , 0.736842 , 0.684211 ,
114
- 0.631579 , 0.578947 , 0.526316 , 0.473684 , 0.421053 , 0.368421 ,
115
- 0.315789 , 0.263158 , 0.210526 , 0.157895 , 0.105263 , 0.052632 , 0. ],
116
- [1. , 0.98063792 , 0.96127585 , 0.94191377 , 0.9225517 ,
117
- 0.90318962 , 0.88382754 , 0.86446547 , 0.84510339 , 0.82574132 ,
118
- 0.80637924 , 0.78701717 , 0.76765509 , 0.74829301 , 0.72893094 ,
119
- 0.70956886 , 0.69020679 , 0.67084471 , 0.65148263 , 0.63212056 ],
120
- [1. , 0.947368 , 0.894737 , 0.842105 , 0.789474 , 0.736842 ,
121
- 0.684211 , 0.631579 , 0.578947 , 0.526316 , 0.473684 , 0.421053 ,
122
- 0.368421 , 0.315789 , 0.263158 , 0.210526 , 0.157895 , 0.105263 ,
123
- 0.052632 , 0. ],
124
- ],
115
+ [1. , 0.94736842 , 0.89473684 , 0.84210526 ,
116
+ 0.78947368 , 0.7368421 , 0.68421052 , 0.63157895 , 0.57894737 , 0.52631579 ,
117
+ 0.47368421 , 0.42105263 , 0.36842105 , 0.31578947 , 0.26315789 ,
118
+ 0.21052632 , 0.15789474 , 0.10526316 , 0.05263158 , 0. ],
119
+ [1. , 0.98063792 ,
120
+ 0.96127585 , 0.94191377 , 0.9225517 , 0.90318962 , 0.88382754 ,
121
+ 0.86446547 , 0.84510339 , 0.82574132 , 0.80637924 , 0.78701717 ,
122
+ 0.76765509 , 0.74829301 , 0.72893094 , 0.70956886 , 0.69020679 ,
123
+ 0.67084471 , 0.65148263 , 0.63212056 ],
124
+ [1. , 0.947368 , 0.894737 ,
125
+ 0.842105 , 0.789474 , 0.736842 , 0.684211 , 0.631579 , 0.578947 ,
126
+ 0.526316 , 0.473684 , 0.421053 , 0.368421 , 0.315789 , 0.263158 ,
127
+ 0.210526 , 0.157895 , 0.105263 , 0.052632 , 0. ], ],
125
128
126
129
expected_losses = numpy .array ([
127
130
[40.5835007 , 70.37142354 , 81.78761801 , 94.22512956 ,
147
150
148
151
class EventBasedTestCase (unittest .TestCase ):
149
152
150
- def assert_allclose (self , expected , actual ):
151
- return numpy .testing .assert_allclose (
152
- expected , actual , atol = 0.0 , rtol = 0.05 )
153
+ def test_mean_based_with_no_correlation (self ):
154
+ # This is a regression test. Data has not been checked
155
+ vf = (
156
+ scientific .VulnerabilityFunction (
157
+ [0.001 , 0.2 , 0.3 , 0.5 , 0.7 ], [0.01 , 0.1 , 0.2 , 0.4 , 0.8 ],
158
+ [0.01 , 0.02 , 0.02 , 0.01 , 0.03 ], "LN" ))
159
+ calc = api .ProbabilisticEventBased (
160
+ vf , 30 , 120 , seed = 1 , correlation = 0 , curve_resolution = 4 )
161
+
162
+ outputs = calc ([scientific .Asset (1000 ), scientific .Asset (2000 )],
163
+ [[10 , 20 , 30 , 40 , 50 ], [1 , 2 , 3 , 4 , 5 ]])
164
+
165
+ numpy .testing .assert_allclose (
166
+ [0.80732874 , 0.82524302 , 0.8401855 , 0.84260182 ],
167
+ outputs [0 ].loss_ratio_curve .abscissae )
168
+
169
+ def test_mean_based_with_partial_correlation (self ):
170
+ # This is a regression test. Data has not been checked
171
+ vf = (
172
+ scientific .VulnerabilityFunction (
173
+ [0.001 , 0.2 , 0.3 , 0.5 , 0.7 ], [0.01 , 0.1 , 0.2 , 0.4 , 0.8 ],
174
+ [0.01 , 0.02 , 0.02 , 0.01 , 0.03 ], "LN" ))
175
+ calc = api .ProbabilisticEventBased (
176
+ vf , 30 , 120 , seed = 1 , correlation = 0.5 , curve_resolution = 4 )
177
+
178
+ outputs = calc ([scientific .Asset (1000 ), scientific .Asset (2000 )],
179
+ [[10 , 20 , 30 , 40 , 50 ], [1 , 2 , 3 , 4 , 5 ]])
180
+
181
+ numpy .testing .assert_allclose (
182
+ [0.77366888 , 0.79923542 , 0.81229682 , 0.82383648 ],
183
+ outputs [0 ].loss_ratio_curve .abscissae )
184
+
185
+ def test_mean_based_with_perfect_correlation (self ):
186
+ # This is a regression test. Data has not been checked
187
+ vf = (
188
+ scientific .VulnerabilityFunction (
189
+ [0.001 , 0.2 , 0.3 , 0.5 , 0.7 ], [0.01 , 0.1 , 0.2 , 0.4 , 0.8 ],
190
+ [0.01 , 0.02 , 0.02 , 0.01 , 0.03 ], "LN" ))
191
+ calc = api .ProbabilisticEventBased (
192
+ vf , 30 , 120 , seed = 1 , correlation = 1 , curve_resolution = 4 )
193
+
194
+ outputs = calc ([scientific .Asset (1000 ), scientific .Asset (2000 )],
195
+ [[10 , 20 , 30 , 40 , 50 ], [1 , 2 , 3 , 4 , 5 ]])
196
+
197
+ numpy .testing .assert_allclose (
198
+ [0.76161603 , 0.78226872 , 0.79620137 , 0.81240868 ],
199
+ outputs [0 ].loss_ratio_curve .abscissae )
153
200
154
201
def test_mean_based (self ):
155
202
vulnerability_function_rm = (
@@ -181,16 +228,19 @@ def test_mean_based(self):
181
228
asset_output .conditional_losses [CONDITIONAL_LOSS_POES ],
182
229
delta = 0.05 * mb .expected_loss_map [i ])
183
230
184
- self .assert_allclose (mb .expected_poes ,
185
- asset_output .loss_ratio_curve .ordinates )
231
+ numpy .testing .assert_allclose (
232
+ mb .expected_poes , asset_output .loss_ratio_curve .ordinates ,
233
+ rtol = 10E-4 )
186
234
187
- self .assert_allclose (mb .expected_poes ,
188
- asset_output .loss_curve .ordinates )
235
+ numpy .testing .assert_allclose (
236
+ mb .expected_poes , asset_output .loss_curve .ordinates ,
237
+ rtol = 10E-4 )
189
238
190
- self .assert_allclose (mb .expected_losses [i ],
191
- asset_output .loss_curve .abscissae )
239
+ numpy .testing .assert_allclose (
240
+ mb .expected_losses [i ], asset_output .loss_curve .abscissae ,
241
+ rtol = 10E-4 )
192
242
193
- self .assert_allclose (
243
+ numpy . testing .assert_allclose (
194
244
mb .expected_losses [2 ] / mb .input_models_asset [2 ].value ,
195
245
asset_output_rc .loss_ratio_curve .abscissae )
196
246
@@ -199,18 +249,22 @@ def test_mean_based(self):
199
249
asset_output_rc .conditional_losses [CONDITIONAL_LOSS_POES ],
200
250
delta = 0.05 * mb .expected_loss_map [2 ])
201
251
202
- self .assert_allclose (mb .expected_poes ,
203
- asset_output_rc .loss_ratio_curve .ordinates )
252
+ numpy .testing .assert_allclose (
253
+ mb .expected_poes , asset_output_rc .loss_ratio_curve .ordinates ,
254
+ rtol = 10E-4 )
204
255
205
- self .assert_allclose (mb .expected_poes ,
206
- asset_output_rc .loss_curve .ordinates )
256
+ numpy .testing .assert_allclose (
257
+ mb .expected_poes , asset_output_rc .loss_curve .ordinates ,
258
+ rtol = 10E-4 )
207
259
208
- self .assert_allclose (mb .expected_losses [2 ],
209
- asset_output_rc .loss_curve .abscissae )
260
+ numpy .testing .assert_allclose (
261
+ mb .expected_losses [2 ], asset_output_rc .loss_curve .abscissae ,
262
+ rtol = 10E-5 )
210
263
211
- self .assert_allclose (
264
+ numpy . testing .assert_allclose (
212
265
mb .expected_losses [2 ] / mb .input_models_asset [2 ].value ,
213
- asset_output_rc .loss_ratio_curve .abscissae )
266
+ asset_output_rc .loss_ratio_curve .abscissae ,
267
+ rtol = 10E-5 )
214
268
215
269
def test_insured_loss_mean_based (self ):
216
270
vulnerability_function_rm = (
@@ -235,10 +289,12 @@ def test_insured_loss_mean_based(self):
235
289
asset_output_rc = calculator_rc ([il .input_models_asset [2 ]], [gmf [2 ]])
236
290
237
291
for i , asset_output in enumerate (asset_output_rm + asset_output_rc ):
238
- self .assert_allclose (
292
+ numpy . testing .assert_allclose (
239
293
il .expected_poes [i ],
240
- asset_output .insured_losses .ordinates )
294
+ asset_output .insured_losses .ordinates ,
295
+ rtol = 10E-5 )
241
296
242
- self .assert_allclose (
297
+ numpy . testing .assert_allclose (
243
298
il .expected_losses [i ],
244
- asset_output .insured_losses .abscissae )
299
+ asset_output .insured_losses .abscissae ,
300
+ rtol = 10E-5 )
0 commit comments