Taller Clustering acciones S&P 500#

Aplicar los tres métodos de clustering estudiados y, con cada uno, determinar los mejores hiperparámetros para obtener los mejores clusters.

import yfinance as yf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Obtener la lista de tickers del S&P 500 desde Wikipedia
url = "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
table = pd.read_html(url)
tickers_df = table[0]
tickers = tickers_df["Symbol"].tolist()
# Descargar datos históricos para cada ticker
data = {}
for ticker in tickers:
    data[ticker] = yf.download(ticker, start="2023-06-30", end="2024-07-01")

# Crear un DataFrame que contenga los precios de cierre de todas las acciones
closing_prices = pd.DataFrame({ticker: data[ticker]["Close"] for ticker in tickers})
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed

1 Failed download:
- BRK.B: No data found, symbol may be delisted
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed

1 Failed download:
- BF.B: No data found for this date range, symbol may be delisted
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed

1 Failed download:
- SW: Data doesn't exist for startDate = 1688101200, endDate = 1719810000
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
[*******************100%*********************]  1 of 1 completed
# Calcular características (media y desviación estándar del retorno diario)
features = []
for ticker in tickers:
    df = data[ticker]
    df["Rendimiento diario"] = df["Adj Close"].pct_change()
    mean_return = df["Rendimiento diario"].mean()
    std_return = df["Rendimiento diario"].std()
    features.append([ticker, mean_return, std_return])

# Crear un DataFrame con las características
features_df = pd.DataFrame(
    features, columns=["Ticker", "Media rendimientos", "Desviación estándar"]
)

# Graficar los datos
plt.figure(figsize=(10, 6))
plt.scatter(
    features_df["Media rendimientos"], features_df["Desviación estándar"], color="blue"
)

# Añadir etiquetas a cada punto
for i in range(features_df.shape[0]):
    plt.text(
        features_df["Media rendimientos"][i],
        features_df["Desviación estándar"][i],
        features_df["Ticker"][i],
    )

plt.xlabel("Media rendimientos")
plt.ylabel("esvación estándar")
plt.title("Media rendimientos vs. Desviación estándar")
plt.grid(True)
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
../../../_images/output_5_12.png