Skip to content

Instantly share code, notes, and snippets.

@quantra-go-algo
Created August 11, 2023 21:00
Show Gist options
  • Save quantra-go-algo/e08e441d0c5a627e0f4a9bf9c2f05b73 to your computer and use it in GitHub Desktop.
Save quantra-go-algo/e08e441d0c5a627e0f4a9bf9c2f05b73 to your computer and use it in GitHub Desktop.
def optimal_d(DF, alpha=0.035, minimum = 0, maximum = 1):
# Copy the dataframe
df = DF.copy()
# Estimate the best d based on the basic d range
out = d_estimates_db(df,minimum,maximum)
out.to_excel('out1.xlsx')
# A try-except block to handle erros while getting a d with better decimals
try:
# Get the d value which critical value is closest to and higher than alpha
d1 = out[out['pVal']>alpha].index.values[-1]
# Get the d value which critical value is closest to and lower than alpha
d2 = out[out['pVal']<alpha].index.values[0]
# Estimate the best d between d1 and d2
out = d_estimates_db(df,d1,d2)
out.to_excel('out2.xlsx')
try:
# Get the d value which critical value is closest to and higher than alpha
d1 = out[out['pVal']>alpha].index.values[-1]
# Get the d value which critical value is closest to and lower than alpha
d2 = out[out['pVal']<alpha].index.values[0]
# Estimate the best d between d1 and d2
out = d_estimates_db(df,d1,d2)
out.to_excel('out3.xlsx')
try:
# Get the d value which critical value is closest to and higher than alpha
d1 = out[out['pVal']>alpha].index.values[-1]
# Get the d value which critical value is closest to and lower than alpha
d2 = out[out['pVal']<alpha].index.values[0]
# Set d as the average between d1 and d2 with four decimals
d = round((d1+d2)/2,4)
except:
# In case the try fails, assign d with the critical value which is closest to and higher than alpha
d = out[out['pVal']<alpha].index.values[0]
except:
# In case the try fails, assign d with the critical value which is closest to and higher than alpha
d = out[out['pVal']<alpha].index.values[0]
except:
# In case the try fails, assign d with the critical value which is closest to and higher than alpha
d = out[out['pVal']<alpha].index.values[0]
# Return the d value
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment