Skip to main content

Exercise 0: Technical prerequisites

Before we begin working with R, Python, and Quarto in Advanced Empirical Finance, please set up your computer so that you can run code from day one. No prior experience with programming or development tools is required — this guide walks you through everything step by step. Should you encounter any issues, please do not hesitate to contact your TAs or me for assistance. To facilitate collaboration, please ask me via the Absalon discussion board to make sure everyone can benefit from the solution.

Prerequisites

Install the required software

  1. Install Positron (latest Version). Positron is the integrated development environment (IDE) we use. It supports R, Python, Git, and Quarto in one place. Install Positron using the default settings.

  2. Register a free account on GitHub. GitHub allows us to share course materials and collaborate.

  3. Install Git. Git is the version control system we use to connect to GitHub.

  4. Install uv and uvr. uv installs Python and the Python packages of the course; uvr does the same for R. You don’t install R or Python yourself. Open a terminal and run the two lines for your system:

    macOS and Linux (Terminal):

    curl -LsSf https://astral.sh/uv/install.sh | sh
    curl -fsSL https://raw.githubusercontent.com/nbafrank/uvr/main/install.sh | sh

    Windows (PowerShell):

    powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
    powershell -ExecutionPolicy ByPass -c "irm https://raw.githubusercontent.com/nbafrank/uvr/main/install.ps1 | iex"

    Then close the terminal and open a new one, so it finds uv and uvr. If uv and uvr are already on your computer, for example from another course, update them instead with uv self update and uvr upgrade; the course needs uvr 0.4.8 or later.

Set up the course repository

  1. Clone the course repository in Positron:
    • Open the Source Control view Ctrl-Shift-G. If Git is not yet installed, the view shows how to install it.

    • Click Clone Repository and paste the address of the course repository:

      https://github.com/advanced-empirical-finance/course-repository.git

      Choose a local folder for the repository. Cloning means creating your own copy of a shared project on your computer, so you can work on it without affecting the original.

  2. Open the cloned repository in Positron with File → Open Folder. The folder holds the slides, the templates for the mandatory assignments, the R and Python environment and the data folder; the licensed data from Absalon go there, too (see the exercises on financial data). When we announce new course material, get it with git pull in the Terminal, or Pull in the Source Control view.

We encourage you to use Git and GitHub for your team work, but you do not have to. If you want to, one of you creates the team’s repository; it stays connected to the course repository, so new course material still reaches you.

  1. On GitHub, create a new private repository, without a README, for example aef27-<team>. Under Settings → Collaborators, invite your teammates.

  2. In the Terminal of your clone of the course repository, run (with your username and repository name):

    git remote rename origin upstream
    git remote add origin https://github.com/<username>/aef27-<team>.git
    git push -u origin main
    git config pull.rebase false
  3. Your teammates accept the invitation, clone the team repository instead of the course repository, and run

    git remote add upstream https://github.com/advanced-empirical-finance/course-repository.git
    git config pull.rebase false

From then on, git pull and git push work with your team’s repository, and git pull upstream main brings new course material. How to split the work and check each other’s reports: collaboration with Git and GitHub.

Install R, Python and the course packages

  1. In the Terminal (not the Console) of Positron: because you opened the course repository in step 6, the Terminal starts in the repository’s folder, the one that holds uvr.toml and pyproject.toml. Then run

    uvr r install 4.6.1
    uvr sync
    uv sync

    uvr r install installs R, uvr sync installs all R packages of the course, and uv sync installs Python and all Python packages of the course, including aefsim, the client for the trading game. Everyone gets exactly the same package versions, because they are recorded in the repository (uvr.lock for R, uv.lock for Python). If Positron asks which R or Python to use, choose R 4.6.1 for R. For Python, choose the environment that uv sync just created in the .venv folder of the course repository: its path ends in course-repository/.venv, not in a Python installed elsewhere on your computer.

    Adding a package works the same way in both: uvr add tidymodels for R, uv add polars for Python. To collaborate with your peers, commit the files uvr.toml, uvr.lock, pyproject.toml and uv.lock; your colleagues only have to run uvr sync and uv sync to replicate your exact package environment.

  2. To verify that R is working correctly, click on New/New File and select R file to open a new R script in Positron and type

    library(tidyfinance)
    download_data(
            domain = "Stock Prices",
            symbols = "AAPL",
            start_date = "2000-01-01",
            end_date = "2023-12-31")

    Mark everything and press to run the code. If everything is working correctly, you should see that you just downloaded financial data in the console. If you receive an error such as there is no package called 'tidyfinance', Positron is not using the course’s R: when R starts, the console should say uvr: library linked (… packages). If it doesn’t, choose R 4.6.1 with the interpreter icon on the top right, restart R (Session → Restart R), and run uvr sync in the Terminal again.

  3. To verify that Python is working correctly, click on New/New File and select Python file to open a new Python script in Positron and type

    import tidyfinance as tf
    tf.download_data(
            domain="Stock Prices",
            symbols="AAPL",
            start_date="2000-01-01",
            end_date="2023-12-31")

    Mark everything and press to run the code. If everything is working correctly, you should see that you just downloaded financial data in the console. If you receive an error, select the course’s Python environment first: click on the interpreter icon on the top right and choose the one in the course repository’s .venv.

Render PDFs with TinyTeX

You hand in every mandatory assignment as a PDF that Quarto renders from your .qmd file. Quarto comes with Positron, but PDF output also needs LaTeX. Install TinyTeX, a small LaTeX distribution, once:

  1. In Positron’s Terminal, run:

    quarto install tinytex
  2. Check the installation with quarto check. Under “Checking LaTeX”, the output should name TinyTeX. If it says Tex: (not detected), run the install command again, then restart Positron.

  3. Test it: open mandatory-assignments/MA_R_template.qmd (or MA_python_template.qmd) from the course repository and click Preview. Positron shows the PDF in the Viewer.