JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.71k stars 5.48k forks source link

Julia is crashing and telling me to send bug report #16494

Closed jmcastro2109 closed 7 years ago

jmcastro2109 commented 8 years ago

Hello, I am doing a process in Julia, by which using fzero in a loop I find the root of an equation. However, consistenly Julia crashes after 6.000 iterations. I have modified the code many times, in order to make it more efficient, and consistently I get this error. I am running Julia in the Atom IDE. Any help, suggestion or solution would be more than appreciated.

Here is the code, and it is followed by the bug report.

Thank you very much.

using Distributions
using Gadfly
using HDF5, JLD
using FastAnonymous
using Roots

# PART I. FUNCTIONS
# In this part we generate the functions to be used along the code.

# I.1) Markov Matrix function
# This function will generate the grid of shocks z as well as the matrix π for the dynamics of the shocks
function markov(rho,stdev_residuals, amplitude, numberShocks) # Note that ρ and σ_u are the parameters that define the random variable z
    stdev_z=sqrt(stdev_residuals^2/(1-rho^2))
    z=collect(linspace(-amplitude*stdev_z,amplitude*stdev_z,numberShocks)) # Definition of z (grid of shocks)
    dz=(z[numberShocks]-z[1])/(numberShocks-1)
    Pi=Array{Float64}(numberShocks,numberShocks) # Definition and construction of π (Markov chain matrix)
    Pi[:,1]=cdf(Normal(0,stdev_residuals),z[1]+dz/2-rho*z[:])
    Pi[:,numberShocks]=1-cdf(Normal(0,stdev_residuals),z[numberShocks]-dz/2-rho*z[:])
    for i=1:numberShocks,j=2:numberShocks-1
      Pi[i,j]=cdf(Normal(0,stdev_residuals),z[j]+dz/2-rho*z[i])-cdf(Normal(0,stdev_residuals),z[j]-dz/2-rho*z[i])
    end
    return Array[z,Pi] # The function will deliver z and π in the form of a 2-element vector
end

# I.2) The production function
production(k,h,z)=exp(z)*k^θ*h^(1-θ)

# I.3) Consumption, defined by imposing a binding budget constraint
consumption(k_0,k_prime,h,z)=production(k_0,h,z)+(1-δ)*k_0-k_prime

# I.4) Utility function (replacing consumption as defined above)
# Note it is defined in order to disregard negative consumption values or labor supply out of the [0,1] interval
utility(k_0,k_prime,h,z)=((consumption(k_0,k_prime,h,z)>=0)&(h<=1)&(h>=0))?log(production(k_0,h,z)+(1-δ)*k_0-k_prime)+A*log(1-h):-Inf

# I.5) First order condition for labor
laborCondition(h,k,z,k1)=(1-θ)*(production(k,h,z))*(1-h)-A*h*(consumption(k,k1,h,z))

######

function solveDP(K::Vector, z::Vector, Pi::Matrix, labor::Matrix, consumption::Function, utility::Function, discount=.99, maxiter=500, precision=0.01)
    sizeK=size(K)
    sizeZ=size(z)

    V₀=ones(sizeK,sizeZ) #intitial guess
    V₁=Array{Float64}(sizeK,sizeZ) #next guess
    V₁try=Array{Float64}(sizeK,sizeZ,sizeK) #initial guess conditioning on k′
    g=zeros(sizeK,sizeZ) #policy function for capital

    crit=1000
    count=0
    while (crit>precision)&(count<maxiter)
      for i=1:sizeK,j=1:sizeZ
        for i2=1:sizeK
          V₁try[i,j,i2]=utility(consumption(K[i],K[i2],labor[i,j,i2],z(j)))+discount*dot(vec(Pi[j,:]),vec(V₀[i2,:]))
          for j2=1:M
            V₁try[i,j,i2]=V₁try[i,j,i2]+discount*Pi[j,j2]*V₀[i2,j2]
          end
        end
        V₁[i,j]=maximum(V₁try[i,j,:])
        g[i,j]=K[indmax(V₁try[i,j,:])]
      end
      crit=!(norm(V₀)==0)?norm(V₁-V₀)/norm(V₀):Inf
      V₀[:,:]=V₁[:,:]
      count+=1
    end
    if count<maxiter
      println("The process has converged with a criterion of ")
    else
      println("The process didn't converged")
    end
end
#César
####
################################################################

# Part II. PARAMETERS

const β=0.99 # The discount factor
const θ=0.36 # The capital share
const δ=0.025 # The depreciation rate
const ρ=0.95 # Persistence of the shocks
const σ_u=0.007 # Standard dev. of the shocks
const nkk=75 # Number of elements in capital grids
const q=3 # Amplitude of the grid of shocks (in terms of std. devs. from the mean)
const M=7 # Number of elements in the grid of shocks

###############################################################

# Part III. THE STEADY STATE

const h_star=1/3 # Labor supply as observed in the data (p. 6)
const k_star=(h_star)*(1/θ*(1/β-(1-δ)))^(1/(θ-1)) # Optimal k as per the model

#A is properly scaled and steady state values are recalculated (??)
# Parameter 'A' obtained from steady state labor supply condition
const A=((1-h_star)/h_star)*(1-θ)*(production(k_star,h_star,0))*(production(k_star,h_star,0)+(1-δ)*k_star-k_star)^-1

# Build the grid for potential K values around k_star
const K_min=0.60*k_star
const K_max=1.40*k_star
K=collect(linspace(K_min,K_max,nkk))

# Using the Markov Chain function we retrieve z and π given the parameters defined
markovChain=markov(ρ,σ_u, q, M)
z=markovChain[1]
Pi=markovChain[2]

###############################################################################

# PART IV. POLICY FUNCTION FOR LABOR SUPPLY
# In this part we obtain the grid of optimal labor decisions for every combination of the state variables (k_t, k_t+1, shock_t)
# The function uses the @anon command, which finds the roots of an equation (in this case, the FOC for labor)
  #####################################################################################################
function FindingLabor(I0::Float64,k::Array{Float64},z::Array{Float64},shockNumbers)
    countLoop=1
    sizeK=size(k)[1]
    sizeZ=size(shockNumbers)[1]
    total=sizeK*sizeK*sizeZ
    progressPrint=100
    function f_gen(k,z,k1)
        @anon h -> laborCondition(h,k,z,k1)
    end
    matrixColumn=1
    for j in shockNumbers
        MatrixN=zeros(sizeK,sizeK)
        for i2=1:sizeK,i=1:sizeK
          countLoop==round(countLoop/progressPrint)*progressPrint?println("Progress: iteration ", countLoop," out of ",total, " i.e. ", round(countLoop/total*100), "%"):
          if consumption(k[i],k[i2],1,z[j])>0
            try MatrixN[i,i2]=fzero(f_gen(k[i],z[j],k[i2]),I0,ftol=1e-4,xtol=1e-4,order=8) # => finds x so f(x,0,0,0,0) = 0 using a derivative free method
            catch
              MatrixN[i,i2]=-Inf
            end
          else
            MatrixN[i,i2]=Inf
          end
          I0=min(max(0.3,MatrixN[i,i2]),0.7)
          countLoop+=1
        end
        name=string("shock00",j,".h5")
        h5open(name, "w") do file
        write(file, "MatrixN", MatrixN)
        end
    end
end

#if optimalH has not been computed uncomment these lines and comment the next section of code
@time FindingLabor(h_star,K,z,1:2)

BUG REPORT:

[Please submit a bug report with steps to reproduce this fault, and any error messages that follow (in their entirety). Thanks.
Exception: EXCEPTION_ACCESS_VIOLATION at 0x64f41528 -- jl_write_malloc_log at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_write_malloc_log at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_extern_c at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_extern_c at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_extern_c at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_load_and_lookup at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_compile at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_trampoline at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_apply_generic at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
julia_thukral8_itr_93640 at  (unknown line)
jlcall_thukral8_itr_93640 at  (unknown line)
_derivative_free at C:\Users\Usuario\.julia\v0.4\Roots\src\derivative_free.jl:496
jlcall____derivative_free#16___93639 at  (unknown line)
jl_apply_generic at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
julia__derivative_free_93638 at  (unknown line)
jlcall__derivative_free_93638 at  (unknown line)
derivative_free at C:\Users\Usuario\.julia\v0.4\Roots\src\derivative_free.jl:480
jlcall___derivative_free#15___93637 at  (unknown line)
jl_apply_generic at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
julia_derivative_free_93631 at  (unknown line)
jlcall_derivative_free_93631 at  (unknown line)
fzero at C:\Users\Usuario\.julia\v0.4\Roots\src\Roots.jl:71
jlcall___fzero#40___93630 at  (unknown line)
jl_apply_generic at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
julia_fzero_93629 at  (unknown line)
jlcall_fzero_93629 at  (unknown line)
FindingLabor at C:\Users\Usuario\Dropbox\Tarea macro\version28_05_16.jl:138
jlcall_FindingLabor_2398 at  (unknown line)
jl_apply_generic at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_interpret_toplevel_expr at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_interpret_toplevel_thunk_with at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_interpret_toplevel_thunk_with at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_interpret_toplevel_expr_in at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_interpret_toplevel_thunk_with at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_eval_with_compiler_p at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_parse_eval_all at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_load_file_string at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
include_string at C:\Users\Usuario\.julia\v0.4\CodeTools\src\eval.jl:28
jlcall_include_string_2376 at  (unknown line)
jl_apply_generic at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_interpret_toplevel_expr at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_interpret_toplevel_thunk_with at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_eval_with_compiler_p at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
jl_f_tuple at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
include_string at C:\Users\Usuario\.julia\v0.4\CodeTools\src\eval.jl:32
jl_apply_generic at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
anonymous at C:\Users\Usuario\.julia\v0.4\Atom\src\eval.jl:39
withpath at C:\Users\Usuario\.julia\v0.4\Requires\src\require.jl:37
jl_apply_generic at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
withpath at C:\Users\Usuario\.julia\v0.4\Atom\src\eval.jl:53
jl_apply_generic at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)
anonymous at C:\Users\Usuario\.julia\v0.4\Atom\src\eval.jl:61
jl_unprotect_stack at C:\Users\Usuario\AppData\Local\Julia-0.4.5\bin\libjulia.dll (unknown line)]
Ismael-VC commented 8 years ago

You forgot this part:

submit a bug report with steps to reproduce this fault

jmcastro2109 commented 8 years ago

Can someone help me? Please!

timholy commented 8 years ago

It doesn't crash for me in Linux. Anything else you might have omitted---for example, are you running with --track-alllocation?

Also please supply versioninfo().

jmcastro2109 commented 8 years ago

@timholy I am not running it with track allocation.

Version info is

Julia Version 0.4.5
Commit 2ac304d (2016-03-18 00:58 UTC)
Platform Info:
  System: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i5-3317U CPU @ 1.70GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.3

I am sorry for not providing many details or information, I am not very savyy and I am just learning how to use Julia.

Anyway, I am very grateful for your help!

andreasnoack commented 8 years ago

@jmcastro2109 Is this still a problem?

KristofferC commented 7 years ago

Please comment if this is still crashing.

IlyaOrson commented 6 years ago

I get a related crashing error with a C wrapper package: EzXML

StefanKarpinski commented 6 years ago

@IlyaOrson: what makes you think it's a related crash?

IlyaOrson commented 6 years ago

I got confused by the similar error message. I just tried to link the corresponding issue because I though it was a julia bug, not sure which issue is the right one though. Sorry for the noise.

ihnorton commented 6 years ago

@IlyaOrson no worries -- for reference, EXCEPTION_ACCESS_VIOLATION is a very common message to see for a crash on Windows due to memory corruption, which looks like the case with EzXML based on the proposed fix.