Category Archives: Finance

A new challenge has entered the ring!

My first social life in the first 6 months of year 2012 has been compromised by a new challenge I decided to take on this year, the Chartered Financial Analyst (CFA) curriculum.

The CFA program is divided in 3 exams denoted Level I, II and III. The candidate gets the CFA designation upon completing the 3 levels which can be taken every year. I am hence taking the Level I in June.

The reason why I decided to take on this challenge is because I believe it contains interesting general finance topic and covers some of the basic concepts of quantitative finance. Let’s face it, it’s also good value to my CV if I can manage to pull it off.

I will try from time to time to put some summaries of what  I have encountered, in order to help me remember the formulas and to share with the community the key concepts.

I must add that I will use the resources provided by Kaplan Schweser which are from what I have seen so far a must-have in order to complete this curriculum successfully, especially if you do have a full position at the same time.

I’ll try to soon add my summary of the quantitative finance part.

Functional approach to portfolio modeling

Good evening everybody, I’ve been paying attention to portfolio modelling for the past few months. When you tackle such problem, you first try to think about how you could represent a portfolio as an object so that you can dive into your C#/C++/Java code so that you can start making money ASAP. However, you’ll soon find yourself cornered in numerous problems, especially when you want to backtest different allocation strategies.

The object-oriented approach

Usually, when people model a portfolio, they will see it as a mapping between assets and weights associated to a date. There is however a misinterpretation of what a portfolio is. Indeed, what the common programmer describer above in his model is in fact a snapshot of the portfolio stat at some time t. If you were to make some changes in between two dates (all the subsequent instances of the portfolio will then be erroneous and the programmer would have to recompute them all to get the right simulation. Let’s take an example. Say we have a portfolio going through time t=1,2,3. We assume the stock has two assets, Microsoft (MSFT) and Yahoo (YHOO), and that the allocation strategy is to have an equally weighted portfolio (weights={0.5,0.5}). Here’s how the implementation would look like (in C#):

class Portfolio
{
     public DateTime date;
     public Dictionary<Asset,double> allocation;

     public Portfolio() {}

     public void Optimize()
     {
          int n=allocation.Count;
          foreach (var pair in allocation)
          {
                pair.Value=1/n;
          }
      }
}

class History
{
    public Dictionary<DateTime,Portfolio> history;

    public Add(DateTime t, Portfolio p)
    {
         history.Add(t,p);
    }
}

Now assume you want to add another stock (STO) to the portfolio at time 2, the previous implementation needs to be extended as follows:

class History
{
    public Dictionary<DateTime,Portfolio> history;

    public Add(DateTime t, Portfolio p)
    {
         history.Add(t,p);
         if (history.Any(x=>x.Key>=t))
         {
              /* Compare the new portfolio composition with the subsequent states
               * Take the necessary operation to adjust the portfolio.
               * OUCH!!!! This is complicated.
              */
         }
    }
}

As you can see in the comments I added, backward changes requires recomputing the portfolio at time 2 and 3. This is computationally intensive and the kind of function you really do not fancy writing. I’m not even discussing the probability that some bug will exist or the change that you would have to add if you were to make more complicated backward operations. Furthermore, assuming you want to see how the portfolio behave before a change, all the information about the previous simulation would be lost. This is because the class actually stores the stateof the portfolio, not really the portfolio itself. “Thanks for the heads-up Einstein! You got anything better to do?” Well, as a matter of fact, yes.

The functional approach

I would like to introduce a different way of representing a portfolio; a way which would be especially meaningful in a functional environment (F#, Scala). First of all, I would like to define a portfolio snapshot as a list of tuples of an Asset and a double representing each asset and its weight. To me, a portfolio is a strategy more than an object. In terms of allocation, the strategy outputs portfolio snapshots with weights, and these weights can actually generate buy/sell order to adjust a “real” portfolio (a basket of real assets) position. But the whole point here is that the portfolio is in fact just as set of operations. In my opinion, the correct way of representing an operation in a programming language is a function. In our case, an operation would be a function. This includes adding an asset to the portfolio, optimizing a portfolio and so on. The portfolio is hence a list of tuples of type DateTime*Operation (the date being the time at which the operation should occur). Let’s just define some formal definitions to these concepts (in F#):

type Action<'a>='a->'a

type Change<'a> = System.DateTime * Action<'a>;

type History<'a>; = Change<'a> list

An action is a function taking some type as an input and return a modified version of this object (actually, a new instance of the object with the modification included). A changeis an action happening at a certain time. Finally, a history is a list of changes. Simple. Now, how do we apply this to portfolio modeling? First, some more definitions:

type Weight=float

type Asset = string

type AssetAlloc=Weight * Asset

type PortfolioAlloc= AssetAlloc list

type PortfolioAction=Action<PortfolioAlloc>

type PortfolioChange = Change<PortfolioAlloc>

type Portfolio = History<PortfolioAlloc>

Thanks to the F# syntax, the code is pretty much self-explanatory. Now, let’s define a simple portfolio action consisting in adding an asset to the portfolio:

let addAsset (ass:Asset) (w:Weight) (pFolioAlloc: PortfolioAlloc) : PortfolioAlloc =
    match List.tryFind (fun p -> snd p = ass) pFolioAlloc with
    |Some _ -> (w,ass) :: pFolioAlloc |> List.filter (fun pa -> snd pa <> ass)
    |None -> (w,ass) :: pFolioAlloc

For those of you not familiar with functional programming, this might look complicated, but if you look a bit into the language (particularly Pattern Matching), you’ll see it’s actually quite trivial. Let’s continue with the optimization of the portfolio:

let equWeightPortfolio (pFAlloc:PortfolioAlloc) : PortfolioAlloc =
    let w:Weight = 1.0/(float pFAlloc.Length)
    pFAlloc |> List.map (fun alloc -> (w, snd alloc))

Trivial. Finally, we need a function that will evaluate the portfolio. This requires the application in succession of all the changes to an initial portfolio allocation (most of the time, an empty portfolio, initially). This kind of operation is well-known in functional programming, it simply consists in foldinga list:

let getPortfolioAllocFromInit (pFolio:Portfolio) (t : System.DateTime) (init:PortfolioAlloc) =
    pFolio |> List.filter (fun pc -> fst pc <= t)
    |> List.sortBy (fun pc -> fst pc)
    |> List.map (fun pc -> snd pc)
    |> List.fold (fun alloc action -> action(alloc)) init

let getPortfolioAlloc (pFolio:Portfolio) (t : System.DateTime) = getPortfolioAllocFromInit pFolio t []

The first function implements the general logic: first sorting the operations and then applying them sequentially. The second function just modifies the first one by giving an initial empty portfolio. The application of this model is as follows:

let addSPAction : PortfolioAction = addAsset "S&P500" 0.0;;
let addMSAction : PortfolioAction = addAsset "MSFT" 0.0;;
let myPortfolio:Portfolio = [(System.DateTime.Parse("01.01.2011"),addSPAction);
                            (System.DateTime.Parse("31.01.2011"),equWeightPortfolio);
                            (System.DateTime.Parse("01.02.2011"),addMSAction);
                            (System.DateTime.Parse("28.02.2011"),equWeightPortfolio);
                            ];;
let myPortfolioAlloc t = getPortfolioAlloc myPortfolio t;

let endAlloc = myPortfolioAlloc System.DateTime.Now;;

let janAlloc = myPortfolioAlloc(System.DateTime.Parse("01.02.2011"));;

which outputs:

val endAlloc : PortfolioAlloc = [(0.5, "MSFT"); (0.5, "S&P500")]
val janAlloc : PortfolioAlloc = [(0.0, "MSFT"); (1.0, "S&P500")]

When hence see how trivial it is to compute the state of the portfolio at different times. With this representation, altering a portfolio at time t=2 means actually adding a function to a list, but does not change the subsequent operations. The state of the portfolio (the snapshot) is computed on demand. Let’s try doing so:

let addYHOOAction:PortfolioAction = addAsset "YHOO" 0.0
let myPortfolio2:Portfolio= (System.DateTime.Parse("30.01.2011"),addYHOOAction)::myPortfolio
let myPortfolioAlloc2 t = getPortfolioAlloc myPortfolio2 t
let endAlloc2 = myPortfolioAlloc2 System.DateTime.Now
let janAlloc2 = myPortfolioAlloc2(System.DateTime.Parse("01.02.2011"));;

which outputs:

val endAlloc2 : PortfolioAlloc =
  [(0.3333333333, "MSFT"); (0.3333333333, "YHOO"); (0.3333333333, "S&P500")]
val janAlloc2 : PortfolioAlloc =
  [(0.0, "MSFT"); (0.5, "YHOO"); (0.5, "S&P500")]

As you can see, no rocket science to add backward operations! Note that we could have done so for any portfolio action… The model could be improved of course, but the idea is here. Keeping track of the old simulation would just mean keeping the date of creation of the change. I hope you enjoyed the ride, please feel free to comment! See you next time!

Resources for Financial Engineers


Warning: DOMDocument::loadXML(): Space required after the Public Identifier in Entity, line: 1 in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 149

Warning: DOMDocument::loadXML(): SystemLiteral " or ' expected in Entity, line: 1 in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 149

Warning: DOMDocument::loadXML(): SYSTEM or PUBLIC, the URI is missing in Entity, line: 1 in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 149

Warning: Invalid argument supplied for foreach() in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 160

Hi everyone,

I’ve been busy recently cooking up a new article which I hope I’ll be posting soon called “Bread and Butter of Risk-Neutral pricing”, that’s why I’ve been quite silent the past few weeks.

Anyway, I wanted to share with you my latest “findings” about literature and resources I recently bought or discovered on the internet.

First of all, let’s talk a bit about Paul Wilmott and the different tools he provides to the quantitative finance community. If you are at the moment working in finance and you’re getting bullied by the quants of your desk because of your lack of knowledge in mathematical theory, you might want to have a look at the CQF Certificate he created recently. I actually thought about taking it myself and I think it’s particularly suited for professional thanks to its flexible structure. What I’d like to be talking about today is the Wilmott magazine and the Wilmott journal you can subscribe to on his website.

The Wilmott magazine is, in my opinion, very interesting in the sense that the articles he contains are not qualitative, yet very interesting. This comes from the fact that their authors are very famous in the quantitative finance community. For example, Ed Thorp whom I talked about in this post, contributes to the magazine along with Alan Brown for example. The format and the quality of the magazine is very impressive as you can see in the image below.

The wilmott magazine

As you can see, it has a “squared” format, and is filled with nice illustration and a nice layout.

The Wilmott journal is, on the contrary, much more quantitative. Indeed, it is purely a research journal containing scientific articles on different topics. For example, this year’s first edition has one article on the SABR models, one article on learning processes and one article on portfolio optimization. I think this is quite interesting for somebody who wants to be reading about new researches but who’s not focused on a single field. The format of the journal, particularly handy, is displayed on the image below. The quality of the paper itself is also very good.

The Wilmott journal.

 

The pricing of these to periodicals is available here; it’s about 250 euros per year and includes six issues of each product.

For those of you who are seeking a job or who are from time to time asked to interview some candidates, I would particularly recommend Wilmott’s book “Frequently asked Questions in Quantitative Finance”.  It is particularly good because it provides both short answers (more qualitative) and long answers (with mathematical developments) to very common question. It can in my opinion be used as a reference to some extent, since it also contains very handy reminders of the different probability distributions and their main characteristics and properties.

Frequently Asked Questions in Quantitative Finance

Finally, I lately had the occasion to use a great website part of the stackexchange group focused on quantitative finance. The idea of this website is to post questions and answers on the subject and I have to say it is very interesting because of the diversity of the contributors. Indeed, you have a mixture of students and professional which enables you to follow some topics for you personal interest as well as helping someone with your knowledge. The adress of the website is http://quant.stackexchange.com/, and I invite you all to check it out, as well as the different site of the same form provided by http://stackexchange.com/, since it covers also mathematics, programming, hardware, LaTeX, and so on.

On that note, I wish you a nice week, and I’ll post again soon!

Quantitative Finance and Computer Science: quick comparison between R and MATLAB


Warning: DOMDocument::loadXML(): Space required after the Public Identifier in Entity, line: 1 in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 149

Warning: DOMDocument::loadXML(): SystemLiteral " or ' expected in Entity, line: 1 in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 149

Warning: DOMDocument::loadXML(): SYSTEM or PUBLIC, the URI is missing in Entity, line: 1 in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 149

Warning: Invalid argument supplied for foreach() in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 160

Warning: DOMDocument::loadXML(): Space required after the Public Identifier in Entity, line: 1 in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 149

Warning: DOMDocument::loadXML(): SystemLiteral " or ' expected in Entity, line: 1 in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 149

Warning: DOMDocument::loadXML(): SYSTEM or PUBLIC, the URI is missing in Entity, line: 1 in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 149

Warning: Invalid argument supplied for foreach() in /home/clients/d6bcc13f01d956648e26ee7f6a76facd/blog/wp-content/plugins/wordpress-amazon-associate/APaPi/AmazonProduct/Result.php on line 160

Hi everybody,

It’s a few weeks without a post and I apologize for that; I am at the moment looking for a job in Geneva, and it’s a bit time consuming as you would imagine.

Anyway, I kept working a bit on different projects and I had the idea to create this post. What encouraged me was several e-mails I received from some former classmates and some questions I recently had to ask. I noticed that, most of us, “quants”, “analysts” or whatever you call your job, are usually wondering what technology to use for in specific situations.

Most of the time, we end up using programming languages we know best. For example, students used to MATLAB will use it instead of R, programmers used to .Net (C#, Visual Basic) will use it instead of the JVM (Java). If you have next to you a whole desk of hugely experimented people, then you might have good hints to guide you in your technology decisions, but here are a few remarks I gathered during my work.

MATLAB vs R

First of all, R is free and open-source as opposed to MATLAB which has an initial fee, plus additional cost for each additional package.

R being open-source, you can browse the Comprehensive R Archive Network (CRAN) to fit the packages you like and start using them. However, you will sometimes find that the documentation is not very clear and hence you might have to ask several questions on the R mailing lists which are not very user-friendly themselves.

Using MATLAB however, you will get packages about different subject (such as Fixed Income, Derivatives, Data Handling and so on) which are quite complete and have a pretty good documentation. I’d say that in term of support, it’s money well spent.

If you wish to integrate this technology within one of your software, I’ve heard (but not tried) that integration is pretty easy. This might allow you to spare some time you’d have spent on handling data marshaling or library implementation in the native language.

As far as R is concerned you will find some very useful packages such as XTS for time series handling. You will also be able to find handy financial packages to perform computations on bond pricing. However, you might have some problems finding the package that suits your need. There is a trade-off between the number of packages available and their quality. However, once you’ve found what you need, you’ll be pretty happy. In terms of integration, I have to tackle the problem myself, and I’d say that Dirk Eddelbuettel’s blog and libraries will help you a lot. If you wish to use R withing Excel, you can use RExcel from Statcon which is pretty easy to work. The following book might help you handle:

R Through Excel: A Spreadsheet Interface for Statistics, Data Analysis, and Graphics (Use R)

For those of you who wish to dig deep into R foundations, the following book is excellent:

Software for Data Analysis: Programming with R (Statistics and Computing)

In terms of performance, I’d say that MATLAB seems to be quicker to find optimization results. Again, this might be due to the fact that I did not find the right R package, but who cares? After my different researches, and especially for the game-theoretic approach on ice hockey penatlies, I had to use MATLAB to get the program to converge before loosing patience.

That’s all from now, but I’ll soon be back to compare the JVM and the .Net framework for finance computations and MATLAB or R integrations.

Until then,

Have fun!