Back of the Envelope

Observations on the Theory and Empirics of Mathematical Finance

[WP] Shiny for Finance

leave a comment »

For those familiar with R, Shiny offers a great alternative to spreadsheets. Targeted at instructors, we have a new paper introducing Shiny for finance applications in the classroom. Here is a longish abstract:

Despite the popularity of open-source languages like R and Python in modern empirical research and the data-science industry, spreadsheet programs like Microsoft Excel remain the data analysis software of choice in much of the business-school curriculum, including at IIMA. Even if instructors are comfortable with modern programming languages, they have to pitch their courses at the level of computer literacy prevalent among students. Excel then appears to be a natural choice given its popularity, but this choice constrains the depth of analysis that is possible and requires a certain amount of dumbing-down of the subject by the instructor. Recent software advances however make the ubiquitous web browser a worthy challenger to the spreadsheet. This article introduces one such browser-based tool called Shiny for bringing finance applications to the classroom and smart phones. Fueled by the availability of high-quality R packages in finance and statistics, Shiny brings together the power of HTML with the R programming language. It naturally creates an environment for the instructor to focus on the role of parameters and assumptions in analysis without the clutter of data, and allows the instructor to go beyond the toy problems that are necessitated by the nature of spreadsheets. The learning curve is short for an interested instructor with even a rudimentary exposure to programming in any language. The article ends with the discussion of a fully-worked out example of Shiny for teaching the mean variance efficient frontier in a basic investments course.

The working paper is here. It needs a bit of fine tuning yet, but comments welcome.

Written by Vineet

April 4, 2017 at 12:30 pm

[OS] Moving to Xubuntu

with 2 comments

About a year and a half back I talked about my transition to Lubuntu. I found it pretty perfect for my usage, and have had no major issues with it but Chrome has been misbehaving a bit since I moved to LTS 16.04. While fixes exist, I couldn’t find anything permanent, and since I had some time I thought it might be a good opportunity to try the slightly more endowed Xubuntu – with a slightly larger user base while remaining sufficiently lean.

In the last couple of days, I have tested Xubuntu on my office desktop (i5) and my Asus laptop (i3). Its boot time is similar to Lubuntu, and Chrome and Firefox open quickly enough (if anything, a bit quicker than Lubuntu). Many settings are similar to those in Lubuntu (e.g. setting proxy), but setting keyboard shortcuts is even easier. So far I am not missing Lubuntu.

Building upon the earlier discussion on Lubuntu, here are my settings for a minimalist Xubuntu (on an i3 machine with 4 GB RAM). I use them in the following order:

  1. Disk partitioning scheme:
    /boot = 4 GB, /root = 16 GB, /swap = 4 GB, /tmp = 4 GB
    /opt = 6 GB, /var = 6 GB, /usr = 24 GB, /usr/local = 16 GB

    /home takes up the remaining space. I also leave about 24 GB space free for emergency.

  2.  First update:
    sudo apt-get update && sudo apt-get upgrade && sudo apt-get install aptitude
  3. Essentials:
    sudo aptitude install synaptic xubuntu-restricted-extras chromium-browser
    sudo aptitude install gdebi libboost-all-dev swig cmake git xdotool vlc
    sudo aptitude install python-gpgme mencoder curl libcurl4-openssl-dev
    sudo aptitude install usb-creator-gtk p7zip keepass2 cryptsetup cryptmount
    sudo aptitude install lame alsaplayer-common alsaplayer-text soundconverter
    sudo aptitude install pdftk pdfchain djview4 zathura zathura-ps zathura-djvu
    sudo aptitude install gparted gimp getmail4 gnome-search-tool ibritish
  4. At this stage I set-up Dropbox in a separate partition and let the folders sync. In the meantime I create the necessary documentation infrastructure:
    sudo aptitude install emacs auctex pandoc markdown texlive-full

    You may install auctex and ess directly from the emacs package manager (containing the more recent versions). I install ess after getting R.

  5. Keyboard shortcuts: Settings Manager -> Settings Editor -> xfce4-keyboard-shortcuts
  6. Printer: If you have a duplex printer, and it does not automatically respect duplex settings, you may have to set it to ON from Settings -> Printer. If you have an HP printer, hplip-gui (available from synaptic) is also useful.
  7. Speedup boot: 
    • Remove unnecessary start-up services (and add any) from Settings Manager -> Session and Startup
    • Create a file ~/.gtkrc-2.0 and paste the following: Hat-tip BinaryTides
      gtk-menu-popup-delay = 0
      gtk-menu-popdown-delay = 0
      gtk-menu-bar-popup-delay = 0
      gtk-enable-animations = 0
      gtk-timeout-expand = 0
      gtk-timeout-initial = 0
      gtk-timeout-repeat = 0
  8. Python-specific: Hat-tip  Chris’ sandbox
    sudo aptitude install gfortran g++ build-essential pkg-config
    sudo aptitude install python-dev python3-dev python-pip python3-pip
    sudo aptitude install python3-tk libatlas-dev liblapack-dev libblas-dev
    sudo aptitude install libfreetype6 libfreetype6-dev libpng-dev libjpeg8-dev

    I no longer use Anaconda for Python and install all necessary packages within virtual environments (a useful intro is here (option 3)). Jorgen Schaefer’s elpy package means that I don’t have to leave emacs at all.

    In particular, after setting the virtual environments, I have the following installed packages (in that order) for data science/financial modeling related work in emacs:

    pip install numpy scipy pandas matplotlib
    pip install sympy seaborn jupyter scikit-learn statsmodels
    pip install rope jedi flake8 importmagic autopep8 yapf

Written by Vineet

January 9, 2017 at 8:53 pm

[Exec-Ed] “Practical Quantitative Finance using Python and QuantLib”

leave a comment »

Some bit of shameless self-promotion, for those with some interest in derivatives:

We are starting a new online executive program covering topics on derivatives pricing and computational finance using Python and QuantLib. The program begins in the 3rd week of January. If you’d like to join or are interested to know more, you may write to me or get in touch with our office. More details here.

Written by Vineet

January 1, 2017 at 6:19 pm

[Etc] So Long, Marianne

leave a comment »

R.I.P Leonard Cohen

Written by Vineet

November 13, 2016 at 2:01 pm

Posted in Stories

Tagged with ,

[PGP-I FM] Option bounds

leave a comment »

Put-Call Parity

We introduced these new products called Call and Put options by unbundling a forward and we had the following relationship:

\mbox{Long Forward = Long Call + Short Put}

 or alternatively,

F_T = S_T - K = \mbox{max}(S_T - K, 0) - \mbox{max}(K - S_T, 0)

\mbox{Value of Forward at T = Call Payoff at T - Put Payoff at T}

Since this is a relationship about a product being ‘broken’ into its constituents, if this holds at time T (i.e. in future value terms), this must also hold today (i.e. in PV terms), and we have the following result:

\mbox{Value of Forward Today = Value of Call Today - Value of Put Today}

\mbox{PV}(S_T - K) = \mbox{Call Price Today (C) - Put Price Today (P)}

or more succinctly in PV terms as:

\boxed{S - PV(K) = C - P}

This is referred to as the Put-Call Parity in options markets.

Having introduced the machinery of payoff diagrams we may also see this graphically. Consider the following payoff. It is known by alternative names of a Call + Bond or a Protective Put.

call-bond-protective-put(Call + Bond / Protective Put; Click to zoom)

The reason they are are known by multiple names is that one can break the same payoff down alternatively as a portfolio of a Call Option (C) and a Zero-Coupon Bond (PV(K)), or a portfolio of a Put Option (P) and Stock (S) as:

call-bond(Call + Bond; Click to zoom)

or

protective-put2(Protective Put; Click to zoom)

Since both Call + Bond and Protective Put represent the same payoff at expiration (T), they must be valued at the same price. That is, we must have:

\begin{aligned} \mbox{Call Option + Zero Coupon Bond} = \mbox{Protective Put} &= \mbox{Put Option + Stock} \\ \Rightarrow \mbox{PV(Call Option) + PV(Zero Coupon Bond)} &= \mbox{PV(Put Option) + PV(Stock)} \end{aligned}

Writing the present values of a Call and Put Option as \mbox{C} and \mbox{P} respectively, we again get the Put-Call Parity as:

\boxed{C + PV(K) = P + S}

So if one knows the price of a Put Option on a stock with strike K and time to maturity T, one can find the price of the Call Option on the same stock for same strike K and time to maturity T.

Option Bounds

Call Option: Lower Bound

Continuing with the same examples of a Call + Bond and Protective Put we now compare the payoff from a Call + Bond with that from holding a Stock, i.e. geometrically we compare the following two payoffs:

call-bond-stock1(Comparing Call + Bond and Stock; Click to zoom)

It’s clear that when the stock price at expiration S_T is more than K, both a Call + Bond and a Stock have the same payoff, but whenever the stock price at expiration, S_T is less than K, a Call  + Bond offers K \ge S_T, but a Stock offers only S_T.

That is, in many ‘states of the world’ a Call + Bond offers the same payoff as holding a stock, but in some states it offers more than what holding a stock offers. This suggests that the present value of Call + Bond must always be more than or equal to that of holding a simple stock. That is, we must have:

\begin{aligned} \mbox{PV(Call + Zero Coupon Bond)} &\ge \mbox{Stock} \\ \Rightarrow \mbox{PV(Call Option)} &\ge \mbox{PV(Stock - Zero Coupon Bond)} \end{aligned}

Writing the present values of a Call as \mbox{C} gives us the Lower Bound for the present value (price) of a Call Option as:

\boxed{C \ge S - PV(K)}

A more trivial lower bound for a Call Option, is, of course C \ge 0. If the strike price K is ‘high enough’ (so as to make S - PV(K) < 0), then, technically, the lower bound for a Call Option must be:

\boxed{C \ge\mbox{max}\big[ S - PV(K), 0 \big]}

Call Option: Intrinsic Value

The fact that PV(K) \le K gives us another result that:

\begin{aligned} C &\ge \mbox{max}\big[S - PV(K), 0 \big] \\ \Rightarrow C &\ge \mbox{max}\big[ S - K, 0 \big] \end{aligned}

The term \mbox{max}\big( S - K, 0 \big) is referred to as the Intrinsic Value of the Call Option, and a European Call Option is always worth more than its Intrinsic Value.

For American Options, the difference between the Call Option Price and its Intrinsic Value is sometimes referred to as in the market as the ‘Time Value of the Call Option’ (not to be confused with the time value of money. It is an unfortunate choice of words, but that’s how it is.) One can think of the difference between the price of the Call Option and its Intrinsic Value as that part of the Call Option price that is the ‘reward for waiting for the potential upsid’ and possibly exercising the option when the stock price is even higher. This is another way to see why American Call is no more valuable than a European call, because there is always a positive ‘reward for waiting’ for a Call on a non-dividend paying stock.

Call Option: Upper Bound

Finding the Upper Bound for the Call Option price is a lot easier, and is just equal to the stock price. Think of it this way – no one is going to pay anyone to get the right to buy a stock, when one can just purchase it directly from the market, i.e. we must have:

\boxed{C \le S}

Put Option: Lower Bound

Again, similar to the way we proceeded in the case of a Call Option, we now compare the payoff from a Protective Put with that from holding a Zero-coupon bond, i.e. now geometrically we compare the following two payoffs:

protective-put-bond(Comparing Protected Put and a Zero Coupon Bond; Click to zoom)

This time is the other way around, and we say that when the stock price at expiration S_T is less than K, both a Protected Put and a Zero Coupon Bond have the same payoff K, but whenever the stock price at expiration, S_T is more than K, a Protected Put offers S_T \ge K, but a Zero Coupon Bond, of course, always offers only K.

That is, in few ‘states of the world’ a Protected Put offers the same payoff as holding a Zero Coupon Bond, but in many states it offers more than what holding a Zero Coupon Bond offers. Again, this suggests that the present value of a Protected Put must always be more than or equal to the price of a Zero Coupon Bond. That is, we must have:

\begin{aligned} \mbox{PV(Protected Put)} &\ge \mbox{PV(Zero Coupon Bond)} \\ \mbox{or } \mbox{PV(Put Option + Stock)} &\ge \mbox{PV(Zero Coupon Bond)} \\ \Rightarrow \mbox{PV(Put Option)} &\ge \mbox{PV(Zero Coupon Bond) - Stock} \end{aligned}

Similarly, writing the present value of a Put as \mbox{P} gives us the Lower Bound for the present value (price) of a Put Option as:

\boxed{P \ge PV(K) - S}

Just like in the case of a Call Option, a more trivial lower bound for a Put Option, is, of course P \ge 0. If the strike price K is ‘low enough’ (so as to make PV(K) - S \le 0), then, technically, the lower bound for a European Put Option must be:

\boxed{P \ge \mbox{max}\big[ PV(K) - S, 0 \big]}

Put Option: Intrinsic Value

The fact that PV(K) \le K doesn’t help us much in the case of a Put Option (why? Hint: Compare K - S with PV(K) - S – which is larger?). The term $latex\mbox{max}(K – S, 0)$, however, is still referred to as the Intrinsic Value of the Put Option, though now one can’t say that a European Put Option is always worth more than its Intrinsic Value. The same we can say for an American Put Option, however, because an American Put can always be exercised prior to maturity and therefore must be worth at least as much as its Intrinsic Value, i.e. for an American put:

\boxed{P_{\mbox{American}} \ge K - S}

Put Option: Upper Bound

Upper bound for the Put Option price is similarly argued. Because one can always put money in the bank, a Put Option must never be sold for more than the value of a Zero Coupon Bond with principal K, i.e. we must have:

\boxed{P \le PV(K)}

Alternatively, one could argue that the maximum payoff at maturity from a European Put Option is just K, so the maximum value of a Put Option today must be just the present value of K, i.e. PV(K).

Because it may be optimal for an American put to be exercised early, the corresponding upper bound for an American put is:

\boxed{P_{\mbox{American}} \le K}

Written by Vineet

October 28, 2016 at 7:11 pm

[PGP-I FM] Payoff diagrams

with one comment

Payoff diagrams are simply diagrammatic representation of payoffs at termination/expiration of a contract w.r.t value of the underlying S_T.

For example, for a baker’s forward contract (long forward) it is a plot of its payoff (S_T - K) at expiration (T) w.r.t the wheat’s spot price S_T. Let’s recap the payoffs for some of the contract we have already talked about, and then some:

  1. Long Forward: F = S_T - K
  2. Short Forward: -F = K - S_T
  3. Long Call: C = \mbox{max}(S_T - K, 0)
  4. Short Call: -C = \mbox{- max}(S_T - K, 0)
  5. Long Put: P = \mbox{max}(K - S_T, 0)
  6. Short Put: -P = \mbox{- max}(K - S_T, 0)

With this idea in place, we can also talk about the payoff diagram of an underlying itself w.r.t itself. This is of course trivial, because a stock is a stock is a stock, so if we buy a stock, that stock’s value is just the value of the stock.

Another easy one is that for a zero-coupon bond. A zero-coupon bond always return the face value at ‘expiration’ (maturity). It is often convenient in the context of options to talk about about a zero-coupon bond with face value K and expiration T, i.e coinciding with that implicit in the forward contracts/options.

Let’s draw some pictures now. Since they are all basically plots of very simple functions as \mbox{Payoff} = f(S_T), I take it that you recall enough of drawing functions of the kind y = f(x) from your school days to not spend time explaining how you would draw these. Here we go.

Long Forward and Short Forward

Payoff Diagram - Long Forward (Baker) and Short Forward (Farmer)

Payoff Diagram – Long Forward (Baker) and Short Forward (Farmer)

Long Call and Short Call

Payoff Diagram - Long Call (Baker's choice) and Short Call (Baker's obligation)

Payoff Diagram – Long Call (Baker’s choice) and Short Call (Baker’s obligation)

Long Put and Short Put

Payoff Diagram - Long Put (Farmer's choice) and Short Put (Farmer's obligation)

Payoff Diagram – Long Put (Farmer’s choice) and Short Put (Farmer’s obligation)

Stock (underlying) and Zero-coupon Bond

Payoff Diagram - Stock (or Wheat or any underlying) and Zero-coupon bond (face value K with maturity T)

Payoff Diagram – Stock (or Wheat or any underlying) and Zero-coupon bond (face value K with maturity T)

Note that all upward sloping lines have a slope of +1 and downward sloping lines have a slope of -1 (why?).

Written by Vineet

October 28, 2016 at 7:07 pm

[PGP-I FM] Options as unbundled forwards

with 2 comments

Let’s go back to our baker-farmer story and consider what happens at the termination/expiration of the forward contract. At expiration, if the ‘spot’ price of the wheat has gone up it is the farmer’s loss and the baker’s gain (because she is delivering wheat at the contracted delivery price instead of the now-higher spot price, and the baker is the lucky bugger who is getting to buy wheat at the lower contracted price when the market price at the end of the period is higher). That is, a forward contract is a zero-sum game.

At the time of the settlement, one side will realize a gain at the expense of the other side. But if the point for both parties was to lock a price in advance, it is not a problem and both would not have too much of a problem resigning to their fate. After all, there will be times when farmer will gain at the expense of the baker.

Let’s quantify that gain/loss now. If the price of the wheat is S_T at the termination time T, and the forward contract delivery price was set at K, the baker’s payoff is S_T - K (the baker is happy when S_T > K) and the farmer’s payoff is K - S_T (the farmer is happy when K > S_T).

Options

While the zero-sum game and all that is fine, farmer nonetheless would like to have a choice to sell at the market price when the price of wheat has increased, and not necessarily feel obliged to sell at a lower price as she has to in a forward contract. Similarly, baker would also like to have a choice to buy the wheat directly from the market if the price of wheat has gone down.

Forward contracts being legally binding do not obviously serve this desire. Again, well-functioning markets provide such felt needs to be addressed.

This is how Options come into the picture. Options serve exactly this felt need of both the farmer and the baker. Options are products that provide farmer the choice to sell the wheat at the contracted price when it suits her interest- that is, exactly when the price has gone down (a “Put Option”), and the baker the choice to buy the wheat at the contracted price when it suits his interest – exactly when the price has gone up (a “Call Option”).

If you think about it an Option basically provides an ‘escape clause’ from the forward contract – it allows the farmer to escape when the price has gone up, and the baker to escape when the price has gone down. The natural question then is how do we make it work. If the farmer has to have a choice, then there has got to be some baker out there who is ready to buy from her. Similarly, if the baker has to have a choice and that choice has to be respected, then there has got to be some farmer out there to sell it to him. Otherwise the whole system kinda breaks down.

Put Option

Let’s consider a Put Option first. It gives the farmer the right to sell at the contracted price K if the price of wheat at expiration has gone down, i.e. when S_T < K. That is, her payoff from the Put Option is K - S_T > 0 – i.e. when the price of the wheat is lower it is in her interest to sell at the contracted price.

But when the price of the wheat has gone up, she is well within her rights not to use the ‘option’ and sell wheat at the prevailing higher market price (S_T > K), so there is no profit no loss – just the piece of paper with Put Option written on it which expires worthless. So her payoff then is simply the larger of K - S_T and 0, or simply \mbox{max}(K - S_T, 0).

Call Option

A Call Option provides baker the choice to buy at the contracted price K when the price of wheat has gone up. Again, the story works similarly. That is, his payoff from the Call Option is S_T - K > 0 – i.e. when the price of the wheat is higher it is in his interest to buy at the contracted price.

But, again, when the price of the wheat has gone down, he is well within his rights not to use the ‘option’ and buy the wheat at the prevailing lower market price  (S_T < K), so there is no profit no loss – just the piece of paper with Call Option written on it which expires worthless. So his payoff then is simply the larger of S_T - K and 0, or simply \mbox{max}(S_T - K, 0).

Jolly good things, aren’t they then, these Options? Keeping only give the upside while keeping the downside to zero. Well, yes. And like all good things in life, they also come with a price attached – that is they are not free. Both must pay a price to buy such Options. And who, pray, must they pay this price to?

For a farmer’s Put to work, and the baker’s Call to work, there has to be a baker to buy from the farmer when she uses her Put and there has to be a farmer when baker uses his Call. That is, for a farmer’s option to sell to be respected there must be a baker who is forced to buy. Similarly, for a baker’s choice to buy to be respected, there must be a farmer who is forced to sell. It should be clear now who these prices must be paid to. When a farmer buys a Put, she pays a price to a baker who is then forced to buy from her when she wants to sell at the contracted price K. Similarly, a baker would buy a Call from a farmer who is then forced to sell it to him when he wants to buy at K.

So now we have four sides. Farmer’s choice to sell is matched by the baker’s obligation to buy from her, and the baker’s choice to buy is matched by the farmer’s obligation to sell. Nice little trick isn’t it?

So what financial market participants have done is that they unbundled/broken-down a forward contract into two parts. A baker now either has a choice to buy (when he buys a Call) or is being forced to buy (when he has sold a Put). Similarly, a farmer either has a right to sell (when she buys a Put) or is being forced to sell (when she has sold a Call).

It is market parlance to speak of a baker’s forward contract to buy as Long Forward, a seller’s forward contract to sell as Short Forward, a baker’s choice to buy as Long Call, a farmer’s choice to sell as Long Put and so on.

That is for a baker we have,

\mbox{Always Buy (Long Forward) = Choice to Buy (Long Call) + Forced to Buy (Short Put)}

which mathematically is

S_T - K = \mbox{max}(S_T - K, 0) - \mbox{max}(K - S_T, 0)

and for a farmer we have:

\mbox{Always Sell (Short Forward) = Choice to Sell (Long Put) + Forced to Sell (Short Call)}

or mathematically,

K - S_T = \mbox{max}(K_T - S, 0) - \mbox{max}(S_T - K, 0)

It is also useful to ‘draw’ the diagram of their payoffs as the spot price at termination S_T varies. These are very useful and handy, but this we leave it for the next post.

Written by Vineet

October 28, 2016 at 7:01 pm