Skip to content

Instantly share code, notes, and snippets.

View andydawson's full-sized avatar

Andria Dawson andydawson

  • Mount Royal University, Calgary
  • Canada
View GitHub Profile
@andydawson
andydawson / gradient.md
Last active April 12, 2023 09:03
Manual gradient in Stan 2.10

Implementing your own gradient function in Stan.

Here's a summary of how to implement a gradient function manually in Stan 2.10 in order to override autodiff. I wanted to do this because my run-times were long, and I knew I could save some time by hard-coding my gradient function. That being said, autodiff is great, and is almost always fast enough.

Step 1. Create the .stan file which implements the model.

Two reasons to do this:

  1. This will allow you to easily generate a .cpp file that will contain the basic structure you need.
  2. Great for testing to make sure your hacked code gives identical output to what you would get using Stan.