# Code

### Market Prediction with ETFs & Convolutional Networks

Convolutional networks are most prominently used for image analysis or on data with multiple spatial dimensions. Of course, since the inputs to the CNNs are all just numbers, you can feed in other data that has some a relationship encoded into the dimensions of the array. This post involves feeding data for historical returns from exchange traded funds (ETFs) into a CNN, and using it to try to predict the direction of the Dow Jones Industrial Average (DJIA) some time in the future. I’ll be using Keras to code the neural network. The Jupyter notebook used to develop this code is here.

As with all posts of this nature, this shouldn’t be taken as advice on what to do with your money.

### Matrix to LaTeX

I recently had to go through some matrix operations in R and then write up the results in LaTeX. Formatting the R output to get it into a form for LaTeX isn’t particularly hard, but it’s tedious and it has a regular structure, so it seemed like it would be easy to code it up. So I decided to try it for R, Python, and Julia.

### An Example With accumulate()

As with most useful (collections of) libraries, the tidyverse has a lot to offer. One interesting bit that I found recently was the `accumulate()` function in the `purrr` library, which allows you to apply a function over a succession of values in a vector. This post is a quick example of its use, using linear regression models.

### Formatting With ggtext Example

This is a quick example regarding the `ggtext` package. It’s one of the many packages that extends `ggplot2`, with this one having a focus on adding and formatting text in graphs. The particularly interesting thing for me is that it allows Markdown and other formatting of the labels in a graph.

### A Slightly More Advanced MCMC Example

I’ve seen a number of examples of MCMC algorithms, and while they’re all solid, a lot of them tend to be a bit too neat - they have a fairly simple model, a single predictor (maybe two), and not much else. This one is a good example, as it covers the theory in detail, but it’s using an obviously toy data set. So I decided to throw together a slightly more intricate example, highlighting a couple of issues and tricks worth noting for a handwritten implementation.

Note that this post is written under the assumption that the reader already has some knowledge about what MCMC is generally for and broadly how it works. This post is all R code (see here), with no JAGS or BUGS or such. The `tidyverse`, `patchwork`, and `ISLR` libraries are required – the former two for the plots, the latter for the data set used.

### Detecting Streaks in R

Inspired by this post, which tries to calculate streaks in Python’s `pandas` library, I thought I’d give it a try in R, since it’s all just dataframe operations in the Python post. I won’t repeat his analysis, but I will replicate the streak determination and some of the plots. The data he uses is here.

### Python Random Forest in C

I had occasion a while back to try to do a random forest prediction in C. This is a highly situational need – I only did it because I needed to get a random forest that could work with other stuff written in C, no Python allowed – but it was interesting to try to pull apart scikit-learn’s `RandomForestRegressor` and restructure it in another way.

### Integer String Conversion in Python

Handling numbers as strings is one of those data things that’s a pretty consistent pain. I, personally, have had to deal with translating between binary and hexadecimal strings with some regularity. And this is a situational need, so there’s not much reason to expect something pre-built. So I threw together a quick class myself.