Skip to content

Commit

Permalink
Format update
Browse files Browse the repository at this point in the history
  • Loading branch information
dcajasn committed May 12, 2024
1 parent 39a0e02 commit 79654c3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion riskfolio/src/AuxFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion riskfolio/src/ConstraintsFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion riskfolio/src/HCPortfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 19 additions & 9 deletions riskfolio/src/PlotFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -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)}
Expand Down

0 comments on commit 79654c3

Please sign in to comment.