DiscRates class¶
This class contains the discount rates for every year and discounts given values. Its attributes are:
tag (Tag): information about the source data
years (np.array): years
rates (np.array): discount rates for each year (between 0 and 1)
[1]:
from climada.entity import DiscRates
help(DiscRates)
2019-10-29 21:39:51,385 - climada - DEBUG - Loading default config file: /Users/aznarsig/Documents/Python/climada_python/climada/conf/defaults.conf
Help on class DiscRates in module climada.entity.disc_rates.base:
class DiscRates(builtins.object)
| Defines discount rates and basic methods. Loads from
| files with format defined in FILE_EXT.
|
| Attributes:
| tag (Tag): information about the source data
| years (np.array): years
| rates (np.array): discount rates for each year (between 0 and 1)
|
| Methods defined here:
|
| __init__(self)
| Empty initialization.
|
| Examples:
| Fill discount rates with values and check consistency data:
|
| >>> disc_rates = DiscRates()
| >>> disc_rates.years = np.array([2000, 2001])
| >>> disc_rates.rates = np.array([0.02, 0.02])
| >>> disc_rates.check()
|
| Read discount rates from year_2050.mat and checks consistency data.
|
| >>> disc_rates = DiscRates(ENT_TEMPLATE_XLS)
|
| append(self, disc_rates)
| Check and append discount rates to current DiscRates. Overwrite
| discount rate if same year.
|
| Parameters:
| disc_rates (DiscRates): DiscRates instance to append
|
| Raises:
| ValueError
|
| check(self)
| Check attributes consistency.
|
| Raises:
| ValueError
|
| clear(self)
| Reinitialize attributes.
|
| net_present_value(self, ini_year, end_year, val_years)
| Compute net present value between present year and future year.
|
| Parameters:
| ini_year (float): initial year
| end_year (float): end year
| val_years (np.array): cash flow at each year btw ini_year and
| end_year (both included)
| Returns:
| float
|
| plot(self, axis=None, **kwargs)
| Plot discount rates per year.
|
| Parameters:
| axis (matplotlib.axes._subplots.AxesSubplot, optional): axis to use
| kwargs (optional): arguments for plot matplotlib function, e.g. marker='x'
|
| Returns:
| matplotlib.axes._subplots.AxesSubplot
|
| read_excel(self, file_name, description='', var_names={'sheet_name': 'discount', 'col_name': {'year': 'year', 'disc': 'discount_rate'}})
| Read excel file following template and store variables.
|
| Parameters:
| file_name (str): absolute file name
| description (str, optional): description of the data
| var_names (dict, optional): name of the variables in the file
|
| read_mat(self, file_name, description='', var_names={'sup_field_name': 'entity', 'field_name': 'discount', 'var_name': {'year': 'year', 'disc': 'discount_rate'}})
| Read MATLAB file generated with previous MATLAB CLIMADA version.
|
| Parameters:
| file_name (str): absolute file name
| description (str, optional): description of the data
| var_names (dict, optional): name of the variables in the file
|
| select(self, year_range)
| Select discount rates in given years.
|
| Parameters:
| year_range (np.array): continuous sequence of selected years.
|
| Returns:
| DiscRates
|
| write_excel(self, file_name, var_names={'sheet_name': 'discount', 'col_name': {'year': 'year', 'disc': 'discount_rate'}})
| Write excel file following template.
|
| Parameters:
| file_name (str): absolute file name to write
| var_names (dict, optional): name of the variables in the file
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
An example of use:
[2]:
%matplotlib inline
import numpy as np
from climada.entity import DiscRates
# define discount rates
disc = DiscRates()
disc.years = np.arange(1950, 2100)
disc.rates = np.ones(disc.years.size) * 0.02
disc.rates[51:55] = 0.025
disc.rates[95:120] = 0.035
disc.check()
disc.plot()
# Compute net present value between present year and future year.
ini_year = 2019
end_year = 2050
val_years = np.zeros(end_year-ini_year+1)
val_years[0] = 100000000 # initial investment
val_years[10:] = 75000 # maintenance from 10th year
npv = disc.net_present_value(ini_year, end_year, val_years)
print('net present value: {:.5e}'.format(npv))
net present value: 1.01099e+08
Read discount rates of an Excel file¶
Discount rates defined in an excel file following the template provided in sheet discount
of climada_python/data/system/entity_template.xlsx
can be ingested directly using the method read_excel()
.
[3]:
from climada.entity import DiscRates
from climada.util import ENT_TEMPLATE_XLS
# Fill DataFrame from Excel file
file_name = ENT_TEMPLATE_XLS # provide absolute path of the excel file
disc = DiscRates()
disc.read_excel(file_name)
print('Read file:', disc.tag.file_name)
disc.plot()
Read file: /Users/aznarsig/Documents/Python/climada_python/data/system/entity_template.xlsx
[3]:
<matplotlib.axes._subplots.AxesSubplot at 0x1c248d4978>
Write discount rates¶
Discount rates defined in an excel file following the template provided in sheet discount
of climada_python/data/system/entity_template.xlsx
can be ingested directly using the method read_excel()
.
[4]:
from climada.entity import DiscRates
from climada.util import ENT_TEMPLATE_XLS
# Fill DataFrame from Excel file
file_name = ENT_TEMPLATE_XLS # provide absolute path of the excel file
disc = DiscRates()
disc.read_excel(file_name)
# write file
disc.write_excel('results/tutorial_disc.xlsx')
Pickle can always be used as well:
[5]:
from climada.util.save import save
# this generates a results folder in the current path and stores the output there
save('tutorial_disc.p', disc)
2019-10-29 21:39:55,425 - climada.util.save - INFO - Written file /Users/aznarsig/Documents/Python/climada_python/doc/tutorial/results/tutorial_disc.p