Giter VIP home page Giter VIP logo

Comments (15)

floswald avatar floswald commented on May 29, 2024

from smm.jl.

floswald avatar floswald commented on May 29, 2024

from smm.jl.

edoardociscato avatar edoardociscato commented on May 29, 2024

I double-checked several times as I was very puzzled. The objective function is evaluated correctly in every chain, then the chain is correctly updated within next_eval!: everything works correctly but the updated chains are not carried over outside of pmap and stored in algo as I expected. However you would have noticed this yourself if you tested the algorithm, so I do not know what is going on. Also, I agree that my correction is gross but it worked out in my case: there is certainly a better way to write it though. The objective function is (algo options are below)

x -> objective_function(x, M)

with


function objective_function(ev::MomentOpt.Eval, M::Simulation.Model)

    # Collect information from Eval object
    MomentOpt.start(ev)
    MomentOpt.setValue(ev, 1.0)
    Parameters.@unpack fxy, u0, v0, λ, δx, δy, δz, ψm, ψf, ψ, r, γ, S0, n0, θ = M
    Kθ = length(θ); θ = zeros(Kθ)

    # Print newly assigned values for the parameter vector
    for tuple in ev.params
        println("Parameter $(tuple[1]) is $(tuple[2])")
    end

    # Replace parameter vector
    for i in 1:Kθ
        θ[i] = ev.params[Symbol("th$i")]
    end

    # Collect empirical moments
    moments_hat = collect(values(ev.dataMoments))
    W = collect(values(ev.dataMomentsW))

    # Compute new production functions
    m = deepcopy(M)
    m = Simulation.add_parameters(m, fxy, u0, v0, λ, δx, δy, δz, ψm, ψf, ψ, r,
        θ, γ; n0 = n0, S0 = S0)

    # Solve the model, compute value function and simulated moments
    m = Simulation.solve_model(m)

    if m.status == 1
        m = Simulation.get_moments_sim(m, match = true)
        Parameters.@unpack moments_sim = m
        W = W / sum(W)
        target = sum( ( (moments_hat - collect(values(moments_sim))) ./ moments_hat ).^2 .* W)
        MomentOpt.setValue(ev, target)
        MomentOpt.setMoment(ev, Dict(moments_sim))
        m.n0 = [m.nx m.ny]; m.S0 = m.Sxyz; m.m0 = m.mxyz # hereon use these as initial values
        ev.status = 1
    end

    MomentOpt.setSolution(ev, m)
    MomentOpt.finish(ev)

    return ev

end

Algo options are

Dict("N" => nchains,
    "maxiter" => 4,
    "maxtemp" => 5,
    "coverage" => 0.025,
    "sigma_update_steps" => 10,
    "sigma_adjust_by" => 0.01,
    "smpl_iters" => 100000,
    "parallel" => true,
    "maxdists" => [0.05 for i in 1:nchains],
    "acc_tuner" => 15.0,
    "animate" => false)

from smm.jl.

edoardociscato avatar edoardociscato commented on May 29, 2024

And to make sure it is clear, AlgoBGP stops because it fails the following test

https://github.com/floswald/MomentOpt.jl/blob/d50ef47b77773f5addfc1e31d52756c64a1a998d/src/mopt/AlgoBGP.jl#L502

although as I told you next_eval! works smoothly within pmap.

from smm.jl.

floswald avatar floswald commented on May 29, 2024

from smm.jl.

floswald avatar floswald commented on May 29, 2024

so this is very strange. I cannot reproduce this. can you please do Pkg.checkout("MomentOpt"), start a new session and so using MomentOpt; MomentOpt.serialNormal() ? this runs for me correctly and is the basis of a just newly added unit test. all tests pass on my machine, travis and appveyor are currently running dying on me because neither can properly load PyPlot.

from smm.jl.

floswald avatar floswald commented on May 29, 2024

the output from a 20-iterations example MomentOpt.serialNormal(20) looks something like this:

julia> MomentOpt.history(x.chains[1])
20×9 DataFrames.DataFrame
│ Row │ iter │ value     │ accepted │ curr_val  │ best_val  │ prob      │ exchanged │ p1         │ p2         │
├─────┼──────┼───────────┼──────────┼───────────┼───────────┼───────────┼───────────┼────────────┼────────────┤
│ 111.45175true1.451751.451751.000.2-0.2       │
│ 220.760537true0.7605370.7605371.030.3080890.0291797  │
│ 330.629384true0.6293840.6293841.000.181651-0.254984  │
│ 440.673976false0.6293840.6293840.5856060-0.123411-0.767274  │
│ 550.802517false0.6293840.6293840.1252300.6972880.235541   │
│ 660.782923false0.6293840.6293840.15842600.141543-0.101378  │
│ 770.723521false0.6293840.6293840.32314900.166334-0.134403  │
│ 880.599857true0.5998570.5998571.020.516552-0.0241237 │
│ 990.507343true0.5073430.5073431.000.431551-0.176718  │
│ 10100.704049false0.5073430.5073430.094375900.5237830.0817499  │
│ 11110.346174true0.3461740.3461741.021.35379-0.258162  │
│ 12120.23485true0.234850.234851.020.36084-1.24317   │
│ 13130.178655true0.1786550.1786551.000.677804-1.50555   │
│ 14140.0803123true0.08031230.08031231.000.767662-1.31983   │
│ 15150.180196false0.08031230.08031230.30161600.469792-0.717618  │
│ 16160.0236958true0.02369580.02369581.000.909039-0.814352  │
│ 17170.0842257true0.08422570.02369580.48366700.642016-0.770849  │
│ 18180.188059true0.1880590.02369580.28765500.419304-0.7793    │
│ 19190.460388false0.1880590.02369580.038084200.0501502-0.893285  │
│ 20200.545659false0.1880590.02369580.01368850-0.0208657-0.785931

from smm.jl.

edoardociscato avatar edoardociscato commented on May 29, 2024

The checkout didn't say anything interesting. I removed the package and cloned the latest version (with fixed warn-logger issue) but I get the same error.
I'm in DEBUG mode here. You can see that on each worker I successfully draw values (at least the initial values) for my 5 parameters and solve my model within pmap. I remember previously checking and including a line to print the result of my objective function to make sure it was correct. Also, always by printing their respective outcomes, I can tell you that doAcceptReject! and set_eval! also work correctly. I do not understand why it is not working for me. It doesn't matter as I fixed it myself, but I expected it to show up for you too. runMOpt! gives

17:24:11:INFO:Main:Starting estimation loop.
17:24:11:INFO:Main:Starting estimation loop.
2017-12-08 17:24:11:INFO:Main:Starting estimation loop.
17:24:11:DEBUG:Main:iteration 1
17:24:11:DEBUG:Main:iteration 1
2017-12-08 17:24:11:DEBUG:Main:iteration 1
	From worker 5:	Parameter th1 is 0.8
	From worker 5:	Parameter th2 is 0.6
	From worker 5:	Parameter th3 is 0.6
	From worker 5:	Parameter th4 is 0.9
	From worker 5:	Parameter th5 is 1.0
	From worker 3:	Parameter th1 is 0.8
	From worker 3:	Parameter th2 is 0.6
	From worker 3:	Parameter th3 is 0.6
	From worker 3:	Parameter th4 is 0.9
	From worker 3:	Parameter th5 is 1.0
	From worker 4:	Parameter th1 is 0.8
	From worker 4:	Parameter th2 is 0.6
	From worker 4:	Parameter th3 is 0.6
	From worker 4:	Parameter th4 is 0.9
	From worker 4:	Parameter th5 is 1.0
	From worker 2:	Parameter th1 is 0.8
	From worker 2:	Parameter th2 is 0.6
	From worker 2:	Parameter th3 is 0.6
	From worker 2:	Parameter th4 is 0.9
	From worker 2:	Parameter th5 is 1.0
	From worker 5:	Compute iterate 1 with error 0.0007456979517434618
	From worker 5:	Converged in 1 steps
	From worker 4:	Compute iterate 1 with error 0.0007456979517434618
	From worker 4:	Converged in 1 steps
	From worker 3:	Compute iterate 1 with error 0.0007456979517434618
	From worker 3:	Converged in 1 steps
	From worker 2:	Compute iterate 1 with error 0.0007456979517434618
	From worker 2:	Converged in 1 steps
17:24:43:WARN:Main:caught exception AssertionError("algo.i == algo.chains[ic].iter")
17:24:43:WARN:Main:caught exception AssertionError("algo.i == algo.chains[ic].iter")
2017-12-08 17:24:43:WARN:Main:caught exception AssertionError("algo.i == algo.chains[ic].iter")
AssertionError: algo.i == algo.chains[ic].iter

from smm.jl.

floswald avatar floswald commented on May 29, 2024

from smm.jl.

floswald avatar floswald commented on May 29, 2024

from smm.jl.

floswald avatar floswald commented on May 29, 2024

ok I got it. you are right with your pmap idea. it seems i never tested this new version on more than 1 processes! d'oh. can you post your snippet with the version of pmap that you use now? one needs to insert the returned evs also into the correct chain, upon making next_eval! to become next_eval

from smm.jl.

floswald avatar floswald commented on May 29, 2024

you could also submit a pull request against the current master to speed this up - i'll only be back on this monday.

from smm.jl.

floswald avatar floswald commented on May 29, 2024

ok done. can you please checkout the lastest version again - maybe try Pkg.checkout("MomentOpt",pull=true) this time. if it does nothing just go into ~/.julia/v0.6/MomentOpt and do a git pull manually. then please confirm that

  1. Pkg.test("MomentOpt") runs
  2. your applicatoin runs

then you can close that. thanks!

from smm.jl.

edoardociscato avatar edoardociscato commented on May 29, 2024

Thanks a lot for these corrections and sorry for the late reply. I did the checkout and it worked.

  1. My application works correctly with the updated MomentOpt. I believe you fixed the main problem I pointed out with pmap. The way you fix it is definitely better than what I suggested: I also realized after applying my fix - assigning the output of pmap to algo.chains directly - that I had compromised the right order of the chains.
  2. Pkg.test gave me three errors, but they seem related to the animated gifs, and not to BGP chains. Here is the summary of the test. I can post details if you want.

Test Summary:                             | Pass  Error  Total
Running MomentOpt tests                   |   70      3     73
  AlgoBGP                                 |    8      2     10
    Constructor                           |    8             8
    serialNormal() runs                   |           1      1
    parallelNormal() runs                 |           1      1
  Testing BGPChains constructor           |   26            26
  Testing Eval object                     |   17            17
  Mopt type                               |    8             8
  testing objfunctions                    |    6             6
  verifying read and write for Slices     |    4      1      5
  handling of failing objective on remote |    1             1

from smm.jl.

floswald avatar floswald commented on May 29, 2024

thanks @edoardociscato, i saw that the tests also fail on travis because I used the Dataframes.isna function - which seems to be deprecated in the newest version of Dataframes which travis uses. fixed now, and tests should finally pass. I will close this issue now 🎉 as it seems to be resolved (please reopen if not!). thanks a lot for reporting and your help, that was quite a major bug! now you can move on to fix my next open source project 😝

from smm.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.