コード例 #1
0
def solve_it(**cc):
    tree = DeterministicTree(N)
    for s in tree.nodes:
        tree.values[s] = zbar
    model = import_tree_model('models.yaml', key='optimal', tree=tree)
    model.calibration.update(cc)
    sol = model.solve(verbose=True)
    df = numpy.concatenate( [get_ts(tree, sol, varname)[:,None] for varname in ['e','f', 'Gamma']], axis=1 )
    df = pandas.DataFrame(df, columns=['e','f','Gamma'])
    return df
def perfect_foresight(calib):

    from calibrations import list_of_calibrations
    calibration = calib.copy()

    from bttt.trees import DeterministicTree, get_ts
    import numpy
    import pandas
    import copy

    from bttt.model import import_tree_model

    # R0 = calibration['Rbar']
    N = calibration['N']
    shock = calibration['zbar']
    model = calibration['model']

    # cc = copy.copy(calibration)
    # cc['Rbar'] = R0

    # solve deterministic case
    etree = DeterministicTree(N)
    for s in etree.nodes:
        etree.values[s] = shock

    model = import_tree_model('models.yaml', key=model, tree=etree)
    model.calibration.update(**calib)
    sol = model.solve(verbose=True, solver_options=dict(eps2=1e-6))

    columns = ['Gamma', 'e', 'f']
    tab = [get_ts(etree, sol, c) for c in columns]
    tab = numpy.row_stack(tab).T

    df = pandas.DataFrame(tab, columns=columns)

    return df
コード例 #3
0
ファイル: example.py プロジェクト: albop/backtothetrees
from bttt.model import import_tree_model
from bttt.trees import get_ts, DeterministicTree

N = 40
etree = DeterministicTree(N)
for n in etree.nodes:
    etree.values[n] = 0.1

model = import_tree_model('example.yaml', tree=etree)

sol = model.solve()

sim = get_ts(etree, sol, 'e')


print(sim)
コード例 #4
0
N = 25
T = 25


calib0 = calib.copy()
beta = calib0['beta']
a = calib0['a']
p = calib0['p']
zbar = calib0['zbar']
max_R=5


tree = DeterministicTree(N)
for s in tree.nodes:
    tree.values[s] = zbar
    model = import_tree_model('models.yaml', key='stochastic', tree=tree)

def solve_it(**cc):
    tree = DeterministicTree(N)
    for s in tree.nodes:
        tree.values[s] = zbar
    model = import_tree_model('models.yaml', key='optimal', tree=tree)
    model.calibration.update(cc)
    sol = model.solve(verbose=True)
    df = numpy.concatenate( [get_ts(tree, sol, varname)[:,None] for varname in ['e','f', 'Gamma']], axis=1 )
    df = pandas.DataFrame(df, columns=['e','f','Gamma'])
    return df

from collections import OrderedDict

コード例 #5
0
ファイル: example.py プロジェクト: albop/backtothetrees
from bttt.model import import_tree_model
from bttt.trees import get_ts, DeterministicTree

N = 40
etree = DeterministicTree(N)
for n in etree.nodes:
    etree.values[n] = 0.1

model = import_tree_model('example.yaml', tree=etree)

sol = model.solve()

sim = get_ts(etree, sol, 'e')

print(sim)
コード例 #6
0
        if beta < 0.82:
            max_R = 4
        else:
            max_R = 4
        r = calib['Rbar']
        # sol = solve_time_consistent(max_R=2, **v)
        sol = solve_time_consistent(max_R=max_R, order=100, **v)
        sim_tc = simulate(r, sol, T)
        results[c] = sim_tc
    else:
        print(p, c[1])
        # if p == 1:
        tree = DeterministicTree(N)
        for s in tree.nodes:
            tree.values[s] = zbar
        model = import_tree_model('models.yaml', key=c[1], tree=tree)
        model.calibration.update(calib)
        sol = model.solve(verbose=True)
        df = numpy.concatenate([
            get_ts(tree, sol, varname)[:, None]
            for varname in ['e', 'f', 'Gamma']
        ],
                               axis=1)
        df = pandas.DataFrame(df, columns=['e', 'f', 'Gamma'])
        results[c] = df


def Gamma(t, e, parm):
    tot = 0
    estar = parm['estar']
    beta = parm['beta']
コード例 #7
0
    else:
        etree.values[s] = 0.0


def get_ts(etree, sol, vname, terminal_state=None, ts_ind=0):
    import numpy
    if terminal_state is None:
        terminal_states = [e for e in etree.nodes if len(e) == len(etree)]
        terminal_state = terminal_states[ts_ind]
    history = etree.history(terminal_state)
    his_inds = [etree.nodes.index(e) for e in history]
    vals = [sol["{}_{}".format(vname, h)] for h in his_inds]
    return numpy.array(vals).astype(dtype=float)


model = import_tree_model("models.yaml", tree=etree, key="stochastic")
model.calibration["Rbar"] = 1.0
model.calibration["beta"] = 0.8 / (0.8 + 0.15)
model.calibration["a"] = 0.8
model.calibration["c"] = 0.15

sol = model.solve(verbose=True,
                  linear=True,
                  solver_options={
                      "presteps": 2,
                      'tol': 1e-5
                  })

from numpy import *
from matplotlib import pyplot as mplt
from pandas import DataFrame