Extracting Yuima Diffusion Simulations for Three-Dimensional Plotting
Article Quant Q&A · Author: reverendjamesm
Summary
The document describes how to turn a simulated three-dimensional diffusion path from the R package Yuima into data suitable for a spatial plot. The default plot displays each simulated variable as a separate time series, while the author wants to view the joint path through the three state coordinates. The accepted answer retrieves each component series from the simulation object and combines them as columns in a matrix, which can then be passed to a three-dimensional plotting tool.
Key ideas
- Yuima’s default simulation plot shows each state variable over time.
- The simulated coordinate series can be retrieved from the simulation object’s stored data.
- Combining the three series as matrix columns provides coordinates for plotting the joint path.
- The answer addresses data extraction and visualization, not the statistical properties or validation of the diffusion model.
Tags
Full text
# R - Plotting a 3-dimensional sample path in yuima?
# R - Plotting a 3-dimensional sample path in yuima?
Apologies if this is not the appropriate place to post this - this my very first contribution to Quantitative Finance Stack Exchange. I was hoping someone could help me with the following issue. I am using `yuima` to model a 3-dimensional diffusion process:
```
model <- setModel(drift = c("((-1)/(2-x1))-1/2","0","0"),
diffusion = matrix(c("1","0","0","0","1","0","0","0","1"), 3, 3),
solve.variable = c("x1","x2","x3"))
```
and then to simulate it and plot it:
```
sampling <- setSampling(Initial = 0, Terminal = 10, n = 1000)
yuima <- setYuima(model = model, sampling = sampling)
simulation <- simulate(yuima,xinit = 1)
plot(simulation)
```
which seems to work. However: this generates a plot of each time-series `x1`, `x2`, and `x3` over time when in reality what I am really trying to visualise is how the three-dimensional path (with polar coordinates `x1`, `x2`, and `x3`) would look like.
Unless there is a version of `plot3D` or similar in `yuima` (I have googled it with no luck), what would really help me would be if there was a way of converting the (three) time-series `simulation` into a matrix or a list, in which case I am pretty sure I would be able to get the desired plot.
Any help will be much appreaciated. All the best.
Edit: the answer I've selected solved my issue, but for future reference these are the contents of `simulate`:
```
> print(str(simulate))
Formal class 'standardGeneric' [package "methods"] with 8 slots
..@ .Data :function (object, nsim = 1, seed = NULL, xinit, true.parameter, space.discretized = FALSE, increment.W = NULL, increment.L = NULL,
method = "euler", hurst, methodfGn = "WoodChan", sampling = sampling, subsampling = subsampling, ...)
..@ generic : chr "simulate"
.. ..- attr(*, "package")= chr "yuima"
..@ package : chr "yuima"
..@ group : list()
..@ valueClass: chr(0)
..@ signature : chr [1:13] "object" "nsim" "seed" "xinit" ...
..@ default : NULL
..@ skeleton : language (function (object, nsim = 1, seed = NULL, xinit, true.parameter, space.discretized = FALSE, increment.W = NULL, i| __truncated__ ...
NULL
```
## Answer by Pleb (score 0, accepted)
https://quant.stackexchange.com/a/63962
You can recover the simulations from the Yuima simulation object via the field calls:
```
Ser1 <- simulation@data@zoo.data$"Series 1"
Ser2 <- simulation@data@zoo.data$"Series 2"
Ser3 <- simulation@data@zoo.data$"Series 3"
```
And then convert the above into a matrix using `cbind`:
```
Sims_to_matrix <- cbind(Ser1, Ser2, Ser3)
```
Now, you can use your favorite 3d plotting tool (eg. `Plotly`), to get what you desire. You could also construct your own fancy line-graphs using `ggplot2`. I hope this helps!Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.