Пример #1
0
    def save_output(self, m, prefix=None):
        """Save attributes calculated in `sensitivity_analysis` into the file prefix + `sensitivity_output`
        in the `data` directory in the current working directory.

        Furthermore, the perpetuity yield, the discount factor for the last period is calculated, and SCC,
        expected damage and risk premium for the first period is calculated and saved in into the file
        prefix + `tree` in the `data` directory in the current working directory. If there is no `data` directory, 
        one is created.

        Parameters
        ----------
        m : ndarray or list
            array of mitigation
        prefix : str, optional
            prefix to be added to file_name

        """
        end_price = find_term_structure(m, self.utility, 0.01)
        perp_yield = perpetuity_yield(end_price, self.sdf_tree.periods[-2])

        damage_scale = self.utility.cost.price(0, m[0], 0) / (
            self.net_discount_damages.sum() + self.risk_premiums.sum())
        scaled_discounted_ed = self.net_discount_damages * damage_scale
        scaled_risk_premiums = self.risk_premiums * damage_scale

        if prefix is not None:
            prefix += "_"
        else:
            prefix = ""

        write_columns_csv([
            self.expected_sdf, self.net_discount_damages,
            self.expected_damages, self.risk_premiums, self.cross_sdf_damages,
            self.discounted_expected_damages, self.cov_term,
            scaled_discounted_ed, scaled_risk_premiums
        ], prefix + "sensitivity_output", [
            "Year", "Discount Prices", "Net Expected Damages",
            "Expected Damages", "Risk Premium", "Cross SDF & Damages",
            "Discounted Expected Damages", "Cov Term",
            "Scaled Net Expected Damages", "Scaled Risk Premiums"
        ], [self.sdf_tree.periods.astype(int) + 2015])

        append_to_existing(
            [[end_price], [perp_yield], [scaled_discounted_ed.sum()],
             [scaled_risk_premiums.sum()],
             [self.utility.cost.price(0, m[0], 0)]],
            prefix + "sensitivity_output",
            header=[
                "Zero Bound Price", "Perp Yield", "Expected Damages",
                "Risk Premium", "SCC"
            ],
            start_char='\n')

        store_trees(prefix=prefix,
                    tree_dict={
                        'SDF': self.sdf_tree,
                        'DeltaConsumption': self.delta_cons_tree
                    })
Пример #2
0
    def write_columns(self, file_name, header, start_year=2015, delimiter=";"):
        """Save values in `tree` as columns into file  `file_name` in the 
        'data' directory in the current working directory. If there is no 'data' 
        directory, one is created. 
            
        +------------+------------+-----------+
        |    Year    |    Node    |  header   |
        +============+============+===========+
        | start_year |     0      |   val0    |
        +------------+------------+-----------+
        |     ..     |     ..     |    ..     |
        +------------+------------+-----------+
        
        Parameters
        ----------
        file_name : str
            name of saved file
        header : str
            description of values in tree
        start_year : int, optional
            start year of analysis
        delimiter : str, optional
            delimiter in file

        """
        from ezclimate.tools import write_columns_csv, file_exists
        if file_exists(file_name):
            #print("***DEBUG File "+file_name+" exists.")
            self.write_columns_existing(file_name, header)
        else:
            #print("***DEBUG File "+file_name+" doesn't exist.")
            real_times = self.decision_times[:-1]
            years = []
            nodes = []
            output_lst = []
            k = 0
            for t in real_times:
                for n in range(len(self.tree[t])):
                    years.append(t + start_year)
                    nodes.append(k)
                    output_lst.append(self.tree[t][n])
                    k += 1
            #print('*** in write_columns, header = ',header,'lst = ',output_lst)
            write_columns_csv(lst=[output_lst],
                              file_name=file_name,
                              header=["Year", "Node", header],
                              index=[years, nodes],
                              delimiter=delimiter)
Пример #3
0
    def save_output(self, prefix=None):
        if prefix is not None:
            prefix += "_"
        else:
            prefix = ""

        write_columns_csv([
            self.con_cost, [self.delta_c], [self.delta_c_billions],
            [self.delta_emission_gton], [self.deadweight], self.delta_u,
            self.marginal_benefit, [self.marginal_cost]
        ], prefix + self.run_name + "_constraint_output", [
            "Constraint Cost", "Delta Consumption", "Delta Consumption $b",
            "Delta Emission Gton", "Deadweight Cost",
            "Marginal Impact Utility", "Marginal Benefit Emissions Reduction",
            "Marginal Cost Emission Reduction"
        ])
Пример #4
0
    def save_output(self, m, prefix=None):
        """Function to save calculated values in `calculate_output` in the file `prefix` + 'node_period_output' 
        in the 'data' directory in the current working directory. 

        The function also saves the values calculated in the utility function in the file
        `prefix` + 'tree' in the 'data' directory in the current working directory. 

        If there is no 'data' directory, one is created. 

        Parameters
        ----------
        m : ndarray or list
            array of mitigation
        prefix : str, optional
            prefix to be added to file_name

        """
        if prefix is not None:
            prefix += "_"
        else:
            prefix = ""

        #print('in ClimateOutput.save_output(), prefix =',prefix)
        write_columns_csv([
            m, self.prices, self.ave_mitigations, self.ave_emissions,
            self.ghg_levels
        ], prefix + "node_period_output", [
            "Node", "Mitigation", "Prices", "Average Mitigation",
            "Average Emission", "GHG Level"
        ], [list(range(len(m)))])

        append_to_existing([
            self.expected_period_price, self.expected_period_mitigation,
            self.expected_period_emissions
        ],
                           prefix + "node_period_output",
                           header=[
                               "Period", "Expected Price",
                               "Expected Mitigation", "Expected Emission"
                           ],
                           index=[list(range(self.utility.tree.num_periods))],
                           start_char='\n')

        tree_dict = self.utility.utility(m, return_trees=True)
        store_trees(prefix=prefix, tree_dict=tree_dict)
Пример #5
0
 def _write_to_file(self):
     filename = "simulated_damages"
     write_columns_csv(self.d[0].T, filename)
     for arr in self.d[1:]:
         append_to_existing(arr.T, filename, start_char='#')