[PYTHON] How to calculate the volatility of a brand

1. Purpose

Calculate the volatility of a stock over a period of time.

2 Contents

2-1 Volatility formula

For the price of a stock, when the stock price on any day is $ S_ {i} $, the rate of change from the previous day $ c_ {i} $ is defined as follows.

c_{i} = \biggl({\frac{S_i}{S_{i-1}}} \biggl)

At this time, the volatility of the stock from the start date to n business days later is defined by the following formula.

vol_{n} = \sqrt{\sum_{i=1}^{n} {(c_{i}-\bar{c})}^{2}}

2-2 Source code example for calculating Volatility

test.py


#Annual volatirity(Setting trading date 252 days)A function that calculates.
def volatility(DF):
    df = DF.copy()
    df["daily_ret"] = DF["Close"].pct_change()
    vol = df["daily_ret"].std() * np.sqrt(252)
    return vol

Recommended Posts

How to calculate the volatility of a brand
Steps to calculate the likelihood of a normal distribution
How to calculate the autocorrelation coefficient
[Numpy, scipy] How to calculate the square root of a semi-fixed definite matrix
[Ubuntu] How to delete the entire contents of a directory
How to calculate the amount of calculation learned from ABC134-D
How to find the scaling factor of a biorthogonal wavelet
How to connect the contents of a list into a string
How to check the version of Django
How to calculate Use% of df command
How to determine the existence of a selenium element in Python
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
How to find the memory address of a Pandas dataframe value
How to output the output result of the Linux man command to a file
How to get the vertex coordinates of a feature in ArcPy
[NNabla] How to remove the middle tier of a pre-built network
A simple example of how to use ArgumentParser
How to find the area of the Voronoi diagram
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[NNabla] How to add a quantization layer to the middle layer of a trained model
How to put a line number at the beginning of a CSV file
Calculate the probability of outliers on a boxplot
How to create a wrapper that preserves the signature of the function to wrap
How to play a video while watching the number of frames (Mac)
How to pass the execution result of a shell command in a list in Python
How to mention a user group in slack notification, how to check the id of the user group
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
[NNabla] How to get the output (variable) of the middle layer of a pre-built network
How to count the number of elements in Django and output to a template
How to access the contents of a Linux disk on a Mac (but read-only)
[python] How to sort by the Nth Mth element of a multidimensional array
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
How to make a Raspberry Pi that speaks the tweets of the specified user
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to know the port number of the xinetd service
Calculate volume from the two-dimensional structure of a compound
How to write a GUI using the maya command
How to get the number of digits in Python
A memo of how to use AIST supercomputer ABCI
A memo to visually understand the axis of pandas.Panel
How to create a submenu with the [Blender] plugin
How to visualize the decision tree model of scikit-learn
How to write a list / dictionary type of Python3
[Blender] How to dynamically set the selection of EnumProperty
Basics of PyTorch (2) -How to make a neural network-
[Python] Summary of how to specify the color of the figure
How to hit the document of Magic Function (Line Magic)
How to access the global variable of the imported module
How to post a ticket from the Shogun API
Calculate the product of matrices with a character expression?
[Selenium] How to specify the relative path of chromedriver?
Python Note: The mystery of assigning a variable to a variable
How to use the generator
How to call a function
How to hack a terminal
Calculate the number of changes
How to use the decorator
How to increase the axis
How to start the program