diff --git a/riskfolio/src/AuxFunctions.py b/riskfolio/src/AuxFunctions.py index aa0b3130..a348aa1c 100644 --- a/riskfolio/src/AuxFunctions.py +++ b/riskfolio/src/AuxFunctions.py @@ -772,7 +772,7 @@ def std_silhouette_score(dist, clusters, max_k=10): for k in range(2, min(len(cluster_lvls.columns), max_k)): level = cluster_lvls.iloc[:, k] # get k clusters b = silhouette_samples(dist, level) - scores_list.append(b.mean()/b.std()) + scores_list.append(b.mean() / b.std()) scores_list = pd.Series(scores_list) n = dist.shape[0] diff --git a/riskfolio/src/ConstraintsFunctions.py b/riskfolio/src/ConstraintsFunctions.py index 4d4833f9..81d07f83 100644 --- a/riskfolio/src/ConstraintsFunctions.py +++ b/riskfolio/src/ConstraintsFunctions.py @@ -838,7 +838,7 @@ def assets_clusters( elif opt_k_method == "stdsil": k = af.std_silhouette_score(dist, clustering, max_k) else: - raise ValueError('The only opt_k_method available are twodiff and stdsil') + raise ValueError("The only opt_k_method available are twodiff and stdsil") # Building clusters clusters_inds = hr.fcluster(clustering, k, criterion="maxclust") diff --git a/riskfolio/src/HCPortfolio.py b/riskfolio/src/HCPortfolio.py index 917194ed..3672ec80 100644 --- a/riskfolio/src/HCPortfolio.py +++ b/riskfolio/src/HCPortfolio.py @@ -291,7 +291,9 @@ def _hierarchical_clustering( elif opt_k_method == "stdsil": k = af.std_silhouette_score(dist, clustering, max_k) else: - raise ValueError('The only opt_k_method available are twodiff and stdsil') + raise ValueError( + "The only opt_k_method available are twodiff and stdsil" + ) else: k = None diff --git a/riskfolio/src/PlotFunctions.py b/riskfolio/src/PlotFunctions.py index 7acc3951..c4c20861 100644 --- a/riskfolio/src/PlotFunctions.py +++ b/riskfolio/src/PlotFunctions.py @@ -2712,7 +2712,7 @@ def plot_clusters( elif opt_k_method == "stdsil": k = af.std_silhouette_score(dist, clustering, max_k) else: - raise ValueError('The only opt_k_method available are twodiff and stdsil') + raise ValueError("The only opt_k_method available are twodiff and stdsil") clustering_inds = hr.fcluster(clustering, k, criterion="maxclust") clusters = {i: [] for i in range(min(clustering_inds), max(clustering_inds) + 1)} @@ -2743,7 +2743,7 @@ def plot_clusters( for cluster_id, cluster in clusters.items(): amin = permutation.index(cluster[0]) xmin, xmax = amin, amin + len(cluster) - ymin, ymax =amin, amin + len(cluster) + ymin, ymax = amin, amin + len(cluster) for i in cluster: a = permutation.index(i) @@ -2753,10 +2753,18 @@ def plot_clusters( amin = a ax.axvline( - x=xmin, ymin=(N - ymin) / dim, ymax=(N - ymax) / dim, linewidth=4, color=linecolor + x=xmin, + ymin=(N - ymin) / dim, + ymax=(N - ymax) / dim, + linewidth=4, + color=linecolor, ) ax.axvline( - x=xmax, ymin=(N - ymin) / dim, ymax=(N - ymax) / dim, linewidth=4, color=linecolor + x=xmax, + ymin=(N - ymin) / dim, + ymax=(N - ymax) / dim, + linewidth=4, + color=linecolor, ) ax.axhline( y=ymin, xmin=xmin / dim, xmax=xmax / dim, linewidth=4, color=linecolor @@ -3064,14 +3072,16 @@ def plot_dendrogram( if show_clusters is False: color_threshold = 0 elif show_clusters is True: - # optimal number of clusters + # optimal number of clusters if k is None: if opt_k_method == "twodiff": k = af.two_diff_gap_stat(dist, clustering, max_k) elif opt_k_method == "stdsil": k = af.std_silhouette_score(dist, clustering, max_k) else: - raise ValueError('The only opt_k_method available are twodiff and stdsil') + raise ValueError( + "The only opt_k_method available are twodiff and stdsil" + ) root, nodes = hr.to_tree(clustering, rd=True) nodes = [i.dist for i in nodes] @@ -3336,7 +3346,7 @@ def plot_network( elif opt_k_method == "stdsil": k = af.std_silhouette_score(dist, clustering, max_k) else: - raise ValueError('The only opt_k_method available are twodiff and stdsil') + raise ValueError("The only opt_k_method available are twodiff and stdsil") clustering_inds = hr.fcluster(clustering, k, criterion="maxclust") clusters = {i: [] for i in range(min(clustering_inds), max(clustering_inds) + 1)} @@ -3906,7 +3916,7 @@ def plot_clusters_network( elif opt_k_method == "stdsil": k = af.std_silhouette_score(dist, clustering, max_k) else: - raise ValueError('The only opt_k_method available are twodiff and stdsil') + raise ValueError("The only opt_k_method available are twodiff and stdsil") clustering_inds = hr.fcluster(clustering, k, criterion="maxclust") clusters = {i: [] for i in range(min(clustering_inds), max(clustering_inds) + 1)} @@ -4183,7 +4193,7 @@ def plot_clusters_network_allocation( elif opt_k_method == "stdsil": k = af.std_silhouette_score(dist, clustering, max_k) else: - raise ValueError('The only opt_k_method available are twodiff and stdsil') + raise ValueError("The only opt_k_method available are twodiff and stdsil") clustering_inds = hr.fcluster(clustering, k, criterion="maxclust") clusters = {i: [] for i in range(min(clustering_inds), max(clustering_inds) + 1)}