Substitute outliers in a time series by most recent valid data
The time series (end-of-day stock prices) has several 'uncomfortable' properties:
- It is non-stationary and can have components of low and high frequency (trends and sudden price moves)
- There can be missing data for some days, typically single occurrences, but sometimes a whole consecutive segment is missing
- Some values are erroneous (implausibly high or low i.e. due to misplaced decimal sign). These are the real outliers and don't belong to the series.
There is an additional caveat: The substitution algorithm must not look into the future of the time series since it will be used in a machine learning scenario.
What I thought of so far:
- Detrending the series. Maybe rolling linear detrending by the last
nvalues. Will still be a problem with very short term moves.
- Replacing the missing values by extrapolating the trend identified in (1)
- Calculating the z-score and replacing values with
z > 6by extrapolating the trend identified in (1)
As the tags suggest, I will realise this in python / scipy. However I don't have much experience with time series analysis. I have no idea if this is an appropriate approach or if I'm missing something important. Any help is appreciated. Thanks.