# Weight Optimization

`Tutorials` are the best documentation — [<mark style="color:blue;">`Weight Optimization Tutorial`</mark>](https://colab.research.google.com/github/sovai-research/sovai-public/blob/main/notebooks/studies/Weight%20Optimization.ipynb)

### Key Features

* Multiple optimization strategies
* Comprehensive performance analysis
* Risk-adjusted return metrics
* Portfolio composition visualization
* Drawdown and contribution analysis
* Correlation and clustering analysis
* Daily weight tracking

### Usage

To use the Weight Optimization module, you first need to prepare your dataset. Here's an example of how to set up and run the optimization:

<pre class="language-python"><code class="lang-python">import sovai as sov

# Authenticate
sov.token_auth(token="your_authentication_token")

# Prepare your data
df_price = sov.data("market/closeadj")
df_mega = df_price.select_stocks("mega").date_range("2000-01-01")
df_returns = df_mega.calculate_returns().dropna(axis=1, how="any")

# Select the most uncorrelated stocks
feature_importance = df_returns.importance()
df_select = df_returns[feature_importance["feature"].head(25)]

# Run weight optimization
<a data-footnote-ref href="#user-content-fn-1">portfolio</a> = df_select.weight_optimization()
</code></pre>

## Overall Portfolio Analysis

### **Sharpe Ratio Distribution**

Shows the distribution of Sharpe ratios across different strategies, helping to understand the consistency of risk-adjusted returns.

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>.sharpe_plot
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-667729561e73bab1f2ebea6d57449b855d38a099%2Fweight_optimization_1.png?alt=media" alt=""><figcaption></figcaption></figure>

### **Cumulative Returns Plot**

Displays the cumulative returns of all portfolio strategies over time, allowing for easy comparison of overall performance.

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>.return_plot
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-03654b370457f4c025d86e8579c0b63c45cdc9ec%2Fweight_optimization_2.png?alt=media" alt=""><figcaption></figcaption></figure>

### **Overall Composition Plot**

Illustrates the asset allocation of all strategies, allowing for a comparison of how different models allocate capital.

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>.composition_plot
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-bbdfd55445129a08d5789f8d6db32185ff7e9531%2Fweight_optimization_3.png?alt=media" alt=""><figcaption></figcaption></figure>

### **Best Performing Model**

Identifies the strategy that performed best according to the Sharpe ratio.

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>.best_model
</code></pre>

```
'NCO'
```

### **Performance Summary**

Provides a comprehensive summary of key performance metrics for all strategies, including returns, volatility, Sharpe ratio, and more.

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>.performance_report
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-15c61ee566afe59a47c5ebd6ffa63af4f1fafe51%2Fweight_optimization_4.png?alt=media" alt=""><figcaption></figcaption></figure>

## Model-Specific Analysis

For model-specific analysis, replace "model\_name" with the actual model name (e.g., HRP, HERC, NCO, or EQUAL).

### **Cumulative Returns**

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>["model_name"].backtest_plot
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-a8ebc47539194c25849929dcebe422a5f59fd029%2Fweight_optimization_5.png?alt=media" alt=""><figcaption></figcaption></figure>

Displays the cumulative returns of the specific model over the backtesting period.

### **Backtest Report**

Detailed performance statistics from the backtesting period for the specific model.

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>["model_name"].backtest_report
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-a85206682419f887bb944a9f68f3852d196a2bfe%2Fweight_optimization_6.png?alt=media" alt=""><figcaption></figcaption></figure>

### **Rolling Sharpe Ratio**

Visualizes how the Sharpe ratio of the model changes over time, indicating consistency of performance.

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>["model_name"].sharpe_rolling_plot
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-54eabb81f2b02d716a435ce03c2b627661536808%2Fweight_optimization_7.png?alt=media" alt=""><figcaption></figcaption></figure>

### **Model Composition**

Illustrates the asset allocation for the specific model.

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>["model_name"].composition_plot
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-590a37286110e1b86f42fec88e95e19def0b3bf9%2Fweight_optimization_8.png?alt=media" alt=""><figcaption></figcaption></figure>

### **Drawdown Contribution**

Shows which assets contribute most to the portfolio's drawdowns, helping identify risk sources.

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>["model_name"].drawdown_contribution_plot
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-b1f6635b97d8dd129a573eebb4cff16d57b14385%2Fweight_optimization_9.png?alt=media" alt=""><figcaption></figcaption></figure>

### **Sharpe Ratio Contribution**

Indicates which assets contribute most to the portfolio's Sharpe ratio, highlighting return drivers.

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>["model_name"].sharpe_contribution_plot
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-2664d30b95877388913448c6275c5b4625b53874%2Fweight_optimization_10.png?alt=media" alt=""><figcaption></figcaption></figure>

### **Correlation Heatmap**

Displays the correlation structure of assets used in the model (not available for EQUAL).

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>["model_name"].heatmap_plot
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-c76679a358f904b2d3ec3e5c89fbb5c17bd5e471%2Fweight_optimization_11.png?alt=media" alt=""><figcaption></figcaption></figure>

### **Clustering Dendrogram**

Visualizes the hierarchical clustering of assets used in the model (not available for EQUAL).

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>["model_name"].cluster_plot
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-163443fdafac99414a454ca46599261538e17309%2Fweight_optimization_12.png?alt=media" alt=""><figcaption></figcaption></figure>

### **Current Recommended Allocation**

Provides the model's most recent recommended asset allocation.

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>["model_name"].recommended_allocation
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-91e7c82525ae144d9b18d6510b51f119f6e6dd89%2Fweight_optimization_13.png?alt=media" alt=""><figcaption></figcaption></figure>

### **Sharpe Ratio Distribution**

Shows the distribution of Sharpe ratio helping to understand the consistency of risk-adjusted returns.

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>["model_name"].recommended_allocation
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-3cc6f9da7460ead176c682b46a1ffe4011a87300%2Fweight_optimization_14.png?alt=media" alt=""><figcaption></figcaption></figure>

### **Daily Weights**

Shows how the model's asset allocation changes day-by-day over the backtesting period.

<pre class="language-python"><code class="lang-python"><a data-footnote-ref href="#user-content-fn-1">portfolio</a>["model_name"].daily_weights
</code></pre>

<figure><img src="https://1304136543-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCbqQ4ogM0YiEs5Z9Djdn%2Fuploads%2Fgit-blob-ac4e2b4f52732fc9ea56960d426e91df69f0f7f6%2Fweight_optimization_15.png?alt=media" alt=""><figcaption></figcaption></figure>

[^1]: class module
