Finance & BankingQuantitative Finance

Free PDF Book: Introduction to R for Quantitative Finance – Solve a diverse range of problems with R, one of the most powerful tools for quantitative finance

Introduction to R for Quantitative Finance - Solve a diverse range of problems with R, one of the most powerful tools for quantitative finance

What this book covers

Chapter 1

Time Series Analysis (Michael Puhle), explains working with time series data in R. Furthermore, you will learn how to model and forecast house prices, improve hedge ratios using cointegration, and model volatility.

Chapter 2

Portfolio Optimization (Péter Csóka, Ferenc Illés, Gergely Daróczi), covers the theoretical idea behind portfolio selection and shows how to apply this knowledge to real-world data.

Chapter 3

Asset Pricing Models (Kata Váradi, Barbara Mária Dömötör, Gergely Daróczi), builds on the previous chapter and presents models for the relationship between asset return and risk. We’ll cover the Capital Asset Pricing Model and the Arbitrage Pricing Theory.

Chapter 4

Fixed Income Securities (Márton Michaletzky, Gergely Daróczi), deals with the basics of fixed income instruments. Furthermore, you will learn how to calculate the risk of such an instrument and construct portfolios that will be immune to changes in interest rates.

Chapter 5

Estimating the Term Structure of Interest Rates (Tamás Makara, Gergely Daróczi), introduces the concept of a yield curve and shows how to estimate it using prices of government bonds.

Chapter 6

Derivatives Pricing (Ágnes Vidovics-Dancs, Gergely Daróczi), explains the pricing of derivatives using discrete and continuous time models. Furthermore, you will learn how to calculate derivatives risk measures and the so-called “Greeks”.

Chapter 7

Credit Risk Management (Dániel Havran, Gergely Daróczi), gives an introduction to the credit default models and shows how to model correlated defaults using copulas.

Chapter 8

Extreme Value Theory (Zsolt Tulassay), presents possible uses of Extreme Value Theory in insurance and finance. You will learn how to fit a model to the tails of the distribution of fire losses. Then we will use the fitted model to calculate Value-at-Risk and Expected Shortfall.

Chapter 9

Financial Networks (Edina Berlinger, Gergely Daróczi), explains how financial networks can be represented, simulated, visualized, and analyzed in R. We will analyze the interbank lending market and learn how to systemically detect important financial institutions.

 

Who this book is for

The book is aimed at readers who wish to use R to solve problems in quantitative finance. Some familiarity with finance is assumed, but we generally provide the financial theory as well. Familiarity with R is not assumed. Those who want to get started with R may find this book useful as we don’t give a complete overview of the R language but show how to use parts of it to solve specific problems. Even if you already use R, you will surely be amazed to see the wide range of problems that it can be applied to.

 

Portfolio Optimization

By now we are familiar with the basics of the R language. We know how to analyze data, call its built-in functions, and apply them to the selected problems in a time series analysis. In this chapter we will use and extend this knowledge to discuss an important practical application: portfolio optimization, or in other words, security selection.

This section covers the idea behind portfolio optimization: the mathematical models and theoretical solutions. To improve programming skills, we will implement an algorithm line by line using real data to solve a real-world example. We will also use the pre-written R packages on the same data set.

Imagine that we live in a tropical island and have only USD 100 to invest. Investment possibilities on the island are very limited; we can invest our entire fund into either ice creams or umbrellas. The payoffs that depend on the weather are as follows:R quantitative finance

Suppose the probability of the weather being rainy or sunny is the same. If we cannot foresee or change the weather, the two options are clearly equivalent and we have an expected return of 5% [(0.5×120+0.5×90)/100-1=0.05] by investing in any of them.

What if we can split our funds between ice creams and umbrellas? Then we should invest USD 50 in both the options. This portfolio is riskless because whatever happens, we earn USD 45 with one asset and USD 60 with the other one. The expected return is still 5%, but now it is guaranteed since (45+60)/100-1=0.05.

R quantitative finance- Portfolio variance
 

Solution concepts

In the last 50 years, many great algorithms have been developed for numerical optimization and these algorithms work well, especially in case of quadratic functions. As we have seen in the previous section, we only have quadratic functions and constraints; so these methods (that are implemented in R as well) can be used in the worst case scenarios (if there is nothing better).

However, a detailed discussion of numerical optimization is out of the scope of this book. Fortunately, in the special case of linear and quadratic functions and constraints, these methods are unnecessary; we can use the Lagrange theorem from the 18th century.

 

Asset Pricing Models

Covered in this chapter are the problem of absolute pricing (Cochrane 2005) and how the value of assets with uncertain payments is determined based on their risk. Chapter 2, Portfolio Optimization, modeled the decision-making of an individual investor based on the analysis of the assets’ return in a mean variance framework.

This chapter focuses on whether or not equilibrium can exist in financial markets, what conditions are needed, and how it can be characterized. Two main approaches—Capital Asset Pricing Model and Arbitrage Pricing Theory—will be presented, which use completely different assumptions and argumentation, but give similar descriptions of the return evolution.

According to the concept of relative pricing, the riskiness of the underlying product is already involved in its price and, so, it does not play any further role in the pricing of the derived instrument; this will be presented in Chapter 6, Derivatives Pricing. The no-arbitrage argument will force consistency in the prices of the derivativeand underlying assets there.

The objective of this chapter is to present the relationship between the asset return and the risk factor. We will explain how to download and clean data from multiple sources. Linear regression is used to measure the dependence and the connected hypothesis test shows the significance of the results. The one-factor index model
is tested through a two-step regression process and the financial interpretation of the results is shown.

 

Capital Asset Pricing Model

The first type of model explaining asset prices uses economic considerations. Using the results of the portfolio selection presented in the previous chapter, the Capital Asset Pricing Model (CAPM) gives an answer to the question asking what can be said of the market by aggregating the rational investors’ decisions and, also, by what assumption the equilibrium would evolve. Sharpe (1964) and Lintner (1965) prove the existence of the equilibrium subject to the following assumptions:

  • Individual investors are price takers
  • Single-period investment horizon
  • Investments are limited to traded financial assets
  • No taxes and no transaction costs
  • Information is costless and available to all investors
  • Investors are rational mean-variance optimizers
  • Homogenous expectations

 

Fixed Income Securities

In Chapter 3, Asset Pricing Models, we focused on models establishing a relationship between the risk measured by its beta, the price of financial instruments, and portfolios. The first model, CAPM, used an equilibrium approach, while the second, APT, has built on the no-arbitrage assumption.

The general objective of fixed income portfolio management is to set up a portfolio of fixed income securities with a given risk/reward profile. In other words, portfolio managers are aiming at allocating their funds into different fixed income securities, in a way that maximizes the expected return of the portfolio while adhering to the given investment objectives.

The process encompasses the dynamic modeling of the yield curve, the prepayment behavior, and the default of the securities. The tools used are time series analysis, stochastic processes, and optimization.

The risks of fixed income securities include credit risk, liquidity risk, and market risk among others. The first two can be handled by selecting only securities with predetermined default risk, for example, with a minimum credit rating and with proper liquidity characteristics. The market risk of a fixed income security is generally captured by duration, modified duration, keynote duration, or factor duration. All measures of the interest rate risk a fixed income security faces. This chapter focuses on the market risk of fixed income securities.

 

Immunization of fixed income portfolios

A portfolio is immunized when it is unaffected by interest rate change. Duration gives a good measure of interest rate sensitivity; therefore, it is generally used to immunize portfolios. As using duration assumes a flat yield curve and a little parallel shift of the yield curve, the immunized portfolio is constrained by these assumptions, and being unaffected will mean that the value of the portfolio changes only slightly as yields change.

There are two different kinds of immunization strategies: net worth immunization and target date immunization.

 

Net worth immunization

Fixed income portfolio managers often have a view on the way the yield curve will change in the future. Let us assume that a portfolio manager expects rates to increase in the near future. As this would have an unfavorable effect on the portfolio, the portfolio manager could decide to set the duration of the portfolio to zero by entering into forward agreements or interest rate swaps. These instruments alter the portfolio’s duration and can help in setting the portfolio’s duration to zero without having to liquidate the entire portfolio.

Another goal of a portfolio manager can be to set the duration of the portfolio relative to the duration of the portfolio’s benchmark. This helps in outperforming the portfolio’s benchmark should their anticipation on market movements be justified.

Banks are usually more interested in protecting their equities’ value from market price changes. This is carried out by setting their equities’ duration to the desired level.

 

Estimating the Term Structure of Interest Rates

In the previous chapter we discussed how changes in the level of interest rates, the term structure, affect the prices of fixed income securities. Now we focus on the estimation of the term structure of interest rates, which is a fundamental concept in finance. It is an important input in almost all financial decisions. This chapter will introduce term structure estimation methods by cubic spline regression, and it will demonstrate how one can estimate the term structure of interest rates using the termstrc package and the govbonds dataset.

 

Derivatives Pricing

Derivatives are financial instruments which derive their value from (or are dependent on) the value of another product, called the underlying. The three basic types of derivatives are forward and futures contracts, swaps, and options. In this chapter we will focus on this latter class and show how basic option pricing models and some related problems can be handled in R.

We will start with overviewing how to use the continuous Black-Scholes model and the binomial Cox-Ross-Rubinstein model in R, and then we will proceed with discussing the connection between these models. Furthermore, with the help of calculating and plotting of the Greeks, we will show how to analyze the most important types of market risks that options involve. Finally, we will discuss what implied volatility means and will illustrate this phenomenon by plotting the volatility smile with the help of real market data.

The most important characteristics of options compared to futures or swaps is that you cannot be sure whether the transaction (buying or selling the underlying) will take place or not. This feature makes option pricing more complex and requires all models to make assumptions regarding the future price movements of the underlying product.

The two models we are covering here differ in these assumptions: the Black-Scholes model works with a continuous process while the Cox-Ross-Rubinstein model works with a discrete stochastic process. However, the remaining assumptions are very similar and we will see that the results are close (moreover, principally identical) too.

 

Credit Risk Management

This chapter introduces some useful tools for credit risk management. Credit risk is the distribution of the financial losses due to unexpected changes in the credit quality of a counterparty in a financial agreement (Giesecke 2004). Several tools and industrial solutions were developed for managing credit risk. In accordance with the literature, one may consider credit risk as the default risk, downgrade risk, or counterparty risk. In most cases, the default risk is related directly to the risk of non-performance of a claim or credit.

In contrast, downgrade risk arises when the price of a bond declines due to its worsening credit rating without any realized credit event. Counterparty risk means the risk when the counterparty of a contract does not meet the contractual obligations. However, the contractual or regulatory definition of a credit event can usually be wider than just a missed payment.

The modeling end estimation of the possibility of default is an essential needin all of the three cases.
Managing credit risk is conducted in various ways at financial institutions. In general, the tasks in credit risk management are as follows:

  • Credit portfolio selection (for example, the decision of a commercial bank about lending or credit scoring)
  • Measuring and predicting the probability of default or downgrade (using, for example, a credit rating migration matrix with CreditMetrics)
  • Modeling the distribution of the financial loss due to default or downgrade (for a single entity: structural and reduced form pricing and risk models or, for a portfolio: dependency structure modeling)
  • Mitigating or eliminating credit risk (with a hedge, diversification, prevention, or insurance; we do not investigate it in this book)

In this chapter, we will show examples using R for some of the preceding listed problems. At first, we introduce the basic concepts of credit loss modeling, namely, the structural and reduced form approaches, and their applications in R. After that, we provide a practical way correlated random variables with copulas, which is
a useful technique of structured credit derivative pricing. We also illustrate how R manages credit migration matrices and, finally, we give detailed insight into credit scoring with analysis tools, such as logit and probit regressions and receiver operating characteristic (ROC) analysis.

Back to top button
Close