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
def extract_dict_from_json_llm_response(text): | |
text = "{"+"{".join([xi.strip() for xi in text.split("{")[1:]]) | |
text = "}".join([xi.strip() for xi in text.split("}")[:-1]])+"}" | |
return eval(text) | |
def extract_python_dict_from_response(text): | |
""" | |
LLM response formatted as python dicts. | |
Args: |
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
#-- Find index of max sharpe portfolio | |
max_sharpe_index = np.argmax(mean_variance_pairs[:,0]/mean_variance_pairs[:,1]**0.5) | |
#-- Add max sharpe portfolio as a seperate point to the plot | |
fig.add_trace(go.Scatter(x=mean_variance_pairs[max_sharpe_index,1]**0.5, y=mean_variance_pairs[max_sharpe_index,0], | |
marker=dict(color='red', size=14), | |
mode='markers')) |
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
# -- Create the figure in Plotly | |
fig = px.scatter( | |
filtered_df, | |
x="gdpPercap", | |
y="lifeExp", | |
size="pop", | |
color="continent", | |
hover_name="country", | |
log_x=log_x_choice, | |
size_max=60, |
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
# -- Read in the data | |
df = px.data.gapminder() | |
# -- Apply the year filter given by the user | |
filtered_df = df[(df.year == year_choice)] | |
# -- Apply the continent filter | |
if continent_choice != "All": | |
filtered_df = filtered_df[filtered_df.continent == continent_choice] |
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
# -- Get the user input | |
year_col, continent_col, log_x_col = st.columns([5, 5, 5]) | |
with year_col: | |
year_choice = st.slider( | |
"What year would you like to examine?", | |
min_value=1952, | |
max_value=2007, | |
step=5, | |
value=2007, | |
) |
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
# -- Create three columns | |
col1, col2, col3 = st.columns([5, 5, 20]) | |
# -- Put the image in the middle column | |
# - Commented out here so that the file will run without having the image downloaded | |
# with col2: | |
# st.image("streamlit.png", width=200) | |
# -- Put the title in the last column | |
with col3: | |
st.title("Streamlit Demo") | |
# -- We use the first column here as a dummy to add a space to the left |
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
import streamlit as st | |
import plotly.express as px |
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
import streamlit as st | |
import plotly.express as px | |
st.set_page_config(layout="wide") | |
# -- Create three columns | |
col1, col2, col3 = st.columns([5, 5, 20]) | |
# -- Put the image in the middle column | |
# - Commented out here so that the file will run without having the image downloaded | |
# with col2: |
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
x = [dt.datetime.date(d) for d in df.index] | |
fig = plt.figure(figsize=(10,5)) | |
plt.title('Walmart Quarterly Earnings') | |
plt.ylabel('Earnings (Billions)') | |
plt.grid(True) | |
plt.plot(x[:-len(predictions)], | |
df.Earnings[:-len(predictions)], | |
"b-") | |
plt.plot(x[-len(predictions):], | |
df.Earnings[-len(predictions):], |
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
model.eval() | |
with torch.no_grad(): | |
predictions, _ = model(train_scaled[-train_periods:], None) | |
#-- Apply inverse transform to undo scaling | |
predictions = scaler.inverse_transform(np.array(predictions.reshape(-1,1))) |
NewerOlder