Skip to content
All library documents

Estimating Daily Volatility from Irregularly Spaced Price Data

Article Quant Q&A · Author: Wynton Lam

Summary

The document presents a Python function for estimating daily volatility from a close-price series. It searches the time index for the most recent observation at least one calendar day before each timestamp, computes the corresponding price returns, then applies an exponentially weighted standard deviation. The result is named as a daily volatility series. The example shows passing the closing-price column from a market-data table into the function, with a chosen span controlling the exponential weighting.

The question arises from a variable-name error: the function parameter named close must be supplied with a price series when the function is called. The provided example uses a downloaded equity history and selects its Close column. The function also reports a warning if return construction fails, suggesting duplicate indices as one possible cause. This is implementation guidance rather than evidence that the estimator predicts risk well; choices such as the calendar-day lookback, data frequency, missing observations, and span affect its output.

Key ideas

  • The function estimates returns using the latest available price at least one calendar day earlier.
  • An exponentially weighted standard deviation of those returns provides the volatility series.
  • The function’s close parameter must receive a price series, such as a table’s closing-price column.
  • Duplicate timestamps and data alignment can cause return calculation problems.

Tags

Full text
# why am I seeing a value error? adv_fml. lopez de prado


# why am I seeing a value error? adv_fml. lopez de prado












This code is a snippet from Lopez De Prado Advances in Financial Machine Learning page 44

```
def getDailyVol(close,span0=100):
    # daily vol reindexed to close

    df0=close.index.searchsorted(close.index-pd.Timedelta(days=1))
    df0=df0[df0>0]   
    df0=(pd.Series(close.index[df0-1], 
                   index=close.index[close.shape[0]-df0.shape[0]:]))   

    try:
        df0=close.loc[df0.index]/close.loc[df0.values].values-1 # daily rets
    except Exception as e:
        print(f'error: {e}\nplease confirm no duplicate indices')
    df0=df0.ewm(span=span0).std().rename('dailyVol')
    return df0
```

The parameters of the function are close and span. In the function statement the close is not a dataFrame of which I called `data['Close']`. Tried to create a variable for that index.Error: name 'close' is not defined.

## Answer by oronimbus (score 3)

https://quant.stackexchange.com/a/65731

Welcome to QSE. Perhaps this question is more suited for SO as it's purely syntax related? I have tried the script from MLDP's book and it works just fine. Using yahoo finance data:

```
import yfinance as yf
import pandas as pd

from datetime import datetime

def getDailyVol(close,span0=100):
    # daily vol reindexed to close

    df0=close.index.searchsorted(close.index-pd.Timedelta(days=1))
    df0=df0[df0>0]
    df0=(pd.Series(close.index[df0-1],
                   index=close.index[close.shape[0]-df0.shape[0]:]))

    try:
        df0=close.loc[df0.index]/close.loc[df0.values].values-1 # daily rets
    except Exception as e:
        print(f'error: {e}\nplease confirm no duplicate indices')
    df0=df0.ewm(span=span0).std().rename('dailyVol')
    return df0

ticker = "SPY"
instrument = yf.Ticker(ticker)
data = instrument.history(start=datetime(2012,1,1), end=datetime.now())
result = getDailyVol(data['Close'], span0=63)
print(result)
```

Perhaps topic for another time, but having read some of MLDP's work, I must say that I am not a fan of his coding style (e.g. rarely comments, disregards any kind of Python coding convention etc.).

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.