<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/introduction-to-copulas-exercises-part-2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/05/17/introduction-to-copulas-exercises-part-2/?utm_source=rss&utm_medium=rss&utm_campaign=introduction-to-copulas-exercises-part-2"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/time_series_exercises_part4-27.png" target="_blank"><img sizes="(max-width: 150px) 100vw, 150px" src="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/time_series_exercises_part4-27-150x150.png?resize=150%2C150" alt="" class="wp-image-5864" data-recalc-dims="1"></a><br>
Copulas are a powerful statistical tool commonly used in the finance sector to generate samples from a given multivariate joint distribution.<br>
The principal advantage of using those types of function over other methods is that copulas describe the multivariate joint distribution as his margin and the dependence structure between them,<br>
which give the user the power to fine tune his model component by component.<br>
For example, if you have two independent variables of known distribution <code>X_1</code> and <code>X_2</code> which interact to create a dependant<br>
variable <code>Y</code> you can set <code>X_1</code> and <code>X_2</code> as the margin of the distribution of <code>Y</code> and find the appropriate copula to simulate the interaction between the margins and fit the data.</p>
<p>In the <a rel="nofollow" href="http://www.r-exercises.com/2017/05/11/introduction-to-copulas-exercises-part-1/" target="_blank">previous post</a> we’ve seen how to create a copula object and how to generate sample with the most commonly used copula.<br>
In this post we’ll learn how to use choose a copula that fit your data and how to make a rough estimate of the probability of a given event.</p>
<p>To be able to do those exercises, you must have installed the packages <code>ggplot2</code>,<code>fitdistrplus</code>, <code>VineCopula</code> and <code>copula</code>. Also, you can find the dataset we’ll use<br>
for this set of exercises <a rel="nofollow" href="https://www.kaggle.com/gtouzin/samplestocksreturn" target="_blank">here</a>. It’s a clean dataset of the daily return of the Apple and Microsoft from<br>
May 2000 to May 2017.</p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://www.r-exercises.com/2017/05/17/introduction-to-copulas-solutions-part-1-2/" target="_blank">here</a>.</p>
<p><strong>Exercise 1</strong><br>
We’ll start by fitting the margin. First, do a histogram of both Apple and Microsoft returns to see the shape of both distributions.</p>
<p><strong>Exercise 2</strong><br>
Both distributions seems symmetric and have a domain which contain positive and negative values.<br>
Knowing those facts, use the <code>fitdist()</code> function to see how the normal, logistic and Cauchy distribution fit the Apple returns dataset.<br>
Which of those three distributions is best suited to simulate the Apple return dataset and what are the parameter of this distribution?</p>
<p><strong>Exercise 3</strong><br>
Repeat exercise 2 with the Microsoft return.</p>
<p><strong>Exercise 4</strong><br>
Plot the joint distribution of the Apple and Microsoft daily returns. Add the regression line to the plot and compute the correlation of both variables.</p>
<p><strong>Exercise 5</strong><br>
Use the <code>pobs()</code> from the <code>VineCopula()</code> package to compute the pseudo-observations for both returns values, then use the <code>BiCopSelect()</code> function to select the copula<br>
which minimise the AIC on the dataset. Which copula is selected and what are his parameters.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/05/17/introduction-to-copulas-exercises-part-2/?utm_source=rss&utm_medium=rss&utm_campaign=introduction-to-copulas-exercises-part-2"></aside>
<div><strong>Learn more</strong> about MultiVariate analysis in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/case-studies-in-data-mining-with-r/" target="_blank">Case Studies in Data Mining with R</a>. In this course you will work thru a case study related to multivariate analysis and how to work with forecasting in the S&amp;P 500.</div>
</div>
<p><strong>Exercise 6</strong><br>
Use the appropriate function from the <code>VineCopula()</code> package to create a copula object with the parameter computed in the last exercise. Then, do a three dimensional plot and a contour plot of the copula.</p>
<p><strong>Exercise 7</strong><br>
Set the seed to 42 and generate a sample of 1000 points from this copula. Plot the sample and calculate the correlation of this sample.<br>
Does the correlation of the sample is similar to the correlation between the Apple and Microsoft returns?</p>
<p><strong>Exercise 8</strong><br>
Create a distribution from the copula you selected and the margins you fitted in the exercise 2 and 3.</p>
<p><strong>Exercise 9</strong><br>
Generate 1000 points from the distribution of exercise 8 and plot those points, with the Apple and Microsoft returns, in the same plot.</p>
<p><strong>Exercise 10</strong><br>
Having made a model, let’s make some crude estimation with it! Imagine that this model has been proven to be effective to describe the relation between the apple return and<br>
the Microsoft return for a considerable amound of time and there’s a spike in the price of Apple stock. Suppose you have another model who describe the Apple stock and who<br>
lead you to believe that the daily return on this stock has a 90% chance to be between 0.038 and 0.045. Using only this information, compute the range containing the possible<br>
daily return of the Microsoft stock at the end of the day and the mean of the possible values.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2016/07/26/stock-prices-analysis-part-1-exercises/" title="Stock prices analysis  part 1 exercises" target="_blank">Stock prices analysis part 1 exercises </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/02/13/data-science-for-doctors-part-3-distributions/" title="Data Science for Doctors – Part 3 : Distributions" target="_blank">Data Science for Doctors – Part 3 : Distributions </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2016/08/02/stock-prices-analysis-part-2-exercises/" title="Stock prices analysis part 2 exercises" target="_blank">Stock prices analysis part 2 exercises </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-exercises.com/2017/05/17/introduction-to-copulas-exercises-part-2/?utm_source=rss&utm_medium=rss&utm_campaign=introduction-to-copulas-exercises-part-2"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/FHDOhCrNx3s" height="1"></a></div>