Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Optimization of a penalty function in Stata

    Dear Stata users,

    for some time now I have been trying to solve an optimization problem via Stata/SE 13.0 on Windows 7. As I was not yet successful, I am now asking you for help. The following variables from my dataset are relevant in this context:

    Ticker: unique identifier for companies in the dataset (e.g. "AAPL" for Apple Inc.)
    Date: date of observation (Stata date format)
    Spread: credit default swap spread of company observed on a particular date (contains values from 0 to 1, mean: 0.023, median: 0.008)
    Rating: numerical average rating of company observed on a particular date (from Moody's, S&P, Fitch: 1 = AAA, 2 = AA, ..., 8 = CC, 9 = C)

    With this data I want to generate market implied ratings under a new variable called Implied_Rating. This is to be done by means of minimizing a linear penalty function under the variable Penalty which penalizes transgressions of the Spread over certain boundaries (b1, b2, ..., b8) which serve as variables in the penalty function. For better understanding, let us e.g. say that the optimization process yields b1 = 0.001 and b2 = 0.002. If an observation had Spread = 0.0015, this would fall between b1 and b2, yielding an implied rating of AA (b1 is boundary for AAA/AA and b2 for AA/A) which will be displayed as Implied_Rating = 2. However, let us assume that the corresponding agency rating observation is A, displayed as Rating = 3. Then the penalty function would yield Penalty = b2 - Spread = 0.002 - 0.0015 = 0.0005. What I want to do is to optimize the boundary variables b1, b2, ... b8 in such a way that sum(Penalty) is minimized. The resulting boundaries can then be used to determine the Implied_Rating variable for each observation.

    New variables for the optimization are b1, b2, ..., b8 as well as Penalty which I would program as follows:
    gen Penalty = cond(Spread>b1, Spread-b1, 0)
    forvalues i = 2/8 {
    replace Penalty = cond(Spread<b`i-1', b`i-1'-Spread, cond(Spread>b`i', Spread-b`i', 0)) if Rating == `i'
    }
    replace Penalty = cond(Spread<b8, b8-Spread, 0) if Rating == 9


    I would program Implied_Rating as:
    gen Implied_Rating = .
    replace Implied_Rating = 1 if (Spread <= b1)
    replace Implied_Rating = 2 if (Spread > b1) & (Spread <= b2)
    (...)
    replace Implied_Rating = 8 if (Spread > b7) & (Spread <= b8)
    replace Implied_Rating = 9 if (Spread > b8)


    Finally my question to you is: How can I achieve such an optimization via Stata? I worked through optimize and moptimize commands, but cannot yet find a connection.

    Many thanks to you in advance for providing answers!

    Sincerely, Timo
Working...
X