Skip to content

Instantly share code, notes, and snippets.

@flare9x
Created March 4, 2022 15:35
Show Gist options
  • Save flare9x/40a10b65ede2b8d7abe6d6bfd727e963 to your computer and use it in GitHub Desktop.
Save flare9x/40a10b65ede2b8d7abe6d6bfd727e963 to your computer and use it in GitHub Desktop.
spot_crude_frequency_distribution.jl
using FredApi, DataFrames, Dates, Statistics, Plots, TimeSeries
# load FRED oil data
key = "your_key"
set_api_key(key)
# full sample
crude_oil = get_symbols("WTISPLC")
price = values(crude_oil)
p1 = plot(crude_oil, title = "Price - Spot Crude Oil Price: West Texas Intermediate (WTI)\n Monthly Time Samples 1946-01-01 to 2022-02-01", titlefontsize=10, legend=false)
p2 = histogram(price, title = "Frequency Distribution - Spot Crude Oil Price: West Texas Intermediate (WTI)\n Monthly Time Samples - 1946-01-01 to 2022-02-01", titlefontsize=10, bins=20, legend=false)
#subset 2005 onwards
x = get_symbols("WTISPLC", "2005-01-01", "2023-01-01")
price_x = values(x)
p3 = plot(x, title = "Price - Spot Crude Oil Price: West Texas Intermediate (WTI)\n Monthly Time Samples 1946-01-01 to 2022-02-01", titlefontsize=10, legend=false)
p4 = histogram(price_x, title = "Frequency Distribution - Spot Crude Oil Price: West Texas Intermediate (WTI) \n Monthly Time Samples - 2000-01-01 to 2022-02-01", titlefontsize=10, bins=40, legend=false)
# % of observations below / over a threshold
upper_threshold = 100.0
lower_threshold = 40.0
# subset the values
upper_observations = price_x[price_x .>= upper_threshold]
percent_ovservations_over_upper_threshold = (size(upper_observations,1) / size(price_x,1)) * 100
lower_observations = price_x[price_x .< lower_threshold]
percent_ovservations_over_lower_threshold = (size(lower_observations,1) / size(price_x,1)) * 100
# plot the data
l = @layout [a b ; c d]
fn = plot(p1, p2, p3, p4, layout = l, size=(1500,1000))
savefig(fn,"C:/Code Development/spot_oil.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment