i think that carloscerro is right.
The valuation-test seems to rely on an unchanged input variable.
please check the following code. It will fail for the first basic test (One toggle), but will succeed if you out-commend the lines 2 and 3:
if 0 < len(puzzle):
puzzle = (puzzle + 1) % 2
return [(1, 1)]
thanks for the feedback.
In fact, uttumuttu did a good job and seems to be very clever in general.
This kind of approach ( using past or self-defined relative shifts for the risk-factors ) is not only used to calculate Vaule-at-Risk, but also other by risk-measures like "expected shortfall", or scenarios ( e.g. black friday ) or sensitivities ( checking how much win/loss e.g. a yield shift of 1% would induce ).
@uttumuttu's second suggestion to describe VAR was helfpul for me: "The simplest explanation (of VAR) is that each equity's total shifts are obtained as the relative shifts of its currency-converted price series." I enjoyed the kata because it not only allowed me to practice Python but also to learn about VAR.
Sorry have written, before thinking:
VAR is not sub-additive in both dimensions ( over Risk-Classes and over sub-Positions ).
Will modify th desciption accordingly.
thanks a lot for your input.
Since i am very new to writing kata, i do need some help indeed.
It's in fact a thin line between spoiling too much of the algorythm/implementation or potentially frustrate someone trying to solve it.
i don't know, if that is common practise, but feel free to apply some your suggestions to the description, as well.
I added the following section ( it's near the end ), but i do get the feeling, that the whole kata description may be too long now ( distracting interested people ) for such a relative simple calculation :
We had no Forex risk involved in this example, since the equity was listed in your base-currency.
Please ensure that the shifts of the foreign currency are taken into account, as well, when calculating the 'Total'-VAR for a foreign-currency-equity-positions.
You then might notice some kind of strange behaviour of your VAR-results:
VAR is not additive !
total VAR = equity VAR + currency VAR
VAR(EQ_all) = VAR(EQ1) + VAR(EQ2)
for an Equity position EQ_all that consists fo Equity position 1 ( EQ1 ) + Equity position 2 ( EQ2 )
In fact, the VAR for a given position might be lower or equal than the sum of the VAR of it's parts.
When looking at the different Riskclasses, it's even 'worse': The total-VAR can be lower, equal or bigger than the sum of equity-VAR and currency-VAR.
Keep in mind, that the Value-at-Risk measurement is based on the potential top losses (relative shifts) only. And these top losses ( for individual equities or riskclasses ) may have occurred on different historical days ( for the equities/riskclasses ) involved.
Two suggestions related to the issues faced by Reswin, aimed to make the kata more accessible to people without a quantitative finance background:
The non-additive nature of VAR could be incorporated into the description. That is, the reader should understand that they are to first construct the daily gain-loss distribution summed across equities, and only then calculate the quantile.
Calculation of total (equity plus currency) shifts could be described more clearly. The simplest explanation is that each equity's total shifts are obtained as the relative shifts of its currency-converted price series. An even simpler alternative is to simply spell out the geometric compounding formula (or, less spoilerly, the equations leading to it), which is equivalent but easier to code on top of existing equity and currency shifts calculated by get_shift).
thanks for the hint.
i did add the provided and the expected results for teh large randomized tests.
this was very helpful, thank you. My solution now passes all the description related tests and most of the random larger tests. The output I am getting for the failed tests is "False should equal True", which does not help me in finding my error(s). Would it be possible to change this to something like: "get_var is returning incorrect value: 96.02 should equal 104.89"?
VAR is not additive ( not over the different assets, nor for the different risk-classes).
You cannot calculate the VAR for one Equity(-position) and then add another.
You have to calculate the potential, relative shifts (based on the history) for your whole position, and then (sort and) calculate the VAR for the whole position.
(i marked the first comment as a spoiler. That's why it is "hidden")
I could solve my problem with rounding: I rounded past relative shifts to 6 digits because you wrote you rounded them to 6 digits (for better readability). I can pass the tests if I don't round.
I am still struggling with how VAR is calculated, except for the simple cases where only one equity is involved.
@Marty3000: I cannot see your comment, you seem to have hidden it.
Thanks a lot, awsomead.
Your're awesome ( and that's not only a lame joke ).
description is updated.
There is an error in the description, specifically in the example for past relative shifts:
[0.044444, -0.010638, -0.021505, -0.010989, 0.111111, 0.04, -0.009615, -0.019417, -0.019802, 0.0]
That last item should be 0.010101. It is correct in the sample tests, but not in the description.
This comment is hidden because it contains spoiler information about the solution
I am having two issues:
(1) I seem to have a problem with rounding, as I am getting this error message in the tests: "1-Day Equity Value at Risk for your 100 MartyCorp - Equities and a confidence level of 0.90 is 206.54 USD: 206.53 should equal 206.54"
(2) It's not clear to me how to calculate total VAR for equtiy and foreign exchange risk. For each day I calculate the value shift due to the change in equity rates and the change in foreign exchange rate and I then add the two amounts.
I am enjoying this kata and would appreciate a hint so that I can finish it.
Shame on me,
thanks for point out these issues.
Should be fixed, now.