Denoising time series e.g. historical price data using Least Squares Method in python using NumPy.
The denoise function in lsm-denoise.py simply could be used by passing a 1-dim numpy array of time series data to the function.
example:
denoised_prices = denoise(
price_vec,
LAMBDA=100,
plot=True
)
suppose y is the time series vector, x is the denoised vector and v is an unknown noise vector
to find x we need to solve the following minimization problem using least squares method:
this problem minimizes the noise factor v with the penalty of the difference between two consequetive x values become minimized.
the penalty term could be written as:
where D is an (n-1)x(n) matrix like:
and finally the main minimization problem could be re-written as:
an example of denoised time series by different LAMBDA valuse plotted below:





