Lecture 3 : Optimization 1

Some Julia Package Stuff

Let's create a julia package!

]
(@v1.8) pkg> add PkgTemplates

julia> using PkgTemplates

help?> Template

julia> t = Template(user = "floswald", interactive = true)
Template keywords to customize:
[press: d=done, a=all, n=none]
   [ ] authors
   [ ] dir
   [ ] host
   [ ] julia
 > [ ] plugins

julia> t("MyNewPackage")
[ Info: Running prehooks
[ Info: Running hooks
...suppressed output...
[ Info: New package is at /Users/74097/.julia/dev/MyNewPackage
module MyNewPackage

mutable struct MPoint
    x::Number
    y::Number
end

import Base.:+

+(a::MPoint,b::MPoint) = MPoint(a.x + b.x, a.y + b.y)

end   # module
julia> include("src/MyNewPackage.jl")
Main.MyNewPackage
julia> include("src/MyNewPackage.jl")
WARNING: replacing module MyNewPackage.
@testset "MyNewPackage.jl" begin
    a = MyNewPackage.MPoint(3,5)
    b = MyNewPackage.MPoint(1,2)
    @test a + b isa MyNewPackage.MPoint
    p = a + b
    @test p.x == a.x + b.x
    @test p.y == a.y + b.y
end
-(a::MPoint,b::MPoint) = MPoint(a.x - b.x, a.y - b.y)
julia> a = MyNewPackage.MPoint(3,4)
MyNewPackage.MPoint(3, 4)

julia> b = MyNewPackage.MPoint(99,100)
MyNewPackage.MPoint(99, 100)

julia> a - b
MyNewPackage.MPoint(-96, -96)

Debugging A Package

function econ_model(; startval = 1.0)
    # make an Mpoint
    x = MPoint(startval, startval-0.5)
    # ... and evaluate a utility function
    MPoint(log(x.x),log(x.y))
end
julia> MyNewPackage.econ_model()
MyNewPackage.MPoint(0.0, -0.6931471805599453)
julia> MyNewPackage.econ_model(startval = 0.3)
  1. Add println statements.

  2. Add @debug statements. then attaching a logger with

using Logging
debug_logger = ConsoleLogger(stdout, Logging.Debug)
global_logger(debug_logger)  # turns on logging of @debug messages
  1. Use an actual debugger to step through our code.

    1. VSCode exports by default the @enter macro. type: @enter MyNewPackage.econ_model(startval = -0.3)

    2. click on teh play symbol. program hits an error.

    3. set a break point just before

    4. click on replay.

Some Julia-Bootcamp stuff

TopicNotebook
Intro to Macrosclick for notebook
Intro to Differential Equationsclick for notebook
Plotting with Plots.jlclick for notebook
Interactiveclick for notebook

Optimization, Finally!

TopicNotebook
Review of Optimization Algorithmsdownload notebook