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

**URL:** <https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 22, 2021, 3:26pm UTC](https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782 "2021-02-22T15:26:45Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [February 22, 2021, 3:26pm UTC](https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782/1 "2021-02-22T15:26:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 22, 2021, 3:33pm UTC](https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782/2 "2021-02-22T15:33:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [February 22, 2021, 3:39pm UTC](https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782/3 "2021-02-22T15:39:25Z")

</div>

Thank you very much for your reply.

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

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

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 22, 2021, 3:43pm UTC](https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782/4 "2021-02-22T15:43:46Z")

</div>

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?

PS. Please [quote your code](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530).

---

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [February 22, 2021, 3:44pm UTC](https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782/5 "2021-02-22T15:44:48Z")

</div>

Sorry, tauchen is from using QuantEcon

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 22, 2021, 3:46pm UTC](https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782/6 "2021-02-22T15:46:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [February 22, 2021, 3:47pm UTC](https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782/7 "2021-02-22T15:47:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 22, 2021, 3:49pm UTC](https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782/8 "2021-02-22T15:49:53Z")

</div>

According to the [QuantEcon documentation](http://quantecon.github.io/QuantEcon.jl/latest/api/QuantEcon.html#QuantEcon.MarkovChain), the transition matrix is in the field `shock.p`; presumably this is what you want to multiply by?

---

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [February 22, 2021, 4:03pm UTC](https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782/9 "2021-02-22T16:03:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 22, 2021, 4:21pm UTC](https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782/10 "2021-02-22T16:21:54Z")

</div>

> [@Zerosum](#):
>
> 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?

---

<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:** [February 22, 2021, 4:25pm UTC](https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782/11 "2021-02-22T16:25:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [February 23, 2021, 2:13pm UTC](https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782/12 "2021-02-23T14:13:48Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [February 23, 2021, 2:14pm UTC](https://discourse.julialang.org/t/question-how-can-i-multiply-a-vector-to-a-stochastic-matrix/55782/13 "2021-02-23T14:14:26Z")

</div>

I see. I will look for those!

Thank you very much!
