Example #1
0
    def build_endogenous_variables(self, code):
        """Return code to store endogenous variable values.

        Parameters
        ==========
        code : string
            Code script containing endogenous model variables

        Returns
        =======
        variables : string
            Python code to insert endogenous variable values into a Dictionary

        See also
        ========
        FSIC.parser.code.identify_variables()

        """
        from FSIC.parser.code import identify_variables
        variables = identify_variables(code)
        variables = variables['endogenous']
        variables = [
            ('values[\'' + v.replace('self.', '') + '\'] = ' + v + '[period]')
            for v in variables
        ]
        variables = '\n'.join(variables)
        return variables
Example #2
0
    def build_results(self, code):
        """Return code to return model results as a DataFrame.

        Parameters
        ==========
        code : string
            Code script containing all model variables

        Returns
        =======
        results : string
            Python code to generate a results DataFrame

        See also
        ========
        FSIC.parser.code.identify_variables()

        """
        from FSIC.parser.code import identify_variables
        variables = identify_variables(code)
        variables = variables['endogenous'] + variables['exogenous']
        results = 'results = DataFrame({' + '\n\t' + (',\n\t'.join(
            ['\'' + v.replace('self.', '') + '\': ' + v
             for v in variables]) + '})')
        return results
Example #3
0
    def build_initialise(self, code):
        """Return code to initialise the variables of a model.

        Parameters
        ==========
        code : string
            Code script containing all model variables

        Returns
        =======
        initialise : string
            Python code to initialise model variables

        See also
        ========
        FSIC.parser.code.identify_variables()

        """
        from FSIC.parser.code import identify_variables
        variables = identify_variables(code)
        variables = variables['endogenous'] + variables['exogenous']
        initialise = [
            v + (' = Series(default, '
                 'index=self.full_span, '
                 'dtype=dtype)') for v in variables
        ]
        initialise = '\n'.join(initialise)
        return initialise
Example #4
0
    def build_results(self, code):
        """Return code to return model results as a DataFrame.

        Parameters
        ==========
        code : string
            Code script containing all model variables

        Returns
        =======
        results : string
            Python code to generate a results DataFrame

        See also
        ========
        FSIC.parser.code.identify_variables()

        """
        from FSIC.parser.code import identify_variables
        variables = identify_variables(code)
        variables = variables['endogenous'] + variables['exogenous']
        results = 'results = DataFrame({' + '\n\t' + (
            ',\n\t'.join(
                ['\'' + v.replace('self.', '') + '\': ' + v
                 for v in variables]) + '})')
        return results
Example #5
0
    def build_endogenous_variables(self, code):
        """Return code to store endogenous variable values.

        Parameters
        ==========
        code : string
            Code script containing endogenous model variables

        Returns
        =======
        variables : string
            Python code to insert endogenous variable values into a Dictionary

        See also
        ========
        FSIC.parser.code.identify_variables()

        """
        from FSIC.parser.code import identify_variables
        variables = identify_variables(code)
        variables = variables['endogenous']
        variables = [('values[\'' +
                      v.replace('self.', '') +
                      '\'] = ' + v + '[period]')
                     for v in variables]
        variables = '\n'.join(variables)
        return variables
Example #6
0
    def build_initialise(self, code):
        """Return code to initialise the variables of a model.

        Parameters
        ==========
        code : string
            Code script containing all model variables

        Returns
        =======
        initialise : string
            Python code to initialise model variables

        See also
        ========
        FSIC.parser.code.identify_variables()

        """
        from FSIC.parser.code import identify_variables
        variables = identify_variables(code)
        variables = variables['endogenous'] + variables['exogenous']
        initialise = [v + (' = Series(default, '
                           'index=self.full_span, '
                           'dtype=dtype)')
                      for v in variables]
        initialise = '\n'.join(initialise)
        return initialise
Example #7
0
def make_graph(equations, warn=True):
    """Return `equations` as a NetworkX DiGraph.

    Parameters
    ==========
    equations : list of strings
        List of equations to reorder, one equation per element
    warn : boolean
        If `True`, print a warning if there is more than one equation with the
        same endogenous variable

    Returns
    =======
    G : NetworkX DiGraph object
        Directed graph representation of `equations`. Nodes signifying
        endogenous variables have, as an attribute, the associated equation
        string(s). If `warn` is `True`, this function prints a warning if
        multiple equations are found for a single endogenous variable.

    """
    from FSIC.parser.code import identify_variables
    G = nx.DiGraph()
    # Initialise dictionary to store equations
    node_equations = {}
    # Loop by equation
    for e in equations:
        v = identify_variables(e, suffix=r'\[.+?\]')
        # Extract endogenous variable (should only be one)
        n = v['endogenous']
        if len(n) != 1:
            raise ValueError('Expected just one endogenous variable')
        n = n[0]
        # Extract exogenous variable(s) and add edges
        x = v['exogenous']
        for term in x:
            G.add_edge(term, n)
        # Store equation with the endogenous variable as the key
        if n in node_equations:
            if warn:
                warnings.warn(
                    'An endogenous variable appears as the left hand-side '
                    'variable in more than one equation',
                    Warning)
            node_equations[n] = node_equations[n] + [e]
        else:
            node_equations[n] = [e]
    # Add equations to node attributes
    nx.set_node_attributes(G, 'equations', node_equations)
    # Return
    return G