Leodongxu.GitHub.io

View My GitHub Profile

welcome

This is the brief introduction about Leo Li.(final update 11/1/2016)

Portfolio simulation for Risk Management

A key tool in portfolio risk management is making forecasts of the portfolio's PnL distribution at some horizon. A typical horizon is one day, but it could be longer. Forecasting the PnL distribution typically is done by first creating a market model of the factors that drive changes in portfolio value. Generating a large number of market scenarios gives a distribution of PnL.

In this project, students build a Monte Carlo simulation of financial markets relevant to a client portfolio. They use this simulation to analyze the forecasted distribution of PnL. The simulation is based on a method used by MSCI/RiskMetrics, originally described in A general approach to calculating VaR without volatilities and correlations (1997), and again in Monte Carlo Simulation using the Benson-Zangari Approach (2013).

The steps involved can be summarized as:

1.Determine which market factors (e.g. prices, exchange rates, interest rates, implied volatilities, etc) drive portfolio value.

2.Find market factor level data (typically daily close). To start, can use Yahoo Finance. Define a file format, which might look something like this. Note that the example file uses YYYYMMDD format, which you can generate in Excel via a custom date format.

3.Create a C++ data model for market factor data. For example, you might have a Market class, consisting of a map of named MarketFactor objects.

4.Read your CSV market data from a stream and populate MarketFactor objects that are added to Market. Since log returns never change, these could be computed and stored as well while you load prices into your MarketFactor. You can also capture the associated dates, taking advantage of the boost date_time library.

5.You should be able to retrieve a MarketFactor from the Market object by name, using a std::map.

6.Create a scenario generating object (e.g. MarketSimulation). You shoud be able to ask a MarketFactor object for its return given the MarketSimulation. Now you have a working simulation of the market!

7.Now we need to see the PnL impact of the scenario on our portfolio. That means we can create a Portfolio, which contains Positions. Each Position is based on a some amount of a Security. Initially, we can assume our Security is a stock.

Resume

2015 ICM Paper

cs project

I am currently participating CS50 course, which give me intense training in programming and help me develop computational thinking. And I keep an update of my progress.

Week1 Greedy

Implement a program that calculates the minimum number of coins required to give a user change. $ ./greedy hai! How much change is owed? 0.41 4

Background Coin Changer Toy

When using a device like this, odds are you want to minimize the number of coins you’re dispensing for each customer, lest you have to press levers more times than are necessary. Fortunately, computer science has given cashiers everywhere ways to minimize numbers of coins due: greedy algorithms. According to the National Institute of Standards and Technology (NIST), a greedy algorithm is one "that always takes the best immediate, or local, solution while finding an answer. Greedy algorithms find the overall, or globally, optimal solution for some optimization problems, but may find less-than-optimal solutions for some instances of other problems." What’s all that mean? Well, suppose that a cashier owes a customer some change and on that cashier’s belt are levers that dispense quarters, dimes, nickels, and pennies. Solving this "problem" requires one or more presses of one or more levers. Think of a "greedy" cashier as one who wants to take, with each press, the biggest bite out of this problem as possible. For instance, if some customer is owed 41¢, the biggest first (i.e., best immediate, or local) bite that can be taken is 25¢. (That bite is "best" inasmuch as it gets us closer to 0¢ faster than any other coin would.) Note that a bite of this size would whittle what was a 41¢ problem down to a 16¢ problem, since 41 - 25 = 16. That is, the remainder is a similar but smaller problem. Needless to say, another 25¢ bite would be too big (assuming the cashier prefers not to lose money), and so our greedy cashier would move on to a bite of size 10¢, leaving him or her with a 6¢ problem. At that point, greed calls for one 5¢ bite followed by one 1¢ bite, at which point the problem is solved. The customer receives one quarter, one dime, one nickel, and one penny: four coins in total. It turns out that this greedy approach (i.e., algorithm) is not only locally optimal but also globally so for America’s currency (and also the European Union’s). That is, so long as a cashier has enough of each coin, this largest-to-smallest approach will yield the fewest coins possible. How few? Well, you tell us!) When using a device like this, odds are you want to minimize the number of coins you’re dispensing for each customer, lest you have to press levers more times than are necessary. Fortunately, computer science has given cashiers everywhere ways to minimize numbers of coins due: greedy algorithms.

According to the National Institute of Standards and Technology (NIST), a greedy algorithm is one "that always takes the best immediate, or local, solution while finding an answer. Greedy algorithms find the overall, or globally, optimal solution for some optimization problems, but may find less-than-optimal solutions for some instances of other problems."

What’s all that mean? Well, suppose that a cashier owes a customer some change and on that cashier’s belt are levers that dispense quarters, dimes, nickels, and pennies. Solving this "problem" requires one or more presses of one or more levers. Think of a "greedy" cashier as one who wants to take, with each press, the biggest bite out of this problem as possible. For instance, if some customer is owed 41¢, the biggest first (i.e., best immediate, or local) bite that can be taken is 25¢. (That bite is "best" inasmuch as it gets us closer to 0¢ faster than any other coin would.) Note that a bite of this size would whittle what was a 41¢ problem down to a 16¢ problem, since 41 - 25 = 16. That is, the remainder is a similar but smaller problem. Needless to say, another 25¢ bite would be too big (assuming the cashier prefers not to lose money), and so our greedy cashier would move on to a bite of size 10¢, leaving him or her with a 6¢ problem. At that point, greed calls for one 5¢ bite followed by one 1¢ bite, at which point the problem is solved. The customer receives one quarter, one dime, one nickel, and one penny: four coins in total.

It turns out that this greedy approach (i.e., algorithm) is not only locally optimal but also globally so for America’s currency (and also the European Union’s). That is, so long as a cashier has enough of each coin, this largest-to-smallest approach will yield the fewest coins possible. How few? Well, you tell us!

#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
{   
    float x;
    do{
        printf("O hai! How much change is owed\n");
        x = GetFloat();
    }while(x<0);
    x = round(x*100)/100;
    int y = round(100*x);
    int count = 0;
    count = count + y/25;
    y = y%25;
    count = count + y/10;
    y = y%10;
    count = count + y/5;
    y = y%5;
    count = count + y/1;
    printf("%i\n",count);
}

Week2 Vigenère

Background

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[])
{
    if (argc !=2 ) 
    {
        printf("Usage: ./caesar k \n");
        return 1;
    }

    int n = strlen(argv[1]);
    int count;
    string key = argv[1];

    for(int i=0; i<n ;i++)
    {
        if (isalpha(key[i]) == false)
        {
            break;
        }
        count = i;
    }

    if(count != n-1) 
    {
        printf("Usage: ./caesar k \n");
        return 1;  
    }

    printf("plaintext:   ");
    string p = get_string();
    printf("ciphertext:  ");
    int j = 0;
    for(int i=0;i<strlen(p);i++)
    {
        if isalpha(p[i])
        {
            if isupper(key[j%n])
            {
                if isupper(p[i])
                {
                    p[i] = (p[i] - 65 + key[j%n] - 65)%26 + 65;
                }
                if islower(p[i])
                {
                    p[i] = (p[i] - 97 + key[j%n] - 65)%26 + 97;
                }
            }
            if islower(key[j%n])
            {
                if isupper(p[i])
                {
                    p[i] = (p[i] - 65 + key[j%n] - 97)%26 + 65;
                }
                if islower(p[i])
                {
                    p[i] = (p[i] - 97 + key[j%n] - 97)%26 + 97;
                }
            }
            j = j + 1;
        }
        printf("%c",p[i]);
    }
    printf("\n");
}

About me

I am now a student in the Quantitative Finance and Risk Management Program at the university of Michigan. And I am actively seeking for an intern position.