forked from roberthansen/py_cet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
measure_calculations_match_sql.py
640 lines (580 loc) · 26.1 KB
/
measure_calculations_match_sql.py
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
import numpy as np
import pandas as pd
from measure_calculations import ratepayer_impact_measure
import quarter_calculations_match_sql as qc
def calculate_avoided_electric_costs(measure, AvoidedCostElectric, Settings, first_year):
### parameters:
### measure : a single row from the 'data' variable of an
### 'InputMeasures' object of class 'EDCS_Table' or
### 'EDCS_Query_Results'
### AvoidedCostElectric : an instance of an 'AvoidedCostElectric' object
### of class 'EDCS_Table' or 'EDCS_Query_Results'
### Settings : an instance of a 'Settings' object of class 'EDCS_Table'
### or 'EDCS_Query_Results'
### first_year : an int representing the first year for the program
### through which the input measure is implemented
###
### returns:
### pandas Series containing calculated measure benefits due to avoided
### electric costs
# filter avoided cost table for calculations based on sql version of cet:
avoided_cost_electric = AvoidedCostElectric.filter_by_measure(measure)
if avoided_cost_electric.size > 0:
# filter settings table:
settings = Settings.filter_by_measure(measure).iloc[0]
f = lambda r: qc.present_value_generation_benefits(r, measure, settings, first_year)
present_value_generation_benefits = avoided_cost_electric.apply(f, axis='columns').aggregate(np.sum)
f = lambda r: qc.present_value_transmission_and_distribution_benefits(r, measure, settings, first_year)
present_value_transmission_and_distribution_benefits = avoided_cost_electric.apply(f, axis='columns').aggregate(np.sum)
else:
present_value_generation_benefits = 0
present_value_transmission_and_distribution_benefits = 0
avoided_electric_costs = pd.Series({
'CET_ID' : measure.CET_ID,
'ProgramID' : measure.ProgramID,
'Qi' : measure.Qi,
'GenerationBenefitsGross' : max(
measure[['Quantity','IRkWh','RRkWh']].product() *
present_value_generation_benefits,
0
),
'TransmissionAndDistributionBenefitsGross' : max(
measure[['Quantity','IRkW','RRkW']].product() *
present_value_transmission_and_distribution_benefits,
0
),
'ElectricBenefitsGross' : max(
measure[['Quantity','IRkWh','RRkWh']].product() *
present_value_generation_benefits +
measure[['Quantity','IRkW','RRkW']].product() *
present_value_transmission_and_distribution_benefits,
0
),
'GenerationCostsGross' : max(
-measure[['Quantity','IRkWh','RRkWh']].product() *
present_value_generation_benefits,
0
),
'TransmissionAndDistributionCostsGross' : max(
-measure[['Quantity','IRkW','RRkW']].product() *
present_value_transmission_and_distribution_benefits,
0
),
'ElectricCostsGross' : max(
-measure[['Quantity','IRkWh','RRkWh']].product() *
present_value_generation_benefits -
measure[['Quantity','IRkW','RRkW']].product() *
present_value_transmission_and_distribution_benefits,
0
),
'GenerationBenefitsNet' : max(
measure[['Quantity','IRkWh','RRkWh']].product() *
measure[['NTGRkWh','MarketEffectsBenefits']].sum() *
present_value_generation_benefits,
0
),
'TransmissionAndDistributionBenefitsNet' : max(
measure[['Quantity','IRkW','RRkW']].product() *
measure[['NTGRkW','MarketEffectsBenefits']].sum() *
present_value_transmission_and_distribution_benefits,
0
),
'ElectricBenefitsNet' : max(
measure[['Quantity','IRkWh','RRkWh']].product() *
measure[['NTGRkWh','MarketEffectsBenefits']].sum() *
present_value_generation_benefits +
measure[['Quantity','IRkW','RRkW']].product() *
measure[['NTGRkW','MarketEffectsBenefits']].sum() *
present_value_transmission_and_distribution_benefits,
0
),
'GenerationCostsNet' : max(
-measure[['Quantity','IRkWh','RRkWh']].product() *
measure[['NTGRkWh','MarketEffectsBenefits']].sum() *
present_value_generation_benefits,
0
),
'TransmissionAndDistributionCostsNet' : max(
-measure[['Quantity','IRkW','RRkW']].product() *
measure[['NTGRkW','MarketEffectsBenefits']].sum() *
present_value_transmission_and_distribution_benefits,
0
),
'ElectricCostsNet' : max(
-measure[['Quantity','IRkWh','RRkWh']].product() *
measure[['NTGRkWh','MarketEffectsBenefits']].sum() *
present_value_generation_benefits -
measure[['Quantity','IRkW','RRkW']].product() *
measure[['NTGRkW','MarketEffectsBenefits']].sum() *
present_value_transmission_and_distribution_benefits,
0
),
})
return avoided_electric_costs
def calculate_avoided_gas_costs(measure, AvoidedCostGas, Settings, first_year):
### parameters:
### measure : a single row from the 'data' variable of an
### 'InputMeasures' object of class 'EDCS_Table' or
### 'EDCS_Query_Results'
### AvoidedCostGas : an instance of an 'AvoidedCostGas' object of class
### 'EDCS_Table' or 'EDCS_Query_Results'
### Settings : an instance of a 'Settings' object of class 'EDCS_Table'
### or 'EDCS_Query_Results'
###
### returns:
### pandas Series containing calculated measure benefits due to avoided
### gas costs
# filter avoided cost table for calculations based on sql version of cet:
avoided_cost_gas = AvoidedCostGas.filter_by_measure(measure)
if avoided_cost_gas.size > 0:
# filter settings table for calculations based on sql version of cet:
settings = Settings.filter_by_measure(measure).iloc[0]
f = lambda r: qc.present_value_gas_benefits(r, measure, settings, first_year)
pv_gas = avoided_cost_gas.apply(f,axis='columns').aggregate(np.sum)
else:
pv_gas = 0
avoided_gas_costs = pd.Series({
'CET_ID' : measure.CET_ID,
'ProgramID' : measure.ProgramID,
'Qi' : measure.Qi,
'GasBenefitsGross' : max(
measure[['Quantity','IRTherm','RRTherm']].product() * pv_gas,
0
),
'GasCostsGross' : max(
-measure[['Quantity','IRTherm','RRTherm']].product() * pv_gas,
0
),
'GasBenefitsNet' : max(
measure[['Quantity','IRTherm','RRTherm']].product() *
measure[['NTGRkW','MarketEffectsBenefits']].sum() * pv_gas,
0
),
'GasCostsNet' : max(
-measure[['Quantity','IRTherm','RRTherm']].product() *
measure[['NTGRkW','MarketEffectsBenefits']].sum() * pv_gas,
0
),
})
return avoided_gas_costs
def calculate_emissions_reductions(measure, AvoidedCostElectric, Emissions, CombustionTypes, Settings):
### parameters:
### measure : a pandas Series containing a single row from the
### 'data' pandas DataFrame in an 'InputMeasures' object of class
### 'EDCS_Table' or 'EDCS_Query_Results'
### AvoidedCostElectric : an instance of an 'AvoidedCostElectric' object
### of class 'EDCS_Table' or 'EDCS_Query_Results'
### Emissions : an instance of an 'Emissions' object of class 'EDCS_Table'
### or 'EDCS_Query_Results'
### CombustionTypes : an instance of a 'CombustionTypes' object of class
### 'EDCS_Table' or 'EDCS_Query_Results'
### Settings : an instance of a 'Settings' object of class 'EDCS_Table'
### or 'EDCS_Query_Results'
###
### returns:
### pandas Series containing calculated emissions reductions attributed
### to the measure
# filter emissions table:
emissions = Emissions.filter_by_measure(measure)
# filter avoided cost electric table:
avoided_cost_electric = AvoidedCostElectric.filter_by_measure(measure)
# calculate raw per-unit quarterly emissions reductions due to electric savings:
if avoided_cost_electric.size > 0:
f = lambda r: pd.Series(qc.emissions_reductions_electric(r, emissions, measure))
emissions_reductions_electric = avoided_cost_electric.apply(f, axis='columns')
else:
emissions_reductions_electric = pd.DataFrame({
'CO2' : [0],
'NOx' : [0],
'PM10' : [0],
})
emissions_reductions_electric_first_year = emissions_reductions_electric.head(4).aggregate(np.sum)
emissions_reductions_electric_lifecycle = emissions_reductions_electric.aggregate(np.sum)
# filter CombustionTypes table:
combustion_type = CombustionTypes.filter_by_measure(measure)
# filter Settings table:
settings = Settings.filter_by_measure(measure)
# calculate raw per-unit first year and lifecycle emissions reductions
# due to natural gas savings:
emissions_reductions_gas = pd.Series(
qc.emissions_reductions_gas(measure, combustion_type, settings)
)
gross_electric_coefficient = measure[['Quantity','IRkWh','RRkWh']].product()
net_electric_coefficient = (
gross_electric_coefficient *
measure[['NTGRkWh','MarketEffectsBenefits']].sum()
)
gross_gas_coefficient = measure[['Quantity','IRTherm','RRTherm']].product()
net_gas_coefficient = (
gross_gas_coefficient *
measure[['NTGRTherm','MarketEffectsBenefits']].sum()
)
emissions_reductions = pd.Series({
'CET_ID' : measure.CET_ID,
'CO2GrossElectricFirstYear' : (
gross_electric_coefficient *
emissions_reductions_electric_first_year.CO2
),
'CO2GrossGasFirstYear' : (
gross_gas_coefficient *
emissions_reductions_gas.CO2FirstYear
),
'CO2GrossFirstYear' : (
gross_electric_coefficient *
emissions_reductions_electric_first_year.CO2 +
gross_gas_coefficient *
emissions_reductions_gas.CO2FirstYear
),
'CO2GrossElectricLifecycle' : (
gross_electric_coefficient *
emissions_reductions_electric_lifecycle.CO2
),
'CO2GrossGasLifecycle' : (
gross_gas_coefficient *
emissions_reductions_gas.CO2Lifecycle
),
'CO2GrossLifecycle' : (
gross_electric_coefficient *
emissions_reductions_electric_lifecycle.CO2 +
gross_gas_coefficient *
emissions_reductions_gas.CO2Lifecycle
),
'CO2NetElectricFirstYear' : (
net_electric_coefficient *
emissions_reductions_electric_first_year.CO2
),
'CO2NetGasFirstYear' : (
net_gas_coefficient *
emissions_reductions_gas.CO2FirstYear
),
'CO2NetFirstYear' : (
net_electric_coefficient *
emissions_reductions_electric_first_year.CO2 +
net_gas_coefficient *
emissions_reductions_gas.CO2FirstYear
),
'CO2NetElectricLifecycle' : (
net_electric_coefficient *
emissions_reductions_electric_lifecycle.CO2
),
'CO2NetGasLifecycle' : (
net_gas_coefficient *
emissions_reductions_gas.CO2Lifecycle
),
'CO2NetLifecycle' : (
net_electric_coefficient *
emissions_reductions_electric_lifecycle.CO2 +
net_gas_coefficient *
emissions_reductions_gas.CO2Lifecycle
),
'NOxGrossElectricFirstYear' : (
gross_electric_coefficient *
emissions_reductions_electric_first_year.NOx
),
'NOxGrossGasFirstYear' : (
gross_gas_coefficient *
emissions_reductions_gas.NOxFirstYear
),
'NOxGrossFirstYear' : (
gross_electric_coefficient *
emissions_reductions_electric_first_year.NOx +
gross_gas_coefficient *
emissions_reductions_gas.NOxFirstYear
),
'NOxGrossElectricLifecycle' : (
gross_electric_coefficient *
emissions_reductions_electric_lifecycle.NOx
),
'NOxGrossGasLifecycle' : (
gross_gas_coefficient *
emissions_reductions_gas.NOxLifecycle
),
'NOxGrossLifecycle' : (
gross_electric_coefficient *
emissions_reductions_electric_lifecycle.NOx +
gross_gas_coefficient *
emissions_reductions_gas.NOxLifecycle
),
'NOxNetElectricFirstYear' : (
net_electric_coefficient *
emissions_reductions_electric_first_year.NOx
),
'NOxNetGasFirstYear' : (
net_gas_coefficient *
emissions_reductions_gas.NOxFirstYear
),
'NOxNetFirstYear' : (
net_electric_coefficient *
emissions_reductions_electric_first_year.NOx +
net_gas_coefficient *
emissions_reductions_gas.NOxFirstYear
),
'NOxNetElectricLifecycle' : (
net_electric_coefficient *
emissions_reductions_electric_lifecycle.NOx
),
'NOxNetGasLifecycle' : (
net_gas_coefficient *
emissions_reductions_gas.NOxLifecycle
),
'NOxNetLifecycle' : (
net_electric_coefficient *
emissions_reductions_electric_lifecycle.NOx +
net_gas_coefficient *
emissions_reductions_gas.NOxLifecycle
),
'PM10GrossFirstYear' : (
gross_gas_coefficient *
emissions_reductions_electric_first_year.PM10
),
'PM10GrossLifecycle' : (
gross_gas_coefficient *
emissions_reductions_electric_lifecycle.PM10
),
'PM10NetFirstYear' : (
net_electric_coefficient *
emissions_reductions_electric_first_year.PM10
),
'PM10NetLifecycle' : (
net_electric_coefficient *
emissions_reductions_electric_lifecycle.PM10
),
})
return emissions_reductions
def total_resource_cost_test(measure, programs, Settings, first_year):
### parameters:
### measure: a pandas Series containing a single row from a pandas
### DataFrame representing a single input measure and corresponding
### calculated avoided costs
### programs : a pandas Series containing summed measure benefits
### rolled up at the program level along with program costs
### Settings : an instance of a 'Settings' object of class 'EDCS_Table'
### or 'EDCS_Query_Results'
### first_year : an int representing the first year of programs in a cet
### run
###
### outputs:
### float value of the total resource cost test for the given measure
# filter programs based on measure, both subtotals for measure's installation quarter and totals for all quarters:
sum_columns = ['ProgramID','Count','ElectricBenefitsGross','ElectricBenefitsNet','GasBenefitsGross','GasBenefitsNet']
program = programs.get(programs.ProgramID == measure.ProgramID)
program_total = programs.get(programs.ProgramID == measure.ProgramID)[sum_columns].groupby('ProgramID').aggregate(np.sum).iloc[0]
# filter settings based on measure:
settings = Settings.filter_by_measure(measure).iloc[0]
# get quarterly and annual discount rates for exponentiation:
quarterly_discount_rate = 1 + settings.DiscountRateQtr
annual_discount_rate = 1 + settings.DiscountRateAnnual
# get measure inflation rate:
quarterly_measure_inflation_rate = 1 + measure.AnnualInflationRate / 4
# calculate the present value of cost to external parties:
present_value_external_costs = qc.present_value_external_costs(measure, quarterly_discount_rate, first_year)
# calculate the present value of the incremental cost of the measure:
present_value_gross_measure_cost = qc.present_value_gross_measure_cost(measure, quarterly_measure_inflation_rate, quarterly_discount_rate, first_year)
# calculate present value of up- and mid-stream incentives and direct installation costs:
present_value_incentives_and_direct_installation = qc.present_value_incentives_and_direct_installation(measure, quarterly_discount_rate, first_year)
# calculate present value of rebates to end-user:
present_value_rebates = qc.present_value_rebates(measure, quarterly_discount_rate, first_year)
# calculate incentives in excess of measure cost (ONLY IN CALCULATIONS TO MATCH SQL VERSION):
present_value_excess_incentives = max( 0,
present_value_incentives_and_direct_installation -
present_value_gross_measure_cost
)
present_value_gross_participant_costs = (
present_value_gross_measure_cost +
present_value_excess_incentives -
(
present_value_incentives_and_direct_installation +
present_value_rebates
)
)
#INCORRECT CALCULATION WITH MARKET EFFECTS APPLIED ONLY TO MEASURE COSTS AND EXCESS INCENTIVES:
present_value_net_participant_costs = (
measure.NTGRCost * (
present_value_gross_measure_cost +
present_value_excess_incentives -
present_value_incentives_and_direct_installation -
present_value_rebates
) +
measure.MarketEffectsCosts *
(
present_value_gross_measure_cost +
present_value_excess_incentives
)
)
# calculate present value of program-level costs:
program_cost_columns = [
'AdminCostsOverheadAndGA',
'AdminCostsOther',
'MarketingOutreach',
'DIActivity',
'DIInstallation',
'DIHardwareAndMaterials',
'DIRebateAndInspection',
'EMV',
'UserInputIncentive',
'CostsRecoveredFromOtherSources',
]
#INCORRECT CALCULATIONS USING INSTALLATION YEAR TO MATCH SQL CODE:
f = lambda r: r[program_cost_columns].sum() / annual_discount_rate ** (int(r.InstallationQuarter.split('Q')[0]) - first_year)
present_value_program_costs = program.apply(f, axis='columns').aggregate(np.sum)
# weigh program costs based on measure gross savings, if possible, otherwise by install count:
if program_total[['ElectricBenefitsGross','GasBenefitsGross']].sum() > 0:
program_weighting_gross = (
measure[['ElectricBenefitsGross','GasBenefitsGross']].sum() /
program_total[['ElectricBenefitsGross','GasBenefitsGross']].sum()
)
else:
program_weighting_gross = 1 / program_total.Count
#INCORRECT EXCLUSION OF NEGATIVE AVOIDED COSTS:
total_resource_cost_gross = (
program_weighting_gross * present_value_program_costs +
present_value_external_costs +
present_value_gross_participant_costs
)
#INCORRECT EXCLUSION OF NEGATIVE AVOIDED COSTS:
total_resource_cost_gross_no_admin = (
present_value_external_costs +
present_value_gross_participant_costs
)
# weigh program costs based on measure net savings, if possible, otherwise by install count:
if program_total[['ElectricBenefitsNet','GasBenefitsNet']].sum() > 0:
program_weighting_net = (
measure[['ElectricBenefitsNet','GasBenefitsNet']].sum() /
program_total[['ElectricBenefitsNet','GasBenefitsNet']].sum()
)
else:
program_weighting_net = 1 / program_total.Count
#INCORRECT EXCLUSION OF NEGATIVE AVOIDED COSTS:
total_resource_cost_net = (
program_weighting_net * present_value_program_costs +
present_value_external_costs +
present_value_net_participant_costs
)
#INCORRECT EXCLUSION OF NEGATIVE AVOIDED COSTS:
total_resource_cost_net_no_admin = (
present_value_external_costs +
present_value_net_participant_costs
)
#INCORRECT APPLICATION OF NEGATIVE BENEFITS TO NUMERATOR OF RATIO:
if total_resource_cost_net != 0:
total_resource_cost_ratio = (
(
measure[['ElectricBenefitsNet','GasBenefitsNet']].sum() -
measure[['ElectricCostsNet','GasCostsNet']].sum()
) /
total_resource_cost_net
)
else:
total_resource_cost_ratio = 0
#INCORRECT APPLICATION OF NEGATIVE BENEFITS TO NUMERATOR OF RATIO:
if total_resource_cost_net_no_admin != 0:
total_resource_cost_ratio_no_admin = (
(
measure[['ElectricBenefitsNet','GasBenefitsNet']].sum() -
measure[['ElectricCostsNet','GasCostsNet']].sum()
) /
total_resource_cost_net_no_admin
)
else:
total_resource_cost_ratio_no_admin = 0
return pd.Series({
'CET_ID' : measure.CET_ID,
'TotalResourceCostGross' : total_resource_cost_gross,
'TotalResourceCostGrossNoAdmin' : total_resource_cost_gross_no_admin,
'TotalResourceCostNet' : total_resource_cost_net,
'TotalResourceCostNetNoAdmin' : total_resource_cost_net_no_admin,
'TotalResourceCostRatio' : total_resource_cost_ratio,
'TotalResourceCostRatioNoAdmin' : total_resource_cost_ratio_no_admin,
})
def program_administrator_cost_test(measure, programs, Settings, first_year):
### parameters:
### measure: a pandas Series containing a single row from a pandas
### DataFrame representing a single input measure and corresponding
### calculated avoided costs
### programs : a pandas Series containing summed measure benefits
### rolled up at the program level along with program costs
### Settings : an instance of a 'Settings' object of class 'EDCS_Table'
### or 'EDCS_Query_Results'
### first_year : an int representing the first year of programs in a cet
### run
###
### outputs:
### float value of the program administrator cost test for the given measure
# filter programs based on measure, both subtotals for measure's installation quarter and totals for all quarters:
sum_columns = ['ProgramID','Count','ElectricBenefitsGross','ElectricBenefitsNet','GasBenefitsGross','GasBenefitsNet']
program = programs.get(programs.ProgramID == measure.ProgramID)
program_total = programs.get(programs.ProgramID == measure.ProgramID)[sum_columns].groupby('ProgramID').aggregate(np.sum).iloc[0]
# filter settings based on measure:
settings = Settings.filter_by_measure(measure).iloc[0]
# get quarterly and annual discount rates for exponentiation:
quarterly_discount_rate = 1 + settings.DiscountRateQtr
annual_discount_rate = 1 + settings.DiscountRateAnnual
# get measure inflation rate if present:
try:
quarterly_measure_inflation_rate = 1 + measure.MeasInflation / 4
except:
quarterly_measure_inflation_rate = 1.0
# calculate future and present values of program-level costs:
program_cost_columns = [
'AdminCostsOverheadAndGA',
'AdminCostsOther',
'MarketingOutreach',
'DIActivity',
'DIInstallation',
'DIHardwareAndMaterials',
'DIRebateAndInspection',
'EMV',
'UserInputIncentive',
'CostsRecoveredFromOtherSources',
]
#INCORRECT CALCULATION USING INSTALLATION YEAR INSTEAD OF QUARTER TO MATCH SQL CODE:
f = lambda r: r[program_cost_columns].sum() / annual_discount_rate ** (int(r.InstallationQuarter.split('Q')[0]) - first_year)
present_value_program_costs = program.apply(f, axis='columns').aggregate(np.sum)
# weigh program costs based on measure gross savings, if possible, otherwise by install count:
if program_total[['ElectricBenefitsNet','GasBenefitsNet']].sum() > 0:
program_weighting = (
measure[['ElectricBenefitsNet','GasBenefitsNet']].sum() /
program_total[['ElectricBenefitsNet','GasBenefitsNet']].sum()
)
else:
program_weighting = 1 / program_total.Count
# calculate the present value of cost to external parties:
present_value_external_costs = qc.present_value_external_costs(measure, quarterly_discount_rate, first_year)
#INCORRECT EXCLUSION OF NEGATIVE AVOIDED COSTS:
program_administrator_cost = (
program_weighting * present_value_program_costs +
present_value_external_costs
)
#INCORRECT EXCLUSION OF NEGATIVE AVOIDED COSTS:
program_administrator_cost_no_admin = (
present_value_external_costs
)
#INCORRECT APPLICATION OF NEGATIVE BENEFITS TO NUMERATOR OF RATIO:
if program_administrator_cost != 0:
program_administrator_cost_ratio = (
(
measure[['ElectricBenefitsNet','GasBenefitsNet']].sum() -
measure[['ElectricCostsNet','GasCostsNet']].sum()
) /
program_administrator_cost
)
else:
program_administrator_cost_ratio = 0
#INCORRECT APPLICATION OF NEGATIVE BENEFITS TO NUMERATOR OF RATIO:
if program_administrator_cost_no_admin != 0:
program_administrator_cost_ratio_no_admin = (
(
measure[['ElectricBenefitsNet','GasBenefitsNet']].sum() -
measure[['ElectricCostsNet','GasCostsNet']].sum()
) /
program_administrator_cost_no_admin
)
else:
program_administrator_cost_ratio_no_admin = 0
return pd.Series({
'CET_ID' : measure.CET_ID,
'ProgramAdministratorCost' : program_administrator_cost,
'ProgramAdministratorCostNoAdmin' : program_administrator_cost_no_admin,
'ProgramAdministratorCostRatio' : program_administrator_cost_ratio,
'ProgramAdministratorCostRatioNoAdmin' : program_administrator_cost_ratio_no_admin,
})