# Question) How can I multiply a vector to a stochastic matrix?

I finally managed to use tauchen function. However, the type of outcome is Discrete Markov Chain stochastic matrix of type Array{Float64,2}:

I tried to multiply a scalar, a vector, or any kind of algebra on it, but Julia returns

MethodError: no method matching length(::MarkovChain{Float64,Array{Float64,2},StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}})

Is there any way that I can convert the outcome of tauchen as a normal matrix so that I can do some multiplication?

Thank you

It would be helpful if you posted the code that you tried. i.e. post working code that generates your matrix, along with the code that you tried to use to multiply it by a vector.

1 Like

``````ρ= 0.004     # persistence of the shock
σ= 0.0165    # Volatility of the innovation
μ= 0       # Average of the process
N= 5    # Number of points in the grid of shocks
n_std= 3     # number of std deviations away from the mean

shock = tauchen(N,ρ,σ,μ,n_std)    # If Tauchen's (1986) method is chosen
shock .+ 2
shock + 2
shock .*2
``````

None of them work and it returns :

``````MethodError: no method matching length(::MarkovChain{Float64,Array{Float64,2},StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}})
Closest candidates are:
length(!Matched::DataStructures.EnumerateAll) at C:\Users\1\.julia\packages\DataStructures\TX3PX\src\multi_dict.jl:103
length(!Matched::Plots.GridLayout) at C:\Users\1\.julia\packages\Plots\5srrj\src\layouts.jl:289
length(!Matched::Cmd) at process.jl:638
``````

This code is not runnable unless you specify where you got the `tauchen` function from. Is it a package? (There is no `using` statement in the code you quoted.)

Also, you did’t quote the whole error message; what is the backtrace?

1 Like

Sorry, tauchen is from using QuantEcon

Also, I don’t see any matrix–vector multiplication (`2` is a scalar).

I did try with multiplying zeros(5) for instance. But, that also does not work.

According to the QuantEcon documentation, the transition matrix is in the field `shock.p`; presumably this is what you want to multiply by?

1 Like

thank you so much!!! now it works.
Just one more stupid question. Could you tell me if there are operations like as.matrix() or as.data.frame() in R ??

You need to ask a more specific question. What particular type do you want to convert to a matrix?

3 Likes

Generally, this will be spelled `Matrix` or `DataFrame` and use multiple dispatch to work.

1 Like

I am just a too beginner to ask a specific question. I was more wanting to know what keywords I shall search on.

Thank you very much!

I see. I will look for those!

Thank you very much!