I have quite a large task to run and in doing so I’ve encountered unfortunate issues. Below there are two blocks of code, one with a bunch of for loops (attempt 1), the other using functions (attempt 2). I’m running these on a Windows 11 laptop via a Jupyter Notebook opened from IJulia in Anaconda prompt. Attempt 1 had a time counter. It got to 2% within half an hour, but unfortunately it only got to 13% after 8 hours (overnight). The for loop is embarrassingly parallel, so I’m not sure why it would slow down so much with time. I woke up, begin using my computer, and then it ran out of memory (I think due to other memory-intensive programs I was using during the day).
I tried Attempt 2 the second night. 0.1% of the task (changing the value of T) took 15 seconds, so I thought it should complete overnight. However, when I woke up, Julia had… vanished. My computer had signed out of Windows but not restarted (I had set it never to sleep or sign out). Firefox was still open (I was running in a Jupyter Notebook) but the Notebook showed nothing on screen. I tried reopening the Notebook but I couldn’t connect to localhost:8888 - Anaconda prompt (in which I opened Julia) had seemingly shut. I had no error visible or anything - very strange. Julia just vanished mid-task.
I’ve attempted to use functions to write better code in Attempt 2. I’m quite new to programming so might be making significant rookie errors. Any guidance would be appreciated. For a MWE, simply change the value of T to tau or tau+1, tau+2, etc.
ATTEMPT 1:
using DataFrames, CSV, Dates, StatsBase, ProgressMeter,Statistics
const T,n,tau,M=5788,366,90,1000 #CHANGE T to tau+1
const daily_rf_rate = 0.0;
price_mat=rand(T,n);
empty_array = Array{Float64}(undef, T-tau+1, 91, 3);
# #The main loop is over t.
@showprogress for t in tau:T
for k in 10:100
sharpe_list=Array{Float64}(undef,M)
for i in 1:M
my_sample=sample(1:n,k,replace=false)
drawn_prices=price_mat[t-tau+1:t,my_sample]
# Calculate daily returns for each stock
returns = diff(drawn_prices, dims=1) ./ drawn_prices[1:end-1, :]
# Calculate daily portfolio returns using equal weights (0.1 for each stock since there are 10 stocks)
portfolio_returns = sum(returns, dims=2) * 1/k
# Compute average return and standard deviation for the portfolio
average_portfolio_return = mean(portfolio_returns)
stddev_portfolio_return = std(portfolio_returns)
# Calculate the Sharpe ratio for the portfolio
sharpe_ratio_portfolio = (average_portfolio_return - daily_rf_rate) / stddev_portfolio_return
##empty_array[t,k,i]=sharpe_ratio_portfolio ##I'll save them later
sharpe_list[i]=sharpe_ratio_portfolio
end
empty_array[t-tau+1,k-9,1]=quantile(sharpe_list,0.1); #FILL IN THE QUANTILE VALUES INTO ARRAY
empty_array[t-tau+1,k-9,2]=quantile(sharpe_list,0.5);
empty_array[t-tau+1,k-9,3]=quantile(sharpe_list,0.9);
end
end
ATTEMPT 2
using DataFrames, CSV, Dates, StatsBase, ProgressMeter,Statistics
const T,n,tau,M=5788,366,90,1000 #CHANGE T to tau+1
const daily_rf_rate = 0.0;
price_mat=rand(T,n);
function compute_sharpe(t, k)
sharpe_list = Array{Float64}(undef, M)
for i in 1:M
my_sample = sample(1:n, k, replace=false)
drawn_prices = price_mat[t-tau+1:t, my_sample]
returns = diff(drawn_prices, dims=1) ./ drawn_prices[1:end-1, :]
portfolio_returns = sum(returns, dims=2) * 1/k
average_portfolio_return = mean(portfolio_returns)
stddev_portfolio_return = std(portfolio_returns)
sharpe_ratio_portfolio = (average_portfolio_return - daily_rf_rate) / stddev_portfolio_return
sharpe_list[i] = sharpe_ratio_portfolio
end
return sharpe_list
end
function compute_quantiles(t,k)
sharpe_list=compute_sharpe(t,k)
return [quantile(sharpe_list,0.1), quantile(sharpe_list,0.5), quantile(sharpe_list,0.9)]
end
I=tau:T;J=10:100
Ans_full=compute_quantiles.(I',J);