0% found this document useful (0 votes)
2 views3 pages

R Programming: Statistical Analysis & Exercises

Uploaded by

ESHNA JAIN
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

R Programming: Statistical Analysis & Exercises

Uploaded by

ESHNA JAIN
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Introduction to R

Theory of mathematical finance and extreme value

basic statistical topics of descriptive, inferential, multivariate, and time series analysis

quantification of risk and another to high-frequency data

machine learning techniques

simulation, namely Bayesian Monte Carlo

Programming Exercises for R

Q1. Execute the following lines which create two vectors of random integers which are chosen
with replacement from the integers 0, 1, . . . , 999. Both vectors have length 250.

[Link](50)

xVec <- sample(0:999, 250, replace=T) yVec <- sample(0:999, 250, replace=T)

(a) Pick out the values in yVec which are > 600.
(b) What are the index positions in yVec of the values which are > 600?
(c) What are the values in xVec which correspond to the values in yVec which are > 600? (By
correspond, we mean at the same index positions.)
(d) Create the vector (|x1 − x¯|1/2 , |x2 − x¯|1/2 , . . . , |xn − x¯| 1/2 ) where x¯ denotes the mean of
the vector x = (x1, x2, . . . , xn).
(e) How many values in yVec are within 200 of the maximum value of the terms in yVec?
(f) How many numbers in xVec are divisible by 2? (Note that the modulo operator is denoted
%%.)
(g) Sort the numbers in the vector xVec in the order of increasing values in yVec.
(h) Pick out the elements in yVec at index positions 1, 4, 7, 10, 13, . . .

Q2. Create a 8 × 10 matrix of random integers chosen from 1, 2,. . . , 10 by executing the
following two lines of code:

[Link](75)

aMat <- matrix( sample(10, size=80, replace=T), nr=8)

(a) Find the number of entries in each row which are greater than 4.

(b) Which rows contain exactly two occurrences of the number seven?
Q3.

Q4. Consider the continuous function

{
x 2 +2 x +3 if x <0
f ( x )= x +3 if 0 ≤ x <2
2
x + 4 x if 2≤ x

Write a function Fn which takes a single argument xVec. The function should return the vector of
values of the function f(x) evaluated at the values in xVec. Hence plot the function f(x) for −3 < x <
3.

Q5. Consider the simple returns of monthly indexes of U.S. government bonds with maturities in
30 years, 20 years, 10 years, 5 years, and 1 year. The data obtained from the CRSP database have
696 observations starting from January 1942 to December 1999. [[Link]]

Save this file and use [Link] to import it into R.

What are the means and standard deviations of the simple returns of monthly indexes of U.S.
government bonds with maturities in 30 years, 20 years, 10 years, 5 years, and 1 year?

Examine the histograms and boxplots of the simple returns of monthly indexes of U.S. government
bonds with maturities in 30 years, 20 years, 10 years, 5 years, and 1 year.

(a) Mean
(b) Median
(c) Quartiles
(d) 32nd, 57th and 98th percentiles
(e) Range
(f) Interquartile range
(g) Box plot
(h) Variance
(i) Standard deviation
(j) Third central moment
(k) Skewness
(l) Kurtosis
(m)Plot the scatter plot and then find the correlation coefficient of the simple returns of
monthly indexes of U.S. government bonds with all maturities return. Observe if there is
any linear relationship between the simple returns with different maturities.

(a) yVec[yVec>600] (b) (1:length(yVec))[yVec>600] or which(yVec>600) (c)


xVec[yVec>600] (d) sqrt(abs(xVec-mean(xVec))) (e) sum( yVec>max(yVec)-200 ) (f)
sum(xVec%%2==0) (g) xVec[order(yVec)] (h) yVec[c(T,F,F)]

a) apply(aMat, 1, function(x){sum(x>4)}) (b) which( apply(aMat,1,function(x)


{sum(x==7)==2}) )

(a) sum( (1:20)^4 ) * sum( 1/(4:8) ) or sum(outer((1:20)^4,4:8,"/")) The answer is 639,215. (b)
sum( (1:20)^4 / (3 + outer(1:20,1:5,"*"))) The answer is 89,912.021. (c)
sum( outer(1:10,1:10,function(i,j){ (i>=j)*i^4/(3+i*j) }) ) The answer is 6,944.7434

Fn <- function(x) { ifelse(x < 0, x^2 + 2*x + 3, ifelse(x < 2, x+3, x^2 + 4*x - 7)) }

tmp <- seq(-3, 3, len=100) plot(tmp, Fn(tmp), type="l")

Common questions

Powered by AI

To identify the indexes of values greater than 600 in a vector, you can use the which function in R: 'which(yVec > 600)'. This will return the positions of the vector 'yVec' where the values are greater than 600 .

The third central moment, skewness, and kurtosis are critical in understanding financial return distributions. The third central moment measures symmetry, whereas skewness quantifies the degree of asymmetry in a distribution. Kurtosis indicates whether data peaks are sharp or flat compared to a normal distribution. Together, these moments provide a comprehensive view of potential tail risks and dispersion in return distributions .

To perform a correlation analysis between different bond maturity returns, you need to first plot their simple returns for visual examination and then use R's correlation functions like 'cor()' to compute the correlation coefficient, which indicates the strength and direction of the linear relationship between the returns with different maturities .

The interquartile range (IQR) of bond returns can be computed in R using the 'IQR()' function. It measures the middle 50% of the data, thereby indicating the spread and concentration of data around the median. It is a robust statistic that is less affected by outliers and provides a clearer picture of data distribution .

Matrix entries can be evaluated to meet specific conditions using the 'apply' function in R, which applies a function to the rows or columns of a matrix. For instance, 'apply(aMat, 1, function(x){sum(x>4)})' counts entries greater than 4 across each row, and 'which( apply(aMat,1,function(x) {sum(x==7)==2}) )' identifies rows where the number 7 appears exactly twice .

To visualize distributional characteristics of bond maturity returns, histograms and boxplots are effective tools. A histogram provides a visual representation of the distribution frequency, while a boxplot displays the data’s central tendency, quartiles, and potential outliers. Boxplots are especially useful for comparing distributions across different bond maturities while providing insights into variance and skewness .

Bayesian Monte Carlo simulations combine probability theory and simulation to assess financial risks and model uncertainty in data analysis. They enable you to incorporate prior knowledge about model parameters and update predictions as new data becomes available, thus capturing the dynamic nature of financial markets and refining risk assessments and forecasts .

To sort a numerical vector based order of another vector, use the order function in R. For instance, 'xVec[order(yVec)]' sorts 'xVec' based on the increasing values of 'yVec'. This approach ensures that the sort index aligns with the positions determined by the second vector .

The document discusses various statistical methods used in the context of mathematical finance, focusing on descriptive and inferential statistics, multivariate analysis, time series analysis, and the quantification of risk. It also mentions the use of machine learning techniques and Bayesian Monte Carlo simulations to handle high-frequency data .

The function 'Fn' is designed to compute the values of a piecewise continuous function defined as different algebraic expressions over different intervals of x. The function evaluates 'x^2 + 2*x + 3' for x < 0, 'x + 3' for 0 ≤ x < 2, and 'x^2 + 4*x - 7' for x ≥ 2. This function helps in modeling scenarios where the relationship between variables changes according to the values of the controlling variable .

You might also like