# Shock Decompositions in Julia

**URL:** <https://discourse.julialang.org/t/shock-decompositions-in-julia/24761>\
**Category:** Visualization\
**Created:** [May 30, 2019, 10:10am UTC](https://discourse.julialang.org/t/shock-decompositions-in-julia/24761 "2019-05-30T10:10:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nik.ltin](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@nik.ltin](https://discourse.julialang.org/u/nik.ltin)\
**Post date:** [May 30, 2019, 10:10am UTC](https://discourse.julialang.org/t/shock-decompositions-in-julia/24761/1 "2019-05-30T10:10:46Z")

</div>

Hello, I’m attempting to do historical cumulated shock decompositions in Julia similar to those done in Matlab’s Dynare (attached an example below). Not sure as to how to go about doing this? Using Plots.jl to match up each y-value and individually creating data frames for each set of stacks is proving to be very cumbersome and inefficient. Any help would be appreciated, thanks.

 ![shock_decomposition](https://global.discourse-cdn.com/julialang/original/3X/e/7/e78661f4e8c21ef2433310e63625a4446d28ec4b.jpeg)

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 30, 2019, 10:24am UTC](https://discourse.julialang.org/t/shock-decompositions-in-julia/24761/2 "2019-05-30T10:24:09Z")

</div>

Not really sure what that is, but check [GitHub - JuliaPlots/StatsPlots.jl: Statistical plotting recipes for Plots.jl](https://github.com/JuliaPlots/StatsPlots.jl#grouped-bar-plots)

---

<div class="post-metadata">

**Author:** ![nik.ltin](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@nik.ltin](https://discourse.julialang.org/u/nik.ltin)\
**Post date:** [May 30, 2019, 10:31am UTC](https://discourse.julialang.org/t/shock-decompositions-in-julia/24761/3 "2019-05-30T10:31:35Z")

</div>

Hi, yes this is what I was referring to: creating data frames for individual sets of stacks for 200 periods of time and several parameters (i.e. shocks here) that are decomposed cumulatively is turning out to be very inefficient. In Dynare for example, since it’s built for these DSGE models in economics, there already exist several functions/packages that do so, and I was wondering if there are easier or more effective ways of doing so. In my figure, for example, I’m decomposing the 11 shocks that affect a variable so as to see their sizes proportional to the cumulated aggregate effect.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 30, 2019, 11:02am UTC](https://discourse.julialang.org/t/shock-decompositions-in-julia/24761/4 "2019-05-30T11:02:51Z")

</div>

If you need help plotting something like this, a self-contained MWE that generates the data could be helpful.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 30, 2019, 1:11pm UTC](https://discourse.julialang.org/t/shock-decompositions-in-julia/24761/5 "2019-05-30T13:11:33Z")

</div>

I’m not 100% clear on what your chart is doing - @mkborregaard’s suggestion is for a stacked bar, but it seems to me like your original chart just overlays a bunch of separate bar charts, all starting from the origin, with an additional line chart showing the net effect?

Examples:

```julia
using StatsPlots

shocks = hcat(2*rand(100), -rand(100), 0.5*rand(100))
shock_names = ["alpha", "beta", "gamma"]

total_impact = sum(shocks, dims = 2)

groupedbar(shocks, bar_position = :stack, labels = shock_names)

```

![groupedbar](https://global.discourse-cdn.com/julialang/original/3X/0/a/0ae83b4727688cf7713b741f81663d9a936dc9e0.png)

or alternatively, if you’re overlaying histograms and plotting a separate total:

```julia
p = plot()

for (i, s) ∈ enumerate(eachcol(shocks))
    bar!(p, s, label = shock_names[i])
end

plot!(p, total_impact, label = "Total impact", linewidth=3, color = "black")

```

![overlaid_hists](https://global.discourse-cdn.com/julialang/original/3X/8/1/818e9ddb20e972f5897933254ce9a6191b5db464.png)

although in the latter case you might want to turn down the `alpha` to make overlaid series visible!

---

<div class="post-metadata">

**Author:** ![nik.ltin](https://avatars.discourse-cdn.com/v4/letter/n/6bbea6/32.png) [@nik.ltin](https://discourse.julialang.org/u/nik.ltin)\
**Post date:** [May 30, 2019, 2:42pm UTC](https://discourse.julialang.org/t/shock-decompositions-in-julia/24761/6 "2019-05-30T14:42:57Z")

</div>

This was perfect, thanks a lot!
