Method error in Julia code

I am running this code but it shows a method error. Can someone please help me?

Code:

function lsmc_am_put(S, K, r, σ, t, N, P)
    Δt = t / N
    R = exp(r * Δt)
    T = typeof(S * exp(-σ^2 * Δt / 2 + σ * √Δt * 0.1) / R)
    X = Array{T}(N+1, P)

    for p = 1:P
        X[1, p] = x = S
        for n = 1:N
            x *= R * exp(-σ^2 * Δt / 2 + σ * √Δt * randn())
            X[n+1, p] = x
        end
    end

    V = [max(K - x, 0) / R for x in X[N+1, :]]

    for n = N-1:-1:1
        I = V .!= 0
        A = [x^d for d = 0:3, x in X[n+1, :]]
        β = A[:, I]' \ V[I]
        cV = A' * β
        for p = 1:P
            ev = max(K - X[n+1, p], 0)
            if I[p] && cV[p] < ev
                V[p] = ev / R
            else
                V[p] /= R
            end
        end
    end

    return max(mean(V), K - S)
end

lsmc_am_put(100, 90, 0.05, 0.3, 180/365, 1000, 10000)

error:

MethodError: no method matching (Array{Float64})(::Int64, ::Int64)
Closest candidates are:
  (Array{T})(::LinearAlgebra.UniformScaling, ::Integer, ::Integer) where T at /Volumes/Julia-1.8.3/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/uniformscaling.jl:508
  (Array{T})(::Nothing, ::Any...) where T at baseext.jl:45
  (Array{T})(::UndefInitializer, ::Int64) where T at boot.jl:473
  ...
Stacktrace:
 [1] lsmc_am_put(S::Int64, K::Int64, r::Float64, σ::Float64, t::Float64, N::Int64, P::Int64)
   @ Main ./REPL[39]:5
 [2] top-level scope
   @ REPL[40]:1

Can someone please help me?

Array{T}(N+1, P) isn’t valid. What did you want this to be? Did you want, Array{T}(undef, N+1, P)?

I also tried Array(T,N+1,P) but to no avail

Syntax is important - did you try Array{T}(undef, N+1, P) as suggested?

Yes I did.

julia> lsmc_am_put(100,90,0.05,0.2,180/365,1000,10000)

ERROR: UndefVarError: mean not defined

Stacktrace:

[1] lsmc_am_put(S::Int64, K::Int64, r::Float64, σ::Float64, t::Float64, N::Int64, P::Int64)

@ Main ./REPL[43]:32

[2] top-level scope

@ REPL[44]:1

This is what I get.

Well, that’s a different error from what you were getting before, so we’re making progress. mean is provided by the Statistics.jl standard library - did you include using Statistics?

2 Likes

Yeah I included ‘using statistics’ and the code ran. I then run lsmc_am_put(Dual(100,1,0),90,0.5,Dual(0.3,0.1),180/365,1000,10000) as the second step for my project and it shows the error:

UndefVarError: Dual not defined
Stacktrace:
[1] top-level scope

What can I do here?

what work does Dual function do?
you use that for what work?

The Dual function is supposed to return a bracket with the following values: the option value, the derivative of the function with respect to S and the derivative with respect to sigma.

e.g. (3.2247592,-0.248790465,17.46382046)

i couldn’t found Dual function in julia packages that i have saw until now but there is another way for it.
in your code the Dual function is not define, it’s mean that you have to import it inside of your script but i couldn’t found Dual function in julia packages so you should create Dual function by yourself that works correctly in your script and then use it to return your expected values and then run the main function lsmc_am_put

You may be looking for using ForwardDiff: Dual

1 Like