Skip to content
All library documents

Plotting Weekly Price Distributions as a KDE Surface Over Time

Article Quant Q&A · Author: Adam

Summary

The document addresses how to visualize changes in weekly closing-price distributions from minute-level market data. It proposes estimating a kernel density for each week, evaluating those estimates over a shared range of prices, and combining the resulting density values into a grid indexed by price and week. A three-dimensional surface can then show price, probability density, and time together.

The response suggests either implementing a kernel density formula directly or using a statistical library, then assembling the weekly estimates for surface plotting. This is a visualization recipe rather than a trading signal or empirical study. The discussion does not specify bandwidth selection, how to handle weeks with few observations, or how to make density estimates comparable when samples differ; those choices affect the surface's interpretation.

Key ideas

  • Estimate a separate kernel density from each period's closing prices.
  • Evaluate each estimate over a common price grid so periods can be compared.
  • Arrange density values by price and week to form a surface plot.
  • Bandwidth choice and uneven weekly sample sizes can change the apparent distribution shapes.

Tags

Full text
# Generating surface of Kernel Density Estimates over time


# Generating surface of Kernel Density Estimates over time












I have a 1-minutely OHLC dataset indexed by time as follows:

```
df_ohlc
Out[2]: 
                      open  high  low  close  index  week
Date                                                    
2011-09-13 09:53:00   5.8   6.0  5.8    6.0      1     1
2011-09-13 09:54:00   6.0   6.0  6.0    6.0      2     1
2011-09-13 09:55:00   6.0   6.0  6.0    6.0      3     1
2011-09-13 09:56:00   6.0   6.0  6.0    6.0      4     1
2011-09-13 09:57:00   6.0   6.0  6.0    6.0      5     1
...
2017-07-17 18:19:00  2176.99  2176.99  2176.50  2176.50  3073467   305
2017-07-17 18:20:00  2175.00  2177.65  2175.00  2176.99  3073468   305
2017-07-17 18:21:00  2177.80  2177.84  2173.71  2177.61  3073469   305
2017-07-17 18:22:00  2177.50  2177.50  2175.04  2175.04  3073470   305
2017-07-17 18:23:00  2177.30  2177.30  2175.00  2175.00  3073471   305
```

In Python,

```
for i in range(1,len(df_ohlc)+1):
  plt.clf()
  kde_est.iloc[i] = df_ohlc['close'][df_ohlc['week']==i].plot.kde()
  plt.show()
```

generates the Kernel Density Estimate (a smooth histogram essentially) for each week's closing prices of the dataset. In other words, it generates 305 individual KDE plots for this dataset.

How would I plot all these KDEs over time on one 3-Dimensional surface?

For example, right now each KDE plot is [Close Price] x [Probability Density]. I'd like to introduce a new variable (z = time) so we can see the changes in KDE over time, [Close Price] x [Probability Density] x [Week]

## Answer by Tarasenya (score 1)

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

Perhaps the easiest way is

1) To write manually a function, that takes an array(this corresponds to your data during a week), either with hands, using a kernel you want $f(x)=\frac{1}{nh}\sum_{k=1}^n K((x-x_i)/hn)$, or using scipy.stats.gaussian_kde(or more general using sklearn.neighbors KernelDensity).

2) Merge the results of 1) into 1 array, say Prob.

3) Using the corresponging methods of matplotlib (plot_surface, etc) plot as you said [Price]x[Prob]x[Week]

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.