# Time Segmentation

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

### Overview

The Time Segmentation Module is a powerful tool for analyzing financial time series data. It offers four main functionalities:

1. Change Point Detection
2. Regime Change Analysis
3. Comprehensive Regime Analysis (PCA-based)

Each functionality can be used for data analysis or visualization, allowing users to gain deep insights into their financial data.

### Getting Started

To use the Time Segmentation Module, first import the necessary library and authenticate:

```python
import sovai as sov
sov.token_auth(token="your_token_here")
```

```python
df_accounting = sov.data("accounting/weekly").select_stocks("mega")
```

### 1. Change Point Detection

Identify significant changes in your time series data.

#### Data Analysis

```python
df_change = df_accounting.change_point(method='data', feature="book_equity_value")
df_change.tail(10)  # View the last 10 rows
df_change.attrs['stats']  # View additional statistics
```

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

#### Visualization

```python
df_accounting.change_point(method='plot')
```

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

### 2. Regime Change Analysis

Detect regime changes for a specific stock and feature.

#### Data Analysis

```python
rc_result = df_accounting.regime_change(method="data", ticker="AAPL", feature="total_revenue")
rc_result.tail(10)  # View the last 10 rows
rc_result.attrs['stats']  # View additional statistics
```

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

#### Visualization

```python
df_accounting.regime_change(method="plot", ticker="AAPL", feature="total_revenue")
```

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

### 3. Comprehensive Regime Analysis (PCA-based)

Perform a PCA-based regime change analysis on multiple features for a specific stock.

#### Data Analysis

```python
pca_rc_result = df_accounting.pca_regime_change(method="data", ticker="AAPL")
pca_rc_result.tail()  # View the last rows
pca_rc_result.attrs['stats']  # View additional statistics
```

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

#### Visualization

```python
df_accounting.pca_regime_change(method="plot", ticker="AAPL")
```

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

### Tips for Users

* Always check the `.attrs['stats']` of the result for additional insights and metadata.
* Use the 'plot' method for quick visual analysis and the 'data' method for detailed numerical results.
* Experiment with different features and tickers to gain comprehensive insights into your financial data.

***
