Saturday, May 23, 2015

r - Portfolio Optimization - Zero beta portfolio


I am trying to solve a optimization portfolio in R in which I do the following constraints:




  • Set weight sum to within a boundary

  • Set return to a certain value

  • Set portfolio beta to 0


The purpose is then to minimize risk subject to the constraints above.


While I have no trouble in doing that my issue comes next. I want to make the weights such that the portfolio's total exposure to a factor is 0. Imagine asset a has a beta of 0.3, asset b has 0.7 and asset c -0.3. How can I set this constraint? My issue is that using quadprog I can only add an external parameter vector(mean returns in this case). Is there a way to go around this issue or am i seeing thing s the wrong way?


The code I have so far is as follows :


6 assets and six mean returns. Also, a 6*6 var-cov matrix. The upper weight bound is lev_ub and the lower bound is lev_lb. Returns_full is the vector with returns.


dvec = matrix(colMeans(returns_full),ncol = 1)
Dmat = cov(returns_full)

A.Constraint1 <- matrix(c(1,1,1,1,1,1), ncol=1)
A.Constraint2 = matrix(c(1,1,1,1,1,1), ncol=1)
Amat <- cbind(A.Equality1,A.Equality2, dvec)
bvec <- c(-lev_ub,lev_lb,target_return)
qp <- solve.QP(Dmat, dvec, Amat, bvec, meq=0)

My issue comes now from the fact that assume I have another vector of length 6 with one beta for each asset. I want to make it such that the sum of the product of weights and betas is zero or,in other words, set the total portfolio beta to 0. How can I add this constraint ?




commodities - How to de-seasonalize natural gas term structure data?


I need to de-seasonalize Nat Gas futures data for a project and am hoping to get good suggestions. As we all know natural gas futures are priced higher for the winter months and to analyze/model the term structure we need to de-seasonalize the data.


Any ideas how would one do it?




Font Organisation


I am trying to organise my fonts so when I am in Illustrator or PS that the fonts are as one single font.



Not one font for bold, one for italic etc. - Like this:


enter image description here


Rather it should be in sub-divisions, like this:


enter image description here


For example there are some fonts you install that install as a singular font. So when I scroll looking at fonts it takes an eternity to get through them all as some have so many different variants or weights.


But I need to just click on [desired font] and then choose the variant as when I am scrolling through fonts, it takes forever just to move past one font as sometimes they have 20-30 variants not installed as a group/family.




adobe illustrator - How do I get my vector art to have crisp lines on instagram?


As soon as I upload my art to Instagram, the quality decreases. I have tried everything I can think of. I have used 1080x1080 image resolution, and larger, exported it in many different files (JPG, EPS, SVG). The quality never changes and always ends up the same.


If anyone knows how to fix this problem, it would be much appreciated!


enter image description here




How do you check your option calculations?


I'm implementing a bunch of different algorithms to price options/find Greeks: finite difference, Monte Carlo, binomial...


I'm not really sure how to check my calculations. I tried using QuantLib to price things for me, but it seems to use actual dates a lot (whereas I'm just interested in year fractions) and the documentation is lacking.


I implemented a finite difference algorithm as described in Wilmott's "Mathematics of Financial Derivatives" and he has some numbers in his book. But my "implementation" of just the analytical Black-Scholes formula already gives different results than his (not by much though).


Again, I just typed up the down and out call option formula from Zhang's Exotic options. He actually goes through explicit examples for each of his formulas.


But for a down and out call with $S = 100$, $K= 92$, $H = 95$, $r = 0.08$, $q = 0.03$, $\sigma = 0.2$, $\tau = 0.5$ he gets \$6.936 and I get \$6.908.


So my question is, what is your go to reference for option prices for checking your code?



Answer



1: Follow the calculations in The Complete Guide to Option Pricing Formulas. The book has many formulas, sample values and outputs. Highly recommended for validating your results. Apparently, this is one of most popular books used by real-world quants (simple and fast).


2: You can still use QuantLib to price with year fractions. I have an example:



DayCounter dc = Actual360();
Date today = Date::todaysDate();
Date exerciseDate = today + 90;
assert(dc.yearFraction(today, exerciseDate) == 0.25);

Here, we make the exercise date exactly 0.25 year fraction away from today (pricing date). Anything from QuantLib using the dates should match your own implementation. You can adjust the exerciseDate, print the yearFraction and use it in your own code.


3: Use fOptions. fOptions and it's related fExoticOptions are R-packages. They implement the most commonly quantitative finance models. For example, I use the following script to validate my Levy Asian options:


LevyAsianApproxOption(TypeFlag='c', S=6.80, X=6.90, SA=6.80, r=0.07, b=-0.02, sigma=0.14, Time=0.50, time=0.50)


4: Use OptionMatrix. OptionMatirx is a finance calculator runs on a desktop computer.


American Options relation between greeks


Considering an American option in a Black-Scholes model, is there a relation between Vega and Gamma as it holds in the European case?


I am aware an exact relation would be difficult to find. But in practice is there a kind of approximate relation between these two quantities?



Answer




No, you should not expect such a relationship to hold in general. The reason is that American options have an "exercise barrier" which European options don't, and this results in different prices and greeks.


In the case of put options (with interest rate $r>0$) as the spot price falls, at some point it becomes optimal to exercise early and take the cash. Beyond this point, the option behaves like a short position in the stock, so its delta is exactly -1 and its gamma is zero. As the spot crosses through this barrier, the gamma jumps.


Vega, on the other hand, does not have such a discontinuity. The chart below shows the price of a European and American put with strike 100, three months to maturity, risk-free rate 15% and volatility 20%. The European prices and greeks are from the Black-Scholes model, and the American prices and greeks are from a binomial tree.


enter image description here


Note that when the spot price is sufficiently far from the early exercise barrier, it is unlikely to cross it, and the option behaves like a European. In this case you expect the standard relationship between gamma and vega,


$$\nu = \sigma\tau S^2 \Gamma$$


will approximately hold. The plot below shows how this relationship holds when the spot price is sufficiently far above the strike, but breaks down when it approaches the early exercise barrier.


enter image description here


Friday, May 22, 2015

inkscape - Copy, paste, move colored squares - but keep them aligned to the grid


I am developing a word game for iOS and would like to use an SVG file as the gaming board.


The SVG file (author: Denelson83, Wikimedia Commons) is almost perfect for my game - except for displaced colored squares.


My question is:


How could I please copy such a colored square (is it a Group?), then paste it and move the new square to a new spot - but still keep it aligned to the grid?


screenshot


As an Inkscape newbie I have already found out, that I can jump to the "original" with Shift-D.


And I can hold CTRL key to move objects horizontally or vertically.


But I still don't understand how to keep the moved square aligned to the grid (which also seems to be in a separate layer - so how did the author manage to keep all squares aligned to the grid?)


And also I can't find some view/dialog/toolbar, which would show me the coordinates of the object being dragged by me (I only see the coordinates of the mouse pointer at the bottom right). If I could see/change those coordinates, then I could (in a tiresome way) calculate and set the coordinates for the new squares...



UPDATE:


I've toggled Snap to grid and also enabled showing the grid:


grid


but the square doesn't snap to the grid when I drag it.


And also the grid is too small and I can't find how to make its cells broader.


UPDATE 2:


I have found where to set the grid size (15 x 15) and offset:


properties


But while the grid looks ok now and snaps too - it snaps in the wrong places as shown below.


snapping



Why doesn't the grid snap at the square's borders?


Is this maybe a bug in Inkscape? How to workaround it please?


UPDATE 3: I've received an offline advice to hold ALT while dragging a square



Answer



In order to snap the whole group, you need to enable "Snap bounding box corners", by clicking on the proper icon in snap toolbar:


Snap bounding box corners


Usually this flag isn't enabled and you see the snap to text baseline:


Default snap to text


Enabling this flag you can see the snap to the bounding box of the group (or the selection);


Snap to bounding box corners



You can change the behaviour of the snap using the other buttons in the toolbar. See here for reference.


Be aware that the snap depends from the position of your mouse in the selection: if you begin to drag the group with the mouse near the text, the snap would be related to the text, if your mouse is near the top-left corner of the box, the snap would be related to the top-left corner of the box (if "Snap bounding box corner" is enabled), and so on.


technique - How credible is wikipedia?

I understand that this question relates more to wikipedia than it does writing but... If I was going to use wikipedia for a source for a res...