# Generating surface of Kernel Density Estimates over time

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

df_ohlc
Out:
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]

• Have you had a look at the matplotlib 3d examples? – will Oct 12 '18 at 14:55

## 1 Answer

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]