 You will walk away from this video knowing how to generate quantities with command-stand. The goal is to start building good-stand habits. To do this, I'm going to set aside the full power of the stand sampler, and we are going to repurpose stand to be a beefy calculator. We're going to approach command-stand very different to documentation. We're going to start with writing a simple function, compiling that function, and writing the results to CSV. The reason to do this is that generating quantities is an essential part of command-stand, where our stand and pie-stand do more hand-holding. Command-stand expects more from the user. The fitting of the models is useful, but unless you're comfortable generating predictions from those models, that's only going to get you part of the way there. So being able to generate quantities, any quantity of interest, is going to be essential when working with command-stand. Also before I dive in, I wanted to point out that I will be using a Tmux session. Because command-stand compiles programs using the make command and the command-stand directory, it's handy to be able to bounce around different directories, say it is different windows in Tmux. You will see that the star indicates the window name in my light line bar. I have one window for running make and the command-stand directory, one window for file editing, and a final window for running the executable, either on its own or with a data file. The first thing that we're going to make is a square function. I'm just going to put in the number two, and I want to generate a number that is that value squared. This requires two blocks, a function block and a generated quantities block. The function requires that the input type is defined and the output type is defined. Here the input type is a real and the output type is a real. In the generated quantities block, the function is called. Note that no return statement is required. If calculated in the generated quantities block, it will be returned in the output file. Now it's going to be compiled. We need to go to the command-stand directory, run make on the file we just wrote. Now that the file has been compiled, there's a little extra work that we need to do to tell Stan that we want just a single value. We need to set the number of samples to one. This is because we're going to hold off on Dan's samplers. In this case, we're also going to have to set fixed parameters. That just means that we're going to be using parameters that are already defined. It's not something that has to be fit. Once that is complete, we can use Miller to ignore comments that Stan adds to the output file and just select the value of interest. Say I want to make some modifications. Right now, the value is hard-coded into the compiled script. I want to be able to change the data without needing to recompile. To do this, I'm going to move the value and make a new data block in the Stan file. The data block will use the JSON file we passed to it. With just a couple modifications, the Stan file set up to read any value we set in the JSON file. After recompiling the Stan file, it can be seen that it is functioning as expected. The data in the JSON file can be modified and the output reflects that change. As an example, I will change the input to three, then four. There's one more thing I wanted to show in this video. I wanted to pass more than just one value. I wanted to pass a vector of values. There are a couple options for iterating through values, for loops or vectors. For loops are not optimal. As noted in the Stan documentation, vectorized code is faster than loops. That said, sometimes an application requires for loops. Time series models come to mind. Though the vectorization will not make a noticeable difference here, it is worth getting into the habit of writing early on. The function block, the data block, and the generated quantities block all need to be changed to allow for vectors. The JSON file also needs to change. I find it is easiest to start with the data, then work out the Stan file. In the JSON file, the length of the vector needs to be specified with the N entry. Then the desired quantities can be entered as an array of values in the X key. The data is ready to go. The Stan file needs to reflect the fact that calculations on the vector are wanted. The function needs to return a vector. The data going in is a vector. In the data block, N, the integer defining the length of the array needs to be defined. X is defined as a vector of length N. In the generated quantities, a vector of length N is set as the type for A. Using Vim fugitive, I'm going to compare where this file started and where it is now. In the beginning, there was only two blocks, a function and a generated quantities block. Then the data was separated from the main file and put into a JSON file. This required a data block. Finally, the data was modified to pass an array of values. This required a new approach where all of the types were changed from reels to vectors. Now this program can be compiled. The output shows each iteration of the value A followed by a dot. Stan stores data in the output file in a specific way where each quantity is a column and each sample is a row. Because there is no samples being generated, this is a one row CSV. You now know how to generate any quantity of interest. Here we just square the value. But if you want to practice, you can roll your own transformation, maybe a temperature converter, or you can pass in multiple numbers to calculate something more interesting. Going to Nerd Wallet and replicating one of their financial calculators could be a fun challenge. You also learned how to write vectorized code in Stan. This is something that you will continue to see in future videos. Thank you for watching.