Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rolling median in python

I have some stock data based on daily close values. I need to be able to insert these values into a python list and get a median for the last 30 closes. Is there a python library that does this?

like image 399
yueerhu Avatar asked Mar 30 '11 12:03

yueerhu


People also ask

What is a rolling median?

A rolling median is the median of a certain number of previous periods in a time series.

What is rolling method in Python?

The rolling() function is used to provide rolling window calculations. Syntax: Series.rolling(self, window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None) Parameters: Name.

What is DF rolling?

A rolling mean is simply the mean of a certain number of previous periods in a time series. To calculate the rolling mean for one or more columns in a pandas DataFrame, we can use the following syntax: df['column_name'].


3 Answers

In pure Python, having your data in a Python list a, you could do

median = sum(sorted(a[-30:])[14:16]) / 2.0

(This assumes a has at least 30 items.)

Using the NumPy package, you could use

median = numpy.median(a[-30:])
like image 192
Sven Marnach Avatar answered Sep 25 '22 09:09

Sven Marnach


Have you considered pandas? It is based on numpy and can automatically associate timestamps with your data, and discards any unknown dates as long as you fill it with numpy.nan. It also offers some rather powerful graphing via matplotlib.

Basically it was designed for financial analysis in python.

like image 31
Mike Pennington Avatar answered Sep 22 '22 09:09

Mike Pennington


isn't the median just the middle value in a sorted range?

so, assuming your list is stock_data:

last_thirty = stock_data[-30:]
median = sorted(last_thirty)[15]

Now you just need to get the off-by-one errors found and fixed and also handle the case of stock_data being less than 30 elements...

let us try that here a bit:

def rolling_median(data, window):
    if len(data) < window:
       subject = data[:]
    else:
       subject = data[-30:]
    return sorted(subject)[len(subject)/2]
like image 29
Daren Thomas Avatar answered Sep 25 '22 09:09

Daren Thomas