In [15]:

include("Interpolation.jl")




Out[15]:

my_lin_interp (generic function with 1 method)




In [54]:

#=
Solving the optimal growth problem via value function iteration.

@author : Spencer Lyon <spencer.lyon@nyu.edu>

@date : 2014-07-05

References
----------

Simple port of the file quantecon.models.optgrowth

http://quant-econ.net/jl/dp_intro.html
=#

#=
This type defines the primitives representing the growth model. The
default values are

f(k) = k**alpha, i.e, Cobb-Douglas production function
u(c) = ln(c), i.e, log utility

See the constructor below for details
=#

#using Interpolations#ここを書き換え
using Optim
using Plots
pyplot()
"""
Neoclassical growth model

##### Fields

- f::Function : Production function
- bet::Real : Discount factor in (0, 1)
- u::Function : Utility function
- grid_max::Int : Maximum for grid over savings values
- grid_size::Int : Number of points in grid for savings values
- grid::LinSpace{Float64} : The grid for savings values

"""
type GrowthModel
f::Function
bet::Float64
u::Function
grid_max::Int
grid_size::Int
grid::LinSpace{Float64}
end

default_f(k) = k^0.65
default_u(c) = log(c)

"""
Constructor of GrowthModel

##### Arguments

- f::Function(k->k^0.65) : Production function
- bet::Real(0.95) : Discount factor in (0, 1)
- u::Function(log) : Utility function
- grid_max::Int(2) : Maximum for grid over savings values
- grid_size::Int(150) : Number of points in grid for savings values

"""
function GrowthModel(f=default_f, bet=0.95, u=default_u, grid_max=2,
grid_size=150)
grid = linspace(1e-6, grid_max, grid_size)
return GrowthModel(f, bet, u, grid_max, grid_size, grid)
end

"""
Apply the Bellman operator for a given model and initial value.

##### Arguments

- g::GrowthModel : Instance of GrowthModel
- w::Vector: Current guess for the value function
- out::Vector : Storage for output.
- ;ret_policy::Bool(false): Toggles return of value or policy functions

##### Returns

None, out is updated in place. If ret_policy == true out is filled with the
policy function, otherwise the value function is stored in out.

"""
function bellman_operator!(g::GrowthModel, w::Vector, out::Vector;
ret_policy::Bool=false)
# Apply linear interpolation to w
Aw = my_lin_interp(g.grid, w)

for (i, k) in enumerate(g.grid)
objective(c) = - g.u(c) - g.bet * Aw(g.f(k) - c)
res = optimize(objective, 1e-6, g.f(k))
c_star = res.minimum

if ret_policy
# set the policy equal to the optimal c
out[i] = c_star
else
# set Tw[i] equal to max_c { u(c) + beta w(f(k_i) - c)}
out[i] = - objective(c_star)
end
end

return out
end

function bellman_operator(g::GrowthModel, w::Vector;
ret_policy::Bool=false)
out = similar(w)
bellman_operator!(g, w, out, ret_policy=ret_policy)
end

"""
Extract the greedy policy (policy function) of the model.

##### Arguments

- g::GrowthModel : Instance of GrowthModel
- w::Vector: Current guess for the value function
- out::Vector : Storage for output

##### Returns

None, out is updated in place to hold the policy function

"""
function get_greedy!(g::GrowthModel, w::Vector, out::Vector)
bellman_operator!(g, w, out, ret_policy=true)
end

get_greedy(g::GrowthModel, w::Vector) = bellman_operator(g, w, ret_policy=true)

function main(n::Int=35)
gm = GrowthModel()
w_init = 5 .* log(gm.grid) .- 25  # An initial condition -- fairly arbitrary
w = copy(w_init)

ws = []
colors = []
bet = gm.bet
alpha = 0.65
## Exact solution
ab = alpha * bet
c1 = (log(1 - ab) + log(ab) * ab / (1 - ab)) / (1 - bet)
c2 = alpha / (1 - ab)
v_star(k) = c1 .+ c2 .* log(k)

for i=1:n
w = bellman_operator(gm, w)
push!(ws, w)
push!(colors, RGBA(0, 0, 0, i/n))
end

p = plot(gm.grid, w_init, color=:green, linewidth=2, alpha=0.6,
label="initial condition")
plot!(gm.grid, ws, color=colors', label="", linewidth=2)
plot!(gm.grid, v_star(gm.grid), color=:blue, linewidth=2, alpha=0.8,
label="true value function")
plot!(ylims=(-40, -20), xlims=(minimum(gm.grid), maximum(gm.grid)))

return p
end




Out[54]:

main (generic function with 2 methods)




In [55]:

main()




Out[55]:




In [65]:

#=
Compute the fixed point of a given operator T, starting from
specified initial condition v.

@author : Spencer Lyon <spencer.lyon@nyu.edu>

@date: 2014-07-05

References
----------

http://quant-econ.net/jl/dp_intro.html
=#

"""
Repeatedly apply a function to search for a fixed point

Approximates T^∞ v, where T is an operator (function) and v is an initial
guess for the fixed point. Will terminate either when T^{k+1}(v) - T^k v <
err_tol or max_iter iterations has been exceeded.

Provided that T is a contraction mapping or similar,  the return value will
be an approximation to the fixed point of T.

##### Arguments

* T: A function representing the operator T
* v::TV: The initial condition. An object of type TV
* ;err_tol(1e-3): Stopping tolerance for iterations
* ;max_iter(50): Maximum number of iterations
* ;verbose(2): Level of feedback (0 for no output, 1 for warnings only, 2
for warning and convergence messages during iteration)
* ;print_skip(10) : if verbose == 2, how many iterations to apply between
print messages

##### Returns
---

* '::TV': The fixed point of the operator T. Has type TV

##### Example

julia
using QuantEcon
T(x, μ) = 4.0 * μ * x * (1.0 - x)
x_star = compute_fixed_point(x->T(x, 0.3), 0.4)  # (4μ - 1)/(4μ)


"""
function compute_fixed_point{TV}(T::Function,
v::TV;
err_tol=1e-4,
max_iter=100,
verbose=2,
print_skip=10)

if !(verbose in (0, 1, 2))
throw(ArgumentError("verbose should be 0, 1 or 2"))
end

iterate = 0
err = err_tol + 1
while iterate < max_iter && err > err_tol
new_v = T(v)::TV
iterate += 1
err = Base.maxabs(new_v - v)
if verbose == 2
if iterate % print_skip == 0
println("Compute iterate $iterate with error$err")
end
end
v = new_v
end

if verbose >= 1
if iterate == max_iter
warn("max_iter attained in compute_fixed_point")
elseif verbose == 2
println("Converged in $iterate steps") end end return v end   Out[65]: compute_fixed_point (generic function with 1 method)   In [71]: using PlotlyJS using Interact #using QuantEcon # set up the model alpha, bet = 0.65, 0.95 gm = GrowthModel() w = 5 .* log(gm.grid) .- 25 sigma = get_greedy(gm, w) bellman(w) = bellman_operator(gm, w) true_sigma = (1 - alpha*bet) .* collect(gm.grid).^alpha w = 5 .* gm.u(collect(gm.grid)) .- 25 # Initial condition # construct the plot with the initial condition above layout = Layout(yaxis_range=(0, 1), xaxis_range=(0, 2), title="Initial condition") t1 = PlotlyJS.scatter(x=gm.grid, y=true_sigma, marker_color="black", line_opacity=0.8, name="true optimal policy") t2 = PlotlyJS.scatter(x=gm.grid, y=sigma, marker_color="blue", line_opacity=0.8, name="approximate optimal policy") p = PlotlyJS.plot([t1, t2], layout) display(p) # now for n=1, 2, ..., 10 compute the policy after n VFI iterations vf_n = Dict(0=>get_greedy(gm, w)) for n in 1:10 v_star = compute_fixed_point(bellman, w, max_iter=n, verbose=false) vf_n[n] = get_greedy(gm, v_star) end #plot!(gm.grid, sigma, color="black", label="initial optimal policy") #plot!(gm.grid, true_sigma, color="red", label="true optimal policy") # construct an interactive plot using the data we computed above @manipulate for n in 1:10 # update title relayout!(p, title="$n value function iterations")

# update the y values on approximation
restyle!(p, 2; y=(vf_n[n],))
end




(function (IPython, $, _, MathJax) {$.event.special.destroyed = {
remove: function(o) {
if (o.handler) {
o.handler.apply(this, arguments)
}
}
}

var OutputArea = IPython.version >= "4.0.0" ? require("notebook/js/outputarea").OutputArea : IPython.OutputArea;

var redrawValue = function (container, type, val) {
var selector = $("<div/>"); var oa = new OutputArea(_.extend(selector, { selector: selector, prompt_area: true, events: IPython.events, keyboard_manager: IPython.keyboard_manager })); // Hack to work with IPython 2.1.0 switch (type) { case "image/png": var _src = 'data:' + type + ';base64,' + val;$(container).find("img").attr('src', _src);
break;
default:
var toinsert = OutputArea.append_map[type].apply(
oa, [val, {}, selector]
);
$(container).empty().append(toinsert.contents()); selector.remove(); } if (type === "text/latex" && MathJax) { MathJax.Hub.Queue(["Typeset", MathJax.Hub, toinsert.get(0)]); } }$(document).ready(function() {
function initComm(evt, data) {
var comm_manager = data.kernel.comm_manager;
//_.extend(comm_manager.targets, require("widgets/js/widget"))
comm_manager.register_target("Signal", function (comm) {
comm.on_msg(function (msg) {
var val = msg.content.data.value;
$(".signal-" + comm.comm_id).each(function() { var type =$(this).data("type");
if (val[type]) {
redrawValue(this, type, val[type], type);
}
});
delete val;
delete msg.content.data.value;
});
});

// coordingate with Comm and redraw Signals
// XXX: Test using Reactive here to improve performance
$([IPython.events]).on( 'output_appended.OutputArea', function (event, type, value, md, toinsert) { if (md && md.reactive) { // console.log(md.comm_id); toinsert.addClass("signal-" + md.comm_id); toinsert.data("type", type); // Signal back indicating the mimetype required var comm_manager = IPython.notebook.kernel.comm_manager; var comm = comm_manager.comms[md.comm_id]; comm.then(function (c) { c.send({action: "subscribe_mime", mime: type}); toinsert.bind("destroyed", function() { c.send({action: "unsubscribe_mime", mime: type}); }); }) } }); } try { // try to initialize right away. otherwise, wait on the status_started event. initComm(undefined, IPython.notebook); } catch (e) {$([IPython.events]).on('kernel_created.Kernel kernel_created.Session', initComm);
}
});
})(IPython, jQuery, _, MathJax);

$("#interact-js-shim").bind("destroyed", function () { if (window.interactLoadedFlag) { console.warn("JavaScript required by Interact will be removed if you remove this cell or run using Interact more than once.") } })$([IPython.events]).on("kernel_starting.Kernel kernel_restarting.Kernel", function () { window.interactLoadedFlag = false })

window.PLOTLYENV=window.PLOTLYENV || {};
window.PLOTLYENV.BASE_URL="https://plot.ly";
require(['plotly'], function(Plotly) {
Plotly.newPlot('c65a7477-70ff-449a-ad60-ff73a6aaf3c3', [{"y":[4.815389700112689e-5,0.023214041205814284,0.03642585323319183,0.04740951874165922,0.05715759844878718,0.06607914728063921,0.07439290858298067,0.08223306004022372,0.08968934273863906,0.09682548675692278,0.10368879420909201,0.11031558650058396,0.11673451507177979,0.12296868188213599,0.1290370561305039,0.134955454296016,0.14073723817916525,0.1463938245975861,0.15193506561544085,0.1573695375361743,0.16270476418570157,0.16794739195447622,0.17310332881431714,0.17817785601925026,0.18317571880800299,0.18810120076300924,0.19295818530450654,0.19775020695298137,0.2024804943769437,0.2071520067877742,0.21176746490301532,0.21632937744213215,0.22084006392218014,0.22530167436919113,0.22971620644309804,0.2340855203814295,0.23841135209378678,0.24269532468078456,0.2469389586043515,0.25114368069851695,0.2553108321791301,0.2594416757859007,0.26353740216956084,0.2675991356199583,0.2716279392167893,0.2756248194729279,0.2795907305304697,0.2835265779613374,0.28743322221731177,0.2913114817684362,0.2951621359637112,0.2989859276437014,0.30278356553099894,0.3065557264213309,0.31030305719537354,0.3140261766689854,0.3177256772975313,0.32140212674819724,0.3250560693526529,0.328688027451067,0.33229850263730226,0.33588797691407674,0.3394569137659687,0.34300575915733456,0.346534942461501,0.350044877326962,0.35353596248575203,0.35700858250867257,0.3604631085116041,0.36389989881674434,0.36731929957225734,0.37072164533350777,0.37410725960876495,0.37747645537201363,0.3808295355452747,0.38416679345263716,0.3874885132480135,0.390794970318465,0.39408643166479024,0.39736315626093427,0.40062539539364744,0.40387339298371533,0.4071073858899706,0.4103276041972117,0.41353427148906097,0.4167276051067209,0.41990781639451374,0.4230751109330257,0.42622968876061545,0.429371744583995,0.4325014679785361,0.4356190435789139,0.4387246512606532,0.44181846631310784,0.4449006596043616,0.4479713977385137,0.45103084320577264,0.45407915452576303,0.4571164863844151,0.46014298976478946,0.4631588120721624,0.4661640972536795,0.46915898591286476,0.47214361541925315,0.475118120013402,0.47808263090751574,0.48103727638190985,0.48398218187752134,0.48691747008466485,0.4898432610282197,0.49275967214942085,0.49566681838442267,0.49856481223978694,0.5014537638650437,0.504333781122463,0.5072049696541685,0.5100674329467163,0.5129212723932542,0.5157665873533755,0.518603475210767,0.5214320314287549,0.5242523496038387,0.5270645215173033,0.529868637184994,0.5326647849053325,0.5354530513056527,0.5382335213869236,0.5410062785669321,0.5437714047219885,0.5465289802272159,0.5492790839954832,0.5520217935150367,0.5547571848858843,0.5574853328549816,0.5602063108502692,0.5629201910136055,0.5656270442326411,0.5683269401716737,0.5710199473015255,0.5737061329284786,0.5763855632223074,0.5790583032434405,0.5817244169692857,0.5843839673197487,0.5870370161819782,0.5896836244343626,0.5923238519698091,0.5949577577183285,0.5975853996689536,0.6002068348910145],"name":"true optimal policy","type":"scatter","line":{"opacity":0.8},"x":[1.0e-6,0.013423812080536912,0.026846624161073824,0.04026943624161074,0.053692248322147654,0.06711506040268457,0.08053787248322147,0.09396068456375839,0.1073834966442953,0.12080630872483221,0.13422912080536914,0.14765193288590603,0.16107474496644295,0.17449755704697986,0.18792036912751678,0.2013431812080537,0.2147659932885906,0.22818880536912753,0.24161161744966442,0.25503442953020133,0.26845724161073825,0.28188005369127517,0.2953028657718121,0.308725677852349,0.3221484899328859,0.33557130201342283,0.34899411409395975,0.36241692617449667,0.3758397382550336,0.38926255033557045,0.40268536241610736,0.4161081744966443,0.4295309865771812,0.4429537986577181,0.45637661073825503,0.46979942281879195,0.48322223489932886,0.4966450469798658,0.5100678590604026,0.5234906711409396,0.5369134832214765,0.5503362953020134,0.5637591073825503,0.5771819194630873,0.5906047315436241,0.6040275436241611,0.617450355704698,0.630873167785235,0.6442959798657718,0.6577187919463088,0.6711416040268456,0.6845644161073825,0.6979872281879195,0.7114100402684563,0.7248328523489933,0.7382556644295302,0.7516784765100671,0.765101288590604,0.778524100671141,0.7919469127516778,0.8053697248322148,0.8187925369127517,0.8322153489932886,0.8456381610738255,0.8590609731543624,0.8724837852348993,0.8859065973154362,0.8993294093959732,0.91275222147651,0.926175033557047,0.9395978456375839,0.9530206577181208,0.9664434697986577,0.9798662818791947,0.9932890939597315,1.0067119060402685,1.0201347181208054,1.0335575302013422,1.046980342281879,1.0604031543624162,1.073825966442953,1.08724877852349,1.1006715906040268,1.1140944026845638,1.1275172147651007,1.1409400268456376,1.1543628389261744,1.1677856510067115,1.1812084630872484,1.1946312751677852,1.208054087248322,1.2214768993288592,1.234899711409396,1.248322523489933,1.2617453355704698,1.2751681476510066,1.2885909597315437,1.3020137718120806,1.3154365838926174,1.3288593959731543,1.3422822080536914,1.3557050201342282,1.369127832214765,1.382550644295302,1.395973456375839,1.409396268456376,1.4228190805369128,1.4362418926174496,1.4496647046979865,1.4630875167785236,1.4765103288590604,1.4899331409395973,1.5033559530201341,1.5167787651006712,1.530201577181208,1.543624389261745,1.5570472013422818,1.570470013422819,1.5838928255033558,1.5973156375838926,1.6107384496644295,1.6241612617449663,1.6375840738255034,1.6510068859060403,1.6644296979865771,1.677852510067114,1.691275322147651,1.704698134228188,1.7181209463087248,1.7315437583892617,1.7449665704697988,1.7583893825503356,1.7718121946308725,1.7852350067114093,1.7986578187919464,1.8120806308724833,1.8255034429530201,1.838926255033557,1.8523490671140939,1.865771879194631,1.8791946912751678,1.8926175033557047,1.9060403154362415,1.9194631275167786,1.9328859395973155,1.9463087516778523,1.9597315637583892,1.9731543758389263,1.9865771879194631,2.0],"marker":{"color":"black"}},{"y":[0.00012589253743847058,0.009823052863258274,0.015499503198626852,0.021162690650922356,0.026821027342610555,0.029649251661792407,0.033416519350222516,0.03813180360167062,0.040958862424590674,0.04378577228053608,0.04661254548296129,0.049439214403150425,0.052265797923415826,0.055092304099706474,0.05791875930530927,0.060745148002108874,0.06357148762434471,0.06639778796895195,0.06922405059851301,0.07205027522891885,0.07487648941792187,0.0766612230494499,0.07770266871777182,0.08052882013985534,0.08335495490003206,0.08618107643584895,0.08835766822512679,0.08900717180462085,0.09183325758616875,0.09465933604982801,0.09726381961562885,0.09748538866818106,0.10031143538759253,0.10313747683676068,0.1039201990678615,0.10596349975329308,0.10878952485734707,0.11100690753706173,0.11161552927300908,0.11444152723342,0.11714300336914897,0.11726752911986703,0.12009351640977997,0.12242366270115797,0.12291949933303448,0.12574547613508685,0.12692861435365715,0.1285714424719591,0.1313974027406373,0.13139740822266316,0.13422337344743235,0.13704932312875093,0.1370493220160394,0.13987528721150327,0.14010822037614876,0.1427012459111921,0.14552718154158958,0.14552718575257956,0.14835313151979493,0.14835312548725302,0.1511790673649913,0.15330564834352026,0.15400500109058637,0.15683091713936148,0.15683092990414885,0.15965686033040616,0.1596568542419296,0.16248279460885728,0.16386311077586554,0.16530871265970085,0.16813464289664276,0.16813463558211705,0.17096056053277311,0.17096055657516188,0.17378647849376996,0.17378647027650376,0.1766124003694184,0.17661239281486232,0.17943829354040186,0.17979695306888827,0.1822642110386137,0.18339436605898754,0.1850901293294478,0.18684530938880425,0.18791604457690722,0.1901545260508623,0.19074194138693626,0.19332649785227665,0.1935678520805544,0.1963654733709446,0.19639376246257606,0.19921966907268038,0.19921966599975552,0.20204557110756002,0.20204555051764808,0.20472358307314165,0.20487146247701973,0.20726876150232496,0.207697347710911,0.20969911252084544,0.21052325515202294,0.21201775901861034,0.213349163588986,0.21422767462426043,0.2161750529542206,0.21633170422757242,0.21900094794966773,0.21900094242468082,0.22182683817224996,0.2218268451718348,0.22465273513759845,0.22465273032248745,0.22747863653418648,0.22747863431586807,0.23030452118885159,0.2303045049504105,0.2328380372776041,0.2331303947897625,0.23431497757065065,0.23595630085217958,0.23595629041769955,0.2387821908398291,0.2387821816406123,0.24160805702695046,0.24160808175804907,0.2444339634043191,0.2444339773218206,0.24660984969030206,0.24725985863177927,0.24762547325426518,0.25008573271539625,0.25008572853712957,0.25291159574125427,0.25291162949155177,0.25573749037332827,0.2557374955335094,0.25728661370855754,0.2585633798029608,0.25856336866076485,0.2613892690770866,0.26138925864979695,0.26421516258867217,0.264215136252834,0.2660558856005363,0.267041048916163,0.26704101540348824,0.26986692851633276,0.2698669138929253,0.2726927987655078,0.2726928024072122],"name":"approximate optimal policy","type":"scatter","line":{"opacity":0.8},"x":[1.0e-6,0.013423812080536912,0.026846624161073824,0.04026943624161074,0.053692248322147654,0.06711506040268457,0.08053787248322147,0.09396068456375839,0.1073834966442953,0.12080630872483221,0.13422912080536914,0.14765193288590603,0.16107474496644295,0.17449755704697986,0.18792036912751678,0.2013431812080537,0.2147659932885906,0.22818880536912753,0.24161161744966442,0.25503442953020133,0.26845724161073825,0.28188005369127517,0.2953028657718121,0.308725677852349,0.3221484899328859,0.33557130201342283,0.34899411409395975,0.36241692617449667,0.3758397382550336,0.38926255033557045,0.40268536241610736,0.4161081744966443,0.4295309865771812,0.4429537986577181,0.45637661073825503,0.46979942281879195,0.48322223489932886,0.4966450469798658,0.5100678590604026,0.5234906711409396,0.5369134832214765,0.5503362953020134,0.5637591073825503,0.5771819194630873,0.5906047315436241,0.6040275436241611,0.617450355704698,0.630873167785235,0.6442959798657718,0.6577187919463088,0.6711416040268456,0.6845644161073825,0.6979872281879195,0.7114100402684563,0.7248328523489933,0.7382556644295302,0.7516784765100671,0.765101288590604,0.778524100671141,0.7919469127516778,0.8053697248322148,0.8187925369127517,0.8322153489932886,0.8456381610738255,0.8590609731543624,0.8724837852348993,0.8859065973154362,0.8993294093959732,0.91275222147651,0.926175033557047,0.9395978456375839,0.9530206577181208,0.9664434697986577,0.9798662818791947,0.9932890939597315,1.0067119060402685,1.0201347181208054,1.0335575302013422,1.046980342281879,1.0604031543624162,1.073825966442953,1.08724877852349,1.1006715906040268,1.1140944026845638,1.1275172147651007,1.1409400268456376,1.1543628389261744,1.1677856510067115,1.1812084630872484,1.1946312751677852,1.208054087248322,1.2214768993288592,1.234899711409396,1.248322523489933,1.2617453355704698,1.2751681476510066,1.2885909597315437,1.3020137718120806,1.3154365838926174,1.3288593959731543,1.3422822080536914,1.3557050201342282,1.369127832214765,1.382550644295302,1.395973456375839,1.409396268456376,1.4228190805369128,1.4362418926174496,1.4496647046979865,1.4630875167785236,1.4765103288590604,1.4899331409395973,1.5033559530201341,1.5167787651006712,1.530201577181208,1.543624389261745,1.5570472013422818,1.570470013422819,1.5838928255033558,1.5973156375838926,1.6107384496644295,1.6241612617449663,1.6375840738255034,1.6510068859060403,1.6644296979865771,1.677852510067114,1.691275322147651,1.704698134228188,1.7181209463087248,1.7315437583892617,1.7449665704697988,1.7583893825503356,1.7718121946308725,1.7852350067114093,1.7986578187919464,1.8120806308724833,1.8255034429530201,1.838926255033557,1.8523490671140939,1.865771879194631,1.8791946912751678,1.8926175033557047,1.9060403154362415,1.9194631275167786,1.9328859395973155,1.9463087516778523,1.9597315637583892,1.9731543758389263,1.9865771879194631,2.0],"marker":{"color":"blue"}}],

});

Out[71]:

nothing




In [75]:

gm = GrowthModel()
w = 5 .* gm.u(gm.grid) .- 25

discount_factors = [0.9, 0.94, 0.98]
series_length = 25

traces = GenericTrace[]

for bet in discount_factors

# Compute the optimal policy given the discount factor
gm.bet = bet
bellman(w) = bellman_operator(gm, w)
v_star = compute_fixed_point(bellman, w, max_iter=500, verbose=false)
sigma = get_greedy(gm, v_star)

# Compute the corresponding time series for capital
k = Array(Float64, series_length)
k[1] = 0.1

#sigma_func = scale(interpolate(sigma, BSpline(Linear()), OnGrid()), gm.grid)
sigma_func = my_lin_interp(gm.grid, sigma)

for t=2:series_length
k[t] = gm.f(k[t-1]) - sigma_func(k[t-1])
end
trace = PlotlyJS.scatter(x=1:series_length, y=k, marker_symbol="circle",
line_width=2, marker_opacity=0.75,
name="β=\$(bet)")

push!(traces, trace)

end

layout = Layout(xaxis_title="time",
yaxis=attr(title="capital", range=(0.1, 0.3)))

PlotlyJS.plot(traces, layout)




Out[75]:

window.PLOTLYENV=window.PLOTLYENV || {};
window.PLOTLYENV.BASE_URL="https://plot.ly";
require(['plotly'], function(Plotly) {
Plotly.newPlot('31e2bca8-8d79-4fb9-ae2c-432daaf8b13a', [{"y":[0.1,0.12912199207144076,0.15604512908719348,0.17409373485294322,0.18758238481095046,0.1996584975208333,0.20652130375279937,0.21030692248477978,0.21237007795473847,0.21348318768107402,0.2140804668184493,0.2144000229357153,0.2145707246043058,0.21466183451592505,0.21471044163331315,0.21473636735257584,0.2147501936746199,0.214757566825841,0.21476149855819257,0.2147635951138484,0.2147647130690227,0.21476530919769277,0.21476562707122016,0.21476579657057046,0.21476588695244708],"name":"β=0.9","type":"scatter","line":{"width":2},"x":[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],"marker":{"symbol":"circle","opacity":0.75}},{"y":[0.1,0.13920539215433075,0.16802612330490124,0.19334080116440008,0.2088750258679758,0.22191768397878506,0.2315520877057985,0.23674972916940884,0.23928192903331055,0.24049992059485278,0.24108218529115752,0.24135972138873862,0.2414918234430406,0.2415546596251753,0.24158453904196572,0.2415987449458301,0.24160549853207905,0.2416087091247386,0.24161023538613705,0.241610960939372,0.2416113058511723,0.2416114698142517,0.24161154775843655,0.241611584811251,0.2416116024252758],"name":"β=0.94","type":"scatter","line":{"width":2},"x":[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],"marker":{"symbol":"circle","opacity":0.75}},{"y":[0.1,0.1405799614950588,0.17979204685350192,0.20756986741273148,0.23049497307774688,0.2439385780196784,0.2565072624012654,0.2641414213256984,0.26784050551673466,0.26960390730836215,0.2709290859636222,0.2721206879658871,0.2731901885359001,0.27414850473759494,0.27500592069629937,0.27577204388871646,0.27645578617289873,0.27706536400329373,0.2776083130844276,0.27809151349974615,0.27852122207455,0.2789031093752554,0.2792422993043452,0.27954340972197333,0.27981059291661925],"name":"β=0.98","type":"scatter","line":{"width":2},"x":[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],"marker":{"symbol":"circle","opacity":0.75}}],