Last active
May 23, 2020 14:24
-
-
Save fyyying/6df152b17fbd091ccfffe75e4026dd1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Implementation with PySpark | |
from pyspark.sql import SparkSession | |
from pyspark.sql import Window | |
from pyspark.sql import functions as f | |
# Define Spark settings | |
builder = SparkSession.builder.appName("Forward_Rate_Calculation") | |
spark = builder.getOrCreate() | |
# Read in the spot rate data as csv | |
data = spark.read.csv(rate_path, header=True, inferSchema=True) | |
# Shift the spot rate down along the maturity axis and calculate forward rate | |
window = Window.orderBy("Maturity") | |
data = data.withColumn("Spot_Rate_Shift", f.lag(f.col("Spot_Rate"), 1).over(window)) \ | |
.fillna(0, subset=["Spot_Rate_Shift"]) | |
data = data.withColumn("Forward_Rate", (1 + f.col("Spot_Rate"))**f.col("Maturity") / | |
((1 + f.col("Spot_Rate_Shift"))**(f.col("Maturity") - 1)) - 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment