Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Implement Value at Risk (VaR)

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Implement Value at Risk (VaR). Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

In an EY Oman Quantitative Advisory analysis, calculate the historical Value at Risk for a portfolio of assets. Use each asset's historical daily price changes to create portfolio profit-and-loss scenarios, then return the loss percentile at the requested confidence level.

For each asset, apply its historical percentage return to its current market value, where current market value is quantity * latest_price. Sum the resulting P&L across all assets for each historical day. Portfolio loss is the negative of portfolio P&L.

Use the nearest-rank percentile rule: sort the losses and select the element at zero-based index ceil(confidence * number_of_scenarios) - 1. Return the VaR as a numeric value in portfolio currency. A negative result is valid when the selected percentile represents a gain.

Formal Specification

Implement calculate_var(positions, confidence). positions is a list of dictionaries, each containing quantity and prices. Every prices list contains at least two positive daily prices and all assets have the same number of observations. confidence is a decimal strictly between 0 and 1. Return a floating-point VaR.

Constraints

  • 1 <= len(positions) <= 10^4
  • 2 <= len(prices) <= 10^4
  • All assets have equal price-history lengths
  • prices[i] > 0
  • quantity may be positive or negative
  • 0 < confidence < 1

Function Signature

def calculate_var(positions, confidence):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output