XYZ
XYZ

Reputation: 225

How to create NA matrix in Julia?

I am successful to create this NA matrix using this R code.

tree=matrix(NA,nrow=(4),ncol=(4))
tree

Output:- enter image description here

Edit:-

import Pkg
Pkg.add("Missings")
using Missings

Eurocall = function(S, T , K, r, sigma , N)
    
    # S : Spot Price
    # T : Time to Expire
    # K : Strike Price
    # r : Risk free rate of return
    # sigma : volatility of the asset
    # N : Length of the Binomial Tree
    
    deltaT=T/N
    u=exp(sigma*sqrt(deltaT))
    d=1/u
    p=(exp(r*deltaT)-d)/(u-d)
    
    tree = missings(Int,(N+1),(N+1))
  
    for i in 0:N
        tree[i+1,N+1]=max(0,(S*u^i*d^(N-i))-K)
    end
    
    for j in (N-1):0 
        for i in 0:j
            tree[i+1,j+1]=exp(-r*deltaT)*(p*tree[i+2,j+2]+(1-p)*tree[i+1,j+2])
        end
        
    end
    price=tree[1,1] 
    return(price)
end

Eurocall(10,10,11,0.05,0.1,10)

I wrote this code, but gives me this error

InexactError: Int64(1.2140275816016999)

How to fix this?

Upvotes: 1

Views: 735

Answers (4)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

While the Bogumil's answer is the way to go, it is worth noting that in Julia you also have a nothing value (which is in some way an equivalent of null in other programming languages) and undef (which is an undefined reference).

Hence you can do:

julia> Matrix{Union{Nothing, Float64}}(undef,2,3)
2×3 Matrix{Union{Nothing, Float64}}:
 nothing  nothing  nothing
 nothing  nothing  nothing

This constructed a matrix that can hold both nothings and floats. Note that there is a special type Nothing.

You could also do:

julia> Matrix{String}(undef,2,3)
2×3 Matrix{String}:
 #undef  #undef  #undef
 #undef  #undef  #undef

This created a matrix whose elements are unassigned references.

Finally, when creating an unallocated matrix over a primitive type, Juia will just assign some area of memory (it will not box primitive type around pointers as primitive arrays just present some area of memory for performance):

julia> Matrix{Float64}(undef,2,3)
2×3 Matrix{Float64}:
 7.71946e-316  7.71946e-316  7.71947e-316
 7.71946e-316  7.71947e-316  0.0

You can see that whatever was there in my RAM ended in the matrix.

To add to the list - in some performance critical implementations people use NaNs from the floating point arithmetic to represent missing values. This is almost never the way to go unless you exactly know what you are doing:

julia> typeof(NaN)
Float64

julia> fill(NaN, 3,4)
3×4 Matrix{Float64}:
 NaN  NaN  NaN  NaN
 NaN  NaN  NaN  NaN
 NaN  NaN  NaN  NaN

Upvotes: 0

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69869

The missings function from Missings.jl is designed for this use-case:

julia> using Missings

julia> missings(Int, 3, 4)
3×4 Matrix{Union{Missing, Int64}}:
 missing  missing  missing  missing
 missing  missing  missing  missing
 missing  missing  missing  missing

(note that it is recommended to pass a type of elements that you want to later store in this martix)

Upvotes: 4

Andre Wildberg
Andre Wildberg

Reputation: 19088

Try using DataFrames. It gives you row and column names and a myriad of tabular data features.

julia> using DataFrames

julia> DataFrame(Matrix{Any}(missing, 4,4), :auto)
4×4 DataFrame
 Row │ x1       x2       x3       x4      
     │ Any      Any      Any      Any     
─────┼────────────────────────────────────
   1 │ missing  missing  missing  missing 
   2 │ missing  missing  missing  missing 
   3 │ missing  missing  missing  missing 
   4 │ missing  missing  missing  missing

Upvotes: 3

akrun
akrun

Reputation: 887118

We may use

Array{Union{Missing, String}}(missing, 4, 4)

Upvotes: 4

Related Questions