<jupyter_start><jupyter_text>NetworkX<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

import datetime as dt
from sklearn import covariance
import networkx as nx
from pylab import rcParams

import warnings
warnings.filterwarnings("ignore")

# yahoo finance is used to fetch data 
import yfinance as yf
yf.pdr_override()
# input
symbols = ['AMD','AAPL','MSFT','GS', 'AMZN', 'GOOGL', 'TSLA', 'INTC']
start = '2016-01-01'
end = '2022-01-01'


# Read data 
df = yf.download(symbols,start,end)['Adj Close']

# View Columns
df.head()
num_of_years = 10
start = dt.datetime.now() - dt.timedelta(int(365.25 * num_of_years))
end = dt.datetime.now()
df = np.log1p(df.pct_change()).iloc[1:]

# Glasso algorithm
edge_model = covariance.GraphicalLassoCV(cv=10)
df /= df.std(axis=0)
df = df.dropna()
df.head()
edge_model.fit(df)
p = edge_model.precision_
rcParams['figure.figsize'] = 15,10
sns.heatmap(p)
plt.show()
p = pd.DataFrame(p)
links = p.stack().reset_index()
links.columns = ['var1', 'var2','value']
links=links.loc[(abs(links['value']) > 0.17) &  (links['var1'] != links['var2'])]
G=nx.from_pandas_edgelist(links,'var1','var2', create_using=nx.Graph())
pos = nx.spring_layout(G, k=0.2*1/np.sqrt(len(G.nodes())), iterations=20)
plt.figure(3, figsize=(15, 15))
nx.draw(G, pos=pos)
nx.draw_networkx_labels(G, pos=pos)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17329545454545456, 'fraction_numerical': 0.03622159090909091, 'mean_word_length': 6.045, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '245062', 'license': 'MIT', 'n_tokens_mistral': 611, 'n_tokens_neox': 569, 'n_words': 117, 'path': 'Stock_Algorithms/NetworkX_Part2.ipynb', 'repo_name': 'stungkit/Deep-Learning-Machine-Learning-Stock', 'size': '1408'}