def test_can_pad_with_zero(self): # probability sums up to 0.7 p3 = ProbabilityDistribution({ 1: 0.5, 2: 0.2 }, normalize=False) # sanity check self.assertAlmostEqual(p3.p(0), 0) self.assertEqual(p3.mean, None) p3.pad() self.assertAlmostEqual(p3.p(0), 0.3) self.assertEqual(p3.mean, 0.9)
def process_insulation_type(self, dwelling, insulation_type): base_dist = self.get_base_dist(dwelling)[insulation_type] construction_year = dwelling.attributes['bouwjaar'] dwelling_type = dwelling.attributes['woningtype'] if insulation_type == 'cavity wall': # Only buildings between 1920 and 1974 # are eligible for cavity wall insulation upgrade: # older buildings have no cavity wall, # newer buildings already have an insulated cavity wall. if 1920 <= construction_year <= 1974: applicable_measure_years = range(2010, 2019 + 1) else: applicable_measure_years = [] else: # We only have data available for 2010 to 2019, # and we assume a waiting period of # MIN_YEAR_MEASURE_AFTER_CONSTRUCTION # after construction before a measure gets taken. applicable_measure_years = range( max( 2010, construction_year + self.MIN_YEAR_MEASURE_AFTER_CONSTRUCTION), 2019 + 1) if insulation_type in ['facade', 'floor', 'roof']: measures_r_values = [ self.insulation_measures_r_values[year] for year in applicable_measure_years ] elif insulation_type == 'window': # The info on measures that we have is on # HR glazing. measures_r_values = [ self.glazing_r_values['hr'].copy() for _ in applicable_measure_years ] elif insulation_type == 'cavity wall': measures_r_values = [ self.cavity_wall_r_value for _ in applicable_measure_years ] else: raise NotImplementedError measure_prob_multiplier = self.dwelling_type_multipliers[dwelling_type] measures_prob = [ measure_prob_multiplier * self.insulation_measures_p[self.insulation_measures_p.year == year][insulation_type].values[0] for year in applicable_measure_years ] if len(applicable_measure_years) == 0: # No measures apply, # so no probability for increases. # We need to set this case specifically, # because else the sum() will return 0, # which can't be .pad()-ded. measures_dist = ProbabilityDistribution({0: 1}) else: measures_dist = sum([ measures_r_values[i] * measures_prob[i] for i in range(len(measures_r_values)) ]) if insulation_type in ['facade', 'floor', 'roof']: measures_dist.pad() # Add the increase of R-values in measures_dist # to the base distribution base_dist. return base_dist & measures_dist # With windows, the measure is not an addition to the # existing insulation, but a replacement. elif insulation_type == 'window': p_measures = sum(measures_prob) # 'measures_dist' already includes the absolute # probability of the measures, so doesn't need to # be multiplied by p_measures. if p_measures != 0: window_r_dist = (1 - p_measures) * base_dist + measures_dist else: window_r_dist = base_dist return window_r_dist elif insulation_type == 'cavity wall': measures_dist.pad() # This comes as an addon to regular facade insulation. return measures_dist else: raise NotImplementedError