Skip to content

Instantly share code, notes, and snippets.

@WilliamJosephKessler
Forked from StuartGordonReid/Volatility.py
Created December 19, 2019 08:37
Show Gist options
  • Save WilliamJosephKessler/0f5e8db0dad1e8b46f045982b04ed7b3 to your computer and use it in GitHub Desktop.
Save WilliamJosephKessler/0f5e8db0dad1e8b46f045982b04ed7b3 to your computer and use it in GitHub Desktop.
Volatility Risk Metrics
import numpy
import numpy.random as nrand
def vol(returns):
# Return the standard deviation of returns
return numpy.std(returns)
def beta(returns, market):
# Create a matrix of [returns, market]
m = numpy.matrix([returns, market])
# Return the covariance of m divided by the standard deviation of the market returns
return numpy.cov(m)[0][1] / numpy.std(market)
# Example usage
r = nrand.uniform(-1, 1, 50)
m = nrand.uniform(-1, 1, 50)
print("vol =", vol(r))
print("beta =", beta(r, m))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment