# Method error in Julia code

**URL:** <https://discourse.julialang.org/t/method-error-in-julia-code/91805>\
**Category:** New to Julia\
**Created:** [December 18, 2022, 5:22am UTC](https://discourse.julialang.org/t/method-error-in-julia-code/91805 "2022-12-18T05:22:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamza\_malik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hamza_malik/32/45188_2.png) [@hamza\_malik](https://discourse.julialang.org/u/hamza_malik)\
**Post date:** [December 18, 2022, 5:22am UTC](https://discourse.julialang.org/t/method-error-in-julia-code/91805/1 "2022-12-18T05:22:39Z")

</div>

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

Code:

```julia
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:

```julia
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?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 18, 2022, 5:25am UTC](https://discourse.julialang.org/t/method-error-in-julia-code/91805/2 "2022-12-18T05:25:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hamza\_malik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hamza_malik/32/45188_2.png) [@hamza\_malik](https://discourse.julialang.org/u/hamza_malik)\
**Post date:** [December 18, 2022, 5:28am UTC](https://discourse.julialang.org/t/method-error-in-julia-code/91805/3 "2022-12-18T05:28:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 18, 2022, 5:35am UTC](https://discourse.julialang.org/t/method-error-in-julia-code/91805/4 "2022-12-18T05:35:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hamza\_malik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hamza_malik/32/45188_2.png) [@hamza\_malik](https://discourse.julialang.org/u/hamza_malik)\
**Post date:** [December 18, 2022, 5:36am UTC](https://discourse.julialang.org/t/method-error-in-julia-code/91805/5 "2022-12-18T05:36:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 18, 2022, 6:14am UTC](https://discourse.julialang.org/t/method-error-in-julia-code/91805/6 "2022-12-18T06:14:08Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![hamza\_malik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hamza_malik/32/45188_2.png) [@hamza\_malik](https://discourse.julialang.org/u/hamza_malik)\
**Post date:** [December 18, 2022, 6:18am UTC](https://discourse.julialang.org/t/method-error-in-julia-code/91805/7 "2022-12-18T06:18:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![MrP](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrp/32/44861_2.png) [@MrP](https://discourse.julialang.org/u/MrP)\
**Post date:** [December 18, 2022, 4:05pm UTC](https://discourse.julialang.org/t/method-error-in-julia-code/91805/8 "2022-12-18T16:05:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hamza\_malik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hamza_malik/32/45188_2.png) [@hamza\_malik](https://discourse.julialang.org/u/hamza_malik)\
**Post date:** [December 18, 2022, 9:34pm UTC](https://discourse.julialang.org/t/method-error-in-julia-code/91805/9 "2022-12-18T21:34:03Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![MrP](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrp/32/44861_2.png) [@MrP](https://discourse.julialang.org/u/MrP)\
**Post date:** [December 26, 2022, 2:13am UTC](https://discourse.julialang.org/t/method-error-in-julia-code/91805/10 "2022-12-26T02:13:43Z")

</div>

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**

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [December 26, 2022, 12:49pm UTC](https://discourse.julialang.org/t/method-error-in-julia-code/91805/11 "2022-12-26T12:49:09Z")

</div>

You may be looking for `using ForwardDiff: Dual`
