python-synthetica 0.1.2__tar.gz → 0.1.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {python_synthetica-0.1.2 → python_synthetica-0.1.4}/PKG-INFO +116 -8
- python_synthetica-0.1.4/README.md +251 -0
- {python_synthetica-0.1.2 → python_synthetica-0.1.4}/pyproject.toml +1 -2
- {python_synthetica-0.1.2 → python_synthetica-0.1.4}/synthetica/base.py +64 -59
- {python_synthetica-0.1.2 → python_synthetica-0.1.4}/synthetica/models.py +1 -1
- python_synthetica-0.1.2/README.md +0 -142
- {python_synthetica-0.1.2 → python_synthetica-0.1.4}/LICENSE +0 -0
- {python_synthetica-0.1.2 → python_synthetica-0.1.4}/synthetica/__init__.py +0 -0
- {python_synthetica-0.1.2 → python_synthetica-0.1.4}/synthetica/config.py +0 -0
- {python_synthetica-0.1.2 → python_synthetica-0.1.4}/synthetica/decorators.py +0 -0
- {python_synthetica-0.1.2 → python_synthetica-0.1.4}/synthetica/stats.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: python-synthetica
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.4
|
|
4
4
|
Summary: Generate synthetic time series data.
|
|
5
5
|
Home-page: https://github.com/ActurialCapital/synthetica
|
|
6
6
|
License: BSD 3-Clause License
|
|
@@ -13,7 +13,6 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
13
13
|
Requires-Dist: numpy (>=1.26.4,<2.0.0)
|
|
14
14
|
Requires-Dist: pandas (>=2.2.2,<3.0.0)
|
|
15
15
|
Requires-Dist: scipy (>=1.13.1,<2.0.0)
|
|
16
|
-
Requires-Dist: statsmodels (>=0.14.2,<0.15.0)
|
|
17
16
|
Project-URL: Repository, https://github.com/ActurialCapital/synthetica
|
|
18
17
|
Description-Content-Type: text/markdown
|
|
19
18
|
|
|
@@ -62,12 +61,12 @@ Description-Content-Type: text/markdown
|
|
|
62
61
|
The project latest version incorporates a wide array of models, offering an extensive toolkit for generating synthetic time series data. This version includes features like:
|
|
63
62
|
|
|
64
63
|
* `GeometricBrownianMotion`
|
|
65
|
-
* `
|
|
66
|
-
* `NARMA`
|
|
64
|
+
* `AutoRegressive`
|
|
65
|
+
* `NARMA`
|
|
67
66
|
* `Heston`
|
|
68
|
-
* `CIR`
|
|
67
|
+
* `CIR`
|
|
69
68
|
* `LevyStable`
|
|
70
|
-
* `MeanReverting`
|
|
69
|
+
* `MeanReverting`
|
|
71
70
|
* `Merton`
|
|
72
71
|
* `Poisson`
|
|
73
72
|
* `Seasonal`
|
|
@@ -128,13 +127,122 @@ In this example, we are using the following parameters for illustration purposes
|
|
|
128
127
|
>>> model.transform(matrix) # Produces highly positively correlated features
|
|
129
128
|
```
|
|
130
129
|
|
|
131
|
-
|
|
132
|
-
|
|
133
130
|
<p align="center"><img src="https://github.com/ActurialCapital/synthetica/blob/main/docs/static/gbm_corr_transform.png" alt="chart-2" width="75%" height="75%"></p>
|
|
134
131
|
|
|
135
132
|
|
|
136
133
|
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
137
134
|
|
|
135
|
+
## Notes
|
|
136
|
+
|
|
137
|
+
### Cholesky Decomposition
|
|
138
|
+
|
|
139
|
+
The Cholesky transformation (or Cholesky decomposition) is a mathematical technique used to decompose a positive definite matrix into the product of a lower triangular matrix and its transpose. This is particularly useful in various fields such as numerical analysis, optimization, and financial modeling.
|
|
140
|
+
|
|
141
|
+
#### Mathematical Definition
|
|
142
|
+
|
|
143
|
+
Given a positive definite matrix $\(A\)$, the Cholesky decomposition is a factorization such that:
|
|
144
|
+
$\[ A = L L^T \]$
|
|
145
|
+
where:
|
|
146
|
+
- $\(A\)$ is a positive definite matrix.
|
|
147
|
+
- $\(L\)$ is a lower triangular matrix.
|
|
148
|
+
- $\(L^T\)$ is the transpose of $\(L\)$.
|
|
149
|
+
|
|
150
|
+
### Applications
|
|
151
|
+
|
|
152
|
+
1. **Numerical Stability**: The Cholesky decomposition is more numerically stable than other decomposition methods for positive definite matrices.
|
|
153
|
+
2. **Solving Linear Systems**: It is used to solve linear systems of equations efficiently.
|
|
154
|
+
3. **Simulating Correlated Random Variables**: In finance and statistics, it is used to generate correlated random variables from uncorrelated ones.
|
|
155
|
+
|
|
156
|
+
### Implementation
|
|
157
|
+
|
|
158
|
+
In the context of synthetic data generation, the Cholesky transformation can be used to apply a correlation structure to a set of uncorrelated random variables.
|
|
159
|
+
|
|
160
|
+
### Example Implementation in Python
|
|
161
|
+
|
|
162
|
+
Here's a simple example of how to implement and use the Cholesky transformation in Python:
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
import numpy as np
|
|
166
|
+
|
|
167
|
+
# Generate a positive definite matrix (covariance matrix)
|
|
168
|
+
def generate_positive_definite_matrix(size):
|
|
169
|
+
A = np.random.rand(size, size)
|
|
170
|
+
return np.dot(A, A.T) + size * np.eye(size)
|
|
171
|
+
|
|
172
|
+
# Apply Cholesky decomposition
|
|
173
|
+
def cholesky_transform(data, cov_matrix):
|
|
174
|
+
# Perform Cholesky decomposition
|
|
175
|
+
L = np.linalg.cholesky(cov_matrix)
|
|
176
|
+
# Transform the data
|
|
177
|
+
transformed_data = np.dot(data, L.T)
|
|
178
|
+
return transformed_data
|
|
179
|
+
|
|
180
|
+
# Example usage
|
|
181
|
+
np.random.seed(42)
|
|
182
|
+
size = 5 # Size of the covariance matrix
|
|
183
|
+
cov_matrix = generate_positive_definite_matrix(size)
|
|
184
|
+
|
|
185
|
+
# Generate synthetic data (uncorrelated random variables)
|
|
186
|
+
data = np.random.randn(100, size)
|
|
187
|
+
|
|
188
|
+
# Apply Cholesky transformation to impose the correlation structure
|
|
189
|
+
transformed_data = cholesky_transform(data, cov_matrix)
|
|
190
|
+
|
|
191
|
+
print("Original Data:\n", data[:5])
|
|
192
|
+
print("Transformed Data:\n", transformed_data[:5])
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Explanation
|
|
196
|
+
|
|
197
|
+
1. **generate_positive_definite_matrix**:
|
|
198
|
+
- Generates a random positive definite matrix. This can be a covariance matrix in practical scenarios.
|
|
199
|
+
|
|
200
|
+
2. **cholesky_transform**:
|
|
201
|
+
- Performs Cholesky decomposition on the covariance matrix to obtain the lower triangular matrix \(L\).
|
|
202
|
+
- Transforms the original uncorrelated data by multiplying it with \(L^T\) to introduce the desired correlation structure.
|
|
203
|
+
|
|
204
|
+
3. **Example Usage**:
|
|
205
|
+
- Generates synthetic uncorrelated data.
|
|
206
|
+
- Applies the Cholesky transformation to this data using the generated covariance matrix.
|
|
207
|
+
- The transformed data now exhibits the correlation structure defined by the covariance matrix.
|
|
208
|
+
|
|
209
|
+
### In the Context of the `LevyStable` Class
|
|
210
|
+
|
|
211
|
+
The `cholesky_transform` method in the `LevyStable` class would be used to apply a correlation structure to the generated synthetic data. This would allow for the generation of more realistic synthetic data that incorporates correlations between different paths or time series.
|
|
212
|
+
|
|
213
|
+
Here is how you could integrate and test the `cholesky_transform` method in the `LevyStable` class:
|
|
214
|
+
|
|
215
|
+
### Positive Definiteness
|
|
216
|
+
|
|
217
|
+
#### What positive definite means in a covariance matrix
|
|
218
|
+
|
|
219
|
+
A covariance matrix is considered positive definite if it satisfies the following key properties:
|
|
220
|
+
|
|
221
|
+
1. It is symmetric, meaning the matrix is equal to its transpose.
|
|
222
|
+
2. For any non-zero vector $x$, $x^T * C * x > 0$, where $C$ is the covariance matrix and $x^T$ is the transpose of $x$.
|
|
223
|
+
3. All of its eigenvalues are strictly positive.
|
|
224
|
+
|
|
225
|
+
Positive definiteness in a covariance matrix has important implications:
|
|
226
|
+
|
|
227
|
+
1. It ensures the matrix is invertible, which is crucial for many [statistical techniques](https://stats.stackexchange.com/questions/52976/is-a-sample-covariance-matrix-always-symmetric-and-positive-definite).
|
|
228
|
+
2. It guarantees that the matrix represents a [valid probability distribution](https://statproofbook.github.io/P/covmat-psd.html).
|
|
229
|
+
3. It allows for unique solutions in [optimization problems](https://gowrishankar.info/blog/why-covariance-matrix-should-be-positive-semi-definite-tests-using-breast-cancer-dataset/) and ensures the stability of certain algorithms.
|
|
230
|
+
4. It indicates that no linear combination of the variables has zero variance, meaning all variables contribute [meaningful information](https://math.stackexchange.com/questions/114072/what-is-the-proof-that-covariance-matrices-are-always-semi-definite).
|
|
231
|
+
|
|
232
|
+
A covariance matrix that is positive semi-definite (allowing for eigenvalues to be non-negative rather than strictly positive) is still valid, but may indicate linear dependencies among variables.
|
|
233
|
+
|
|
234
|
+
In practice, sample covariance matrices are often positive definite if the number of observations exceeds the number of variables and there are no perfect linear relationships among the variables.
|
|
235
|
+
|
|
236
|
+
#### Implementation
|
|
237
|
+
|
|
238
|
+
`synthetica` automatically finds the nearest positive-definite matrix to input using `nearest_positive_definite` python function. it is directly sourced from [Computing a nearest symmetric positive semidefinite matrix](https://doi.org/10.1016/0024-3795(88)90223-6).
|
|
239
|
+
|
|
240
|
+
#### Other Sources
|
|
241
|
+
|
|
242
|
+
* [MatLab](https://www.mathworks.com/matlabcentral/fileexchange/42885-nearestspd)
|
|
243
|
+
* [StackOverflow](https://stackoverflow.com/questions/43238173/python-convert-matrix-to-positive-semi-definite)
|
|
244
|
+
* [Gist](https://gist.github.com/fasiha/fdb5cec2054e6f1c6ae35476045a0bbd)
|
|
245
|
+
|
|
138
246
|
<!-- CONTRIBUTING -->
|
|
139
247
|
## Contributing
|
|
140
248
|
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
<a name="readme-top"></a>
|
|
2
|
+
|
|
3
|
+
<!-- PROJECT LOGO -->
|
|
4
|
+
<p align="center"><img src="https://github.com/ActurialCapital/synthetica/blob/main/docs/static/logo.png" alt="logo" width="90%" height="90%"></p>
|
|
5
|
+
|
|
6
|
+
| Overview | |
|
|
7
|
+
|---|---|
|
|
8
|
+
| **Open Source** | [](https://github.com/ActurialCapital/synthetica/blob/main/LICENSE) |
|
|
9
|
+
| **Code** | [](https://pypi.org/project/python-synthetica/) [](https://www.python.org/) |
|
|
10
|
+
| **CI/CD** | [](https://codecov.io/gh/ActurialCapital/synthetica) |
|
|
11
|
+
| **Downloads** |   [)](https://pepy.tech/project/python-synthetica) |
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
<!-- TABLE OF CONTENTS -->
|
|
15
|
+
<details>
|
|
16
|
+
<summary>Table of Contents</summary>
|
|
17
|
+
<ol>
|
|
18
|
+
<li>
|
|
19
|
+
<a href="#about-the-project">About The Project</a>
|
|
20
|
+
<ul>
|
|
21
|
+
<li><a href="#introduction">Introduction</a></li>
|
|
22
|
+
</ul>
|
|
23
|
+
<ul>
|
|
24
|
+
<li><a href="#built-with">Built With</a></li>
|
|
25
|
+
</ul>
|
|
26
|
+
</li>
|
|
27
|
+
<li><a href="#installation">Installation</a></li>
|
|
28
|
+
<li><a href="#getting-started">Getting Started</a></li>
|
|
29
|
+
<li><a href="#contributing">Contributing</a></li>
|
|
30
|
+
<li><a href="#license">License</a></li>
|
|
31
|
+
</ol>
|
|
32
|
+
</details>
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
<!-- ABOUT THE PROJECT -->
|
|
37
|
+
## About The Project
|
|
38
|
+
|
|
39
|
+
### Introduction
|
|
40
|
+
|
|
41
|
+
`Synthetica` is a versatile and robust tool for generating synthetic time series data. Whether you are engaged in financial modeling, IoT data simulation, or any project requiring realistic time series data to create correlated or uncorrelated signals, `Synthetica` provides high-quality, customizable generated datasets. Leveraging advanced statistical techniques and machine learning algorithms, `Synthetica` produces synthetic data that closely replicates the characteristics and patterns of real-world data.
|
|
42
|
+
|
|
43
|
+
The project latest version incorporates a wide array of models, offering an extensive toolkit for generating synthetic time series data. This version includes features like:
|
|
44
|
+
|
|
45
|
+
* `GeometricBrownianMotion`
|
|
46
|
+
* `AutoRegressive`
|
|
47
|
+
* `NARMA`
|
|
48
|
+
* `Heston`
|
|
49
|
+
* `CIR`
|
|
50
|
+
* `LevyStable`
|
|
51
|
+
* `MeanReverting`
|
|
52
|
+
* `Merton`
|
|
53
|
+
* `Poisson`
|
|
54
|
+
* `Seasonal`
|
|
55
|
+
|
|
56
|
+
However, the `SyntheticaAdvenced` version elevates the capabilities further, integrating more sophisticated deep learning data-driven algorithms, such as `TimeGAN`.
|
|
57
|
+
|
|
58
|
+
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
59
|
+
|
|
60
|
+
### Built With
|
|
61
|
+
|
|
62
|
+
* `numpy = "^1.26.4"`
|
|
63
|
+
* `pandas = "^2.2.2"`
|
|
64
|
+
* `scipy = "^1.13.1"`
|
|
65
|
+
|
|
66
|
+
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
67
|
+
|
|
68
|
+
<!-- GETTING STARTED -->
|
|
69
|
+
## Installation
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
$ pip install python-synthetica
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
76
|
+
|
|
77
|
+
<!-- USAGE EXAMPLES -->
|
|
78
|
+
## Getting Started
|
|
79
|
+
|
|
80
|
+
Once you have cloned the repository, you can start using `Synthetica` to generate synthetic time series data. Here are some initial steps to help you kickstart your exploration:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
>>> import synthetica as sth
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
In this example, we are using the following parameters for illustration purposes:
|
|
87
|
+
|
|
88
|
+
* `length=252`: The length of the time series
|
|
89
|
+
* `num_paths=5`: The number of paths to generate
|
|
90
|
+
* `seed=123`: Reseed the `numpy` singleton `RandomState` instance for reproduction
|
|
91
|
+
|
|
92
|
+
**Initialize the model**: Using the `GeometricBrownianMotion` (GBM) model: This approach initializes the model with a specified path length, number of paths, and a fixed random seed:
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
>>> model = sth.GeometricBrownianMotion(length=252, num_paths=5, seed=123)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
**Generate random signals**: The transform method then generates the random signals accordingly:
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
>>> model.transform() # Generate random signals
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
<p align="center"><img src="https://github.com/ActurialCapital/synthetica/blob/main/docs/static/gbm_random_transform.png" alt="chart-1" width="75%" height="75%"></p>
|
|
105
|
+
|
|
106
|
+
**Generate correlated paths**: This process ensures that the resulting features are highly positively correlated, leveraging the Cholesky decomposition method to achieve the desired `matrix` correlation structure:
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
>>> model.transform(matrix) # Produces highly positively correlated features
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
<p align="center"><img src="https://github.com/ActurialCapital/synthetica/blob/main/docs/static/gbm_corr_transform.png" alt="chart-2" width="75%" height="75%"></p>
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
116
|
+
|
|
117
|
+
## Notes
|
|
118
|
+
|
|
119
|
+
### Cholesky Decomposition
|
|
120
|
+
|
|
121
|
+
The Cholesky transformation (or Cholesky decomposition) is a mathematical technique used to decompose a positive definite matrix into the product of a lower triangular matrix and its transpose. This is particularly useful in various fields such as numerical analysis, optimization, and financial modeling.
|
|
122
|
+
|
|
123
|
+
#### Mathematical Definition
|
|
124
|
+
|
|
125
|
+
Given a positive definite matrix $\(A\)$, the Cholesky decomposition is a factorization such that:
|
|
126
|
+
$\[ A = L L^T \]$
|
|
127
|
+
where:
|
|
128
|
+
- $\(A\)$ is a positive definite matrix.
|
|
129
|
+
- $\(L\)$ is a lower triangular matrix.
|
|
130
|
+
- $\(L^T\)$ is the transpose of $\(L\)$.
|
|
131
|
+
|
|
132
|
+
### Applications
|
|
133
|
+
|
|
134
|
+
1. **Numerical Stability**: The Cholesky decomposition is more numerically stable than other decomposition methods for positive definite matrices.
|
|
135
|
+
2. **Solving Linear Systems**: It is used to solve linear systems of equations efficiently.
|
|
136
|
+
3. **Simulating Correlated Random Variables**: In finance and statistics, it is used to generate correlated random variables from uncorrelated ones.
|
|
137
|
+
|
|
138
|
+
### Implementation
|
|
139
|
+
|
|
140
|
+
In the context of synthetic data generation, the Cholesky transformation can be used to apply a correlation structure to a set of uncorrelated random variables.
|
|
141
|
+
|
|
142
|
+
### Example Implementation in Python
|
|
143
|
+
|
|
144
|
+
Here's a simple example of how to implement and use the Cholesky transformation in Python:
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
import numpy as np
|
|
148
|
+
|
|
149
|
+
# Generate a positive definite matrix (covariance matrix)
|
|
150
|
+
def generate_positive_definite_matrix(size):
|
|
151
|
+
A = np.random.rand(size, size)
|
|
152
|
+
return np.dot(A, A.T) + size * np.eye(size)
|
|
153
|
+
|
|
154
|
+
# Apply Cholesky decomposition
|
|
155
|
+
def cholesky_transform(data, cov_matrix):
|
|
156
|
+
# Perform Cholesky decomposition
|
|
157
|
+
L = np.linalg.cholesky(cov_matrix)
|
|
158
|
+
# Transform the data
|
|
159
|
+
transformed_data = np.dot(data, L.T)
|
|
160
|
+
return transformed_data
|
|
161
|
+
|
|
162
|
+
# Example usage
|
|
163
|
+
np.random.seed(42)
|
|
164
|
+
size = 5 # Size of the covariance matrix
|
|
165
|
+
cov_matrix = generate_positive_definite_matrix(size)
|
|
166
|
+
|
|
167
|
+
# Generate synthetic data (uncorrelated random variables)
|
|
168
|
+
data = np.random.randn(100, size)
|
|
169
|
+
|
|
170
|
+
# Apply Cholesky transformation to impose the correlation structure
|
|
171
|
+
transformed_data = cholesky_transform(data, cov_matrix)
|
|
172
|
+
|
|
173
|
+
print("Original Data:\n", data[:5])
|
|
174
|
+
print("Transformed Data:\n", transformed_data[:5])
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Explanation
|
|
178
|
+
|
|
179
|
+
1. **generate_positive_definite_matrix**:
|
|
180
|
+
- Generates a random positive definite matrix. This can be a covariance matrix in practical scenarios.
|
|
181
|
+
|
|
182
|
+
2. **cholesky_transform**:
|
|
183
|
+
- Performs Cholesky decomposition on the covariance matrix to obtain the lower triangular matrix \(L\).
|
|
184
|
+
- Transforms the original uncorrelated data by multiplying it with \(L^T\) to introduce the desired correlation structure.
|
|
185
|
+
|
|
186
|
+
3. **Example Usage**:
|
|
187
|
+
- Generates synthetic uncorrelated data.
|
|
188
|
+
- Applies the Cholesky transformation to this data using the generated covariance matrix.
|
|
189
|
+
- The transformed data now exhibits the correlation structure defined by the covariance matrix.
|
|
190
|
+
|
|
191
|
+
### In the Context of the `LevyStable` Class
|
|
192
|
+
|
|
193
|
+
The `cholesky_transform` method in the `LevyStable` class would be used to apply a correlation structure to the generated synthetic data. This would allow for the generation of more realistic synthetic data that incorporates correlations between different paths or time series.
|
|
194
|
+
|
|
195
|
+
Here is how you could integrate and test the `cholesky_transform` method in the `LevyStable` class:
|
|
196
|
+
|
|
197
|
+
### Positive Definiteness
|
|
198
|
+
|
|
199
|
+
#### What positive definite means in a covariance matrix
|
|
200
|
+
|
|
201
|
+
A covariance matrix is considered positive definite if it satisfies the following key properties:
|
|
202
|
+
|
|
203
|
+
1. It is symmetric, meaning the matrix is equal to its transpose.
|
|
204
|
+
2. For any non-zero vector $x$, $x^T * C * x > 0$, where $C$ is the covariance matrix and $x^T$ is the transpose of $x$.
|
|
205
|
+
3. All of its eigenvalues are strictly positive.
|
|
206
|
+
|
|
207
|
+
Positive definiteness in a covariance matrix has important implications:
|
|
208
|
+
|
|
209
|
+
1. It ensures the matrix is invertible, which is crucial for many [statistical techniques](https://stats.stackexchange.com/questions/52976/is-a-sample-covariance-matrix-always-symmetric-and-positive-definite).
|
|
210
|
+
2. It guarantees that the matrix represents a [valid probability distribution](https://statproofbook.github.io/P/covmat-psd.html).
|
|
211
|
+
3. It allows for unique solutions in [optimization problems](https://gowrishankar.info/blog/why-covariance-matrix-should-be-positive-semi-definite-tests-using-breast-cancer-dataset/) and ensures the stability of certain algorithms.
|
|
212
|
+
4. It indicates that no linear combination of the variables has zero variance, meaning all variables contribute [meaningful information](https://math.stackexchange.com/questions/114072/what-is-the-proof-that-covariance-matrices-are-always-semi-definite).
|
|
213
|
+
|
|
214
|
+
A covariance matrix that is positive semi-definite (allowing for eigenvalues to be non-negative rather than strictly positive) is still valid, but may indicate linear dependencies among variables.
|
|
215
|
+
|
|
216
|
+
In practice, sample covariance matrices are often positive definite if the number of observations exceeds the number of variables and there are no perfect linear relationships among the variables.
|
|
217
|
+
|
|
218
|
+
#### Implementation
|
|
219
|
+
|
|
220
|
+
`synthetica` automatically finds the nearest positive-definite matrix to input using `nearest_positive_definite` python function. it is directly sourced from [Computing a nearest symmetric positive semidefinite matrix](https://doi.org/10.1016/0024-3795(88)90223-6).
|
|
221
|
+
|
|
222
|
+
#### Other Sources
|
|
223
|
+
|
|
224
|
+
* [MatLab](https://www.mathworks.com/matlabcentral/fileexchange/42885-nearestspd)
|
|
225
|
+
* [StackOverflow](https://stackoverflow.com/questions/43238173/python-convert-matrix-to-positive-semi-definite)
|
|
226
|
+
* [Gist](https://gist.github.com/fasiha/fdb5cec2054e6f1c6ae35476045a0bbd)
|
|
227
|
+
|
|
228
|
+
<!-- CONTRIBUTING -->
|
|
229
|
+
## Contributing
|
|
230
|
+
|
|
231
|
+
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.
|
|
232
|
+
|
|
233
|
+
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
|
|
234
|
+
Don't forget to give the project a star! Thanks again!
|
|
235
|
+
|
|
236
|
+
1. Fork the Project
|
|
237
|
+
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
|
|
238
|
+
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
|
|
239
|
+
4. Push to the Branch (`git push origin feature/AmazingFeature`)
|
|
240
|
+
5. Open a Pull Request
|
|
241
|
+
|
|
242
|
+
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
<!-- LICENSE -->
|
|
246
|
+
## License
|
|
247
|
+
|
|
248
|
+
Distributed under the BSD-3 License. See `LICENSE.txt` for more information.
|
|
249
|
+
|
|
250
|
+
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
251
|
+
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "python-synthetica"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.4"
|
|
4
4
|
description = "Generate synthetic time series data."
|
|
5
5
|
authors = ["ActurialCapital"]
|
|
6
6
|
license = "BSD 3-Clause License"
|
|
@@ -13,7 +13,6 @@ python = "^3.11"
|
|
|
13
13
|
numpy = "^1.26.4"
|
|
14
14
|
pandas = "^2.2.2"
|
|
15
15
|
scipy = "^1.13.1"
|
|
16
|
-
statsmodels = "^0.14.2"
|
|
17
16
|
|
|
18
17
|
[build-system]
|
|
19
18
|
requires = ["poetry-core"]
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from abc import ABC, abstractmethod
|
|
2
|
-
from typing import Iterable
|
|
2
|
+
from typing import Iterable, Optional
|
|
3
3
|
from functools import cached_property
|
|
4
4
|
import numpy as np
|
|
5
5
|
import pandas as pd
|
|
@@ -35,21 +35,20 @@ class BaseSynthetic(ABC):
|
|
|
35
35
|
|
|
36
36
|
def __init__(
|
|
37
37
|
self,
|
|
38
|
-
length: int | pd.DatetimeIndex = 252,
|
|
39
|
-
num_paths: int = 1,
|
|
40
|
-
mean: float = 0,
|
|
41
|
-
delta: float = 1/252,
|
|
42
|
-
sigma: float = 0.125,
|
|
43
|
-
freq: str = 'D',
|
|
44
|
-
seed: int = None
|
|
38
|
+
length: Optional[int | pd.DatetimeIndex] = 252,
|
|
39
|
+
num_paths: Optional[int] = 1,
|
|
40
|
+
mean: Optional[float] = 0,
|
|
41
|
+
delta: Optional[float] = 1/252,
|
|
42
|
+
sigma: Optional[float] = 0.125,
|
|
43
|
+
freq: Optional[str] = 'D',
|
|
44
|
+
seed: Optional[int] = None
|
|
45
45
|
):
|
|
46
46
|
# Generic
|
|
47
47
|
self._length = length
|
|
48
48
|
self._freq = freq
|
|
49
49
|
self._num_paths = num_paths
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
self._seed = seed
|
|
51
|
+
|
|
53
52
|
# White noise params
|
|
54
53
|
self._mean = mean
|
|
55
54
|
self._delta = delta
|
|
@@ -106,6 +105,20 @@ class BaseSynthetic(ABC):
|
|
|
106
105
|
size=(self.length, self.num_paths)
|
|
107
106
|
)
|
|
108
107
|
|
|
108
|
+
@property
|
|
109
|
+
def seed(self) -> int:
|
|
110
|
+
if self._seed is not None:
|
|
111
|
+
np.random.seed(self._seed)
|
|
112
|
+
return self._seed
|
|
113
|
+
|
|
114
|
+
@seed.setter
|
|
115
|
+
@sth.callback('white_noise')
|
|
116
|
+
def seed(self, value: int) -> float:
|
|
117
|
+
"""Random seed value update"""
|
|
118
|
+
self._seed = value
|
|
119
|
+
if self._seed is not None:
|
|
120
|
+
np.random.seed(self._seed)
|
|
121
|
+
|
|
109
122
|
@property
|
|
110
123
|
def mean(self) -> float:
|
|
111
124
|
"""Mean value"""
|
|
@@ -113,7 +126,7 @@ class BaseSynthetic(ABC):
|
|
|
113
126
|
|
|
114
127
|
@mean.setter
|
|
115
128
|
@sth.callback('white_noise')
|
|
116
|
-
def mean(self, value) -> float:
|
|
129
|
+
def mean(self, value: float) -> float:
|
|
117
130
|
"""Mean value update"""
|
|
118
131
|
if value != self._mean:
|
|
119
132
|
self._mean = value
|
|
@@ -125,7 +138,7 @@ class BaseSynthetic(ABC):
|
|
|
125
138
|
|
|
126
139
|
@delta.setter
|
|
127
140
|
@sth.callback('white_noise')
|
|
128
|
-
def delta(self, value) -> float:
|
|
141
|
+
def delta(self, value: float) -> float:
|
|
129
142
|
"""Delta value update"""
|
|
130
143
|
if value != self._delta:
|
|
131
144
|
self._delta = value
|
|
@@ -137,7 +150,7 @@ class BaseSynthetic(ABC):
|
|
|
137
150
|
|
|
138
151
|
@sigma.setter
|
|
139
152
|
@sth.callback('white_noise')
|
|
140
|
-
def sigma(self, value) -> float:
|
|
153
|
+
def sigma(self, value: float) -> float:
|
|
141
154
|
"""Sigma value update"""
|
|
142
155
|
if value != self._sigma:
|
|
143
156
|
self._sigma = value
|
|
@@ -184,20 +197,22 @@ class BaseSynthetic(ABC):
|
|
|
184
197
|
|
|
185
198
|
# #### Cholesky #### #
|
|
186
199
|
|
|
187
|
-
|
|
200
|
+
@staticmethod
|
|
201
|
+
def cholesky_transform(rvs: np.array, matrix: np.array) -> np.ndarray:
|
|
188
202
|
"""
|
|
189
|
-
|
|
190
|
-
Cholesky decomposition method.
|
|
203
|
+
Perform Cholesky transformation on random variables.
|
|
191
204
|
|
|
192
205
|
Parameters
|
|
193
206
|
----------
|
|
194
|
-
|
|
195
|
-
|
|
207
|
+
rvs : np.array
|
|
208
|
+
Random variables to transform.
|
|
209
|
+
matrix : np.array
|
|
210
|
+
The matrix for Cholesky decomposition.
|
|
196
211
|
|
|
197
212
|
Returns
|
|
198
213
|
-------
|
|
199
|
-
|
|
200
|
-
|
|
214
|
+
np.ndarray
|
|
215
|
+
Transformed random variables.
|
|
201
216
|
"""
|
|
202
217
|
try:
|
|
203
218
|
decomposition = np.linalg.cholesky(matrix)
|
|
@@ -206,53 +221,43 @@ class BaseSynthetic(ABC):
|
|
|
206
221
|
updated_matrix = sth.nearest_positive_definite(matrix)
|
|
207
222
|
decomposition = np.linalg.cholesky(updated_matrix)
|
|
208
223
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
# Construct uncorrelated paths to convert into correlated paths
|
|
212
|
-
uncorrelated_paths = [
|
|
213
|
-
np.array(
|
|
214
|
-
# Uncorrelated random numbers
|
|
215
|
-
[np.random.normal(0, sqrt_delta_sigma)
|
|
216
|
-
for _ in range(self.num_paths)]
|
|
217
|
-
)
|
|
218
|
-
for _ in range(self.length + 1)
|
|
219
|
-
]
|
|
220
|
-
|
|
221
|
-
uncorrelated_matrix = np.matrix(uncorrelated_paths)
|
|
222
|
-
correlated_matrix = uncorrelated_matrix * decomposition
|
|
223
|
-
extracted_paths = [[] for _ in range(1, self.num_paths + 1)]
|
|
224
|
-
for j in range(0, len(correlated_matrix) * self.num_paths - self.num_paths, self.num_paths):
|
|
225
|
-
for i in range(self.num_paths):
|
|
226
|
-
extracted_paths[i].append(correlated_matrix.item(j + i))
|
|
227
|
-
|
|
228
|
-
return self.to_pandas(np.array(extracted_paths).T)
|
|
224
|
+
# return (decomposition @ rvs.T).T
|
|
225
|
+
return np.matmul(decomposition, rvs.T).T
|
|
229
226
|
|
|
230
|
-
def
|
|
227
|
+
def create_corr_returns(self, matrix: np.ndarray | pd.DataFrame) -> pd.Series | pd.DataFrame:
|
|
231
228
|
"""
|
|
232
|
-
|
|
229
|
+
This method can construct a basket of correlated asset paths using the
|
|
230
|
+
Cholesky decomposition method.
|
|
233
231
|
|
|
234
232
|
Parameters
|
|
235
233
|
----------
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
matrix : np.array
|
|
239
|
-
The matrix for Cholesky decomposition.
|
|
234
|
+
matrix : pd.DataFrame or np.array
|
|
235
|
+
The matrix applied in Cholesky decomposition.
|
|
240
236
|
|
|
241
237
|
Returns
|
|
242
238
|
-------
|
|
243
|
-
|
|
244
|
-
|
|
239
|
+
pd.Series or pd.DataFrame:
|
|
240
|
+
Data representing correlated log returns.
|
|
245
241
|
"""
|
|
246
|
-
|
|
247
|
-
decomposition = np.linalg.cholesky(matrix)
|
|
242
|
+
self.seed # Regenerate seed
|
|
248
243
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
244
|
+
# Construct uncorrelated paths to convert into correlated paths
|
|
245
|
+
rvs = np.random.normal(
|
|
246
|
+
loc=0,
|
|
247
|
+
scale=np.sqrt(self.delta) * self.sigma,
|
|
248
|
+
size=(self.length + 1, self.num_paths)
|
|
249
|
+
)
|
|
250
|
+
rvs_matrix = np.matrix(rvs)
|
|
253
251
|
|
|
254
|
-
|
|
255
|
-
|
|
252
|
+
correlated_matrix = self.cholesky_transform(rvs_matrix, matrix)
|
|
253
|
+
|
|
254
|
+
extracted_paths = [[] for _ in range(1, self.num_paths + 1)]
|
|
255
|
+
for j in range(0, len(correlated_matrix) * self.num_paths - self.num_paths, self.num_paths):
|
|
256
|
+
for i in range(self.num_paths):
|
|
257
|
+
extracted_paths[i].append(correlated_matrix.item(j + i))
|
|
258
|
+
|
|
259
|
+
output = np.array(extracted_paths).T
|
|
260
|
+
return self.to_pandas(output)
|
|
256
261
|
|
|
257
262
|
# #### Converter #### #
|
|
258
263
|
|
|
@@ -276,7 +281,7 @@ class BaseSynthetic(ABC):
|
|
|
276
281
|
def to_prices(
|
|
277
282
|
self,
|
|
278
283
|
log_returns: Iterable,
|
|
279
|
-
start_value: float = 100.0
|
|
284
|
+
start_value: Optional[float] = 100.0
|
|
280
285
|
) -> np.ndarray:
|
|
281
286
|
"""
|
|
282
287
|
This method converts a sequence of log returns into normal returns
|
|
@@ -302,7 +307,7 @@ class BaseSynthetic(ABC):
|
|
|
302
307
|
# A sequence of prices starting with start_value
|
|
303
308
|
return returns * start_value
|
|
304
309
|
|
|
305
|
-
def to_pandas(self, output) -> pd.Series | pd.DataFrame:
|
|
310
|
+
def to_pandas(self, output: np.ndarray) -> pd.Series | pd.DataFrame:
|
|
306
311
|
"""
|
|
307
312
|
Convert synthetic output to a pandas DataFrame or Series.
|
|
308
313
|
|
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
<a name="readme-top"></a>
|
|
2
|
-
|
|
3
|
-
<!-- PROJECT LOGO -->
|
|
4
|
-
<p align="center"><img src="https://github.com/ActurialCapital/synthetica/blob/main/docs/static/logo.png" alt="logo" width="90%" height="90%"></p>
|
|
5
|
-
|
|
6
|
-
| Overview | |
|
|
7
|
-
|---|---|
|
|
8
|
-
| **Open Source** | [](https://github.com/ActurialCapital/synthetica/blob/main/LICENSE) |
|
|
9
|
-
| **Code** | [](https://pypi.org/project/python-synthetica/) [](https://www.python.org/) |
|
|
10
|
-
| **CI/CD** | [](https://codecov.io/gh/ActurialCapital/synthetica) |
|
|
11
|
-
| **Downloads** |   [)](https://pepy.tech/project/python-synthetica) |
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
<!-- TABLE OF CONTENTS -->
|
|
15
|
-
<details>
|
|
16
|
-
<summary>Table of Contents</summary>
|
|
17
|
-
<ol>
|
|
18
|
-
<li>
|
|
19
|
-
<a href="#about-the-project">About The Project</a>
|
|
20
|
-
<ul>
|
|
21
|
-
<li><a href="#introduction">Introduction</a></li>
|
|
22
|
-
</ul>
|
|
23
|
-
<ul>
|
|
24
|
-
<li><a href="#built-with">Built With</a></li>
|
|
25
|
-
</ul>
|
|
26
|
-
</li>
|
|
27
|
-
<li><a href="#installation">Installation</a></li>
|
|
28
|
-
<li><a href="#getting-started">Getting Started</a></li>
|
|
29
|
-
<li><a href="#contributing">Contributing</a></li>
|
|
30
|
-
<li><a href="#license">License</a></li>
|
|
31
|
-
</ol>
|
|
32
|
-
</details>
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
<!-- ABOUT THE PROJECT -->
|
|
37
|
-
## About The Project
|
|
38
|
-
|
|
39
|
-
### Introduction
|
|
40
|
-
|
|
41
|
-
`Synthetica` is a versatile and robust tool for generating synthetic time series data. Whether you are engaged in financial modeling, IoT data simulation, or any project requiring realistic time series data to create correlated or uncorrelated signals, `Synthetica` provides high-quality, customizable generated datasets. Leveraging advanced statistical techniques and machine learning algorithms, `Synthetica` produces synthetic data that closely replicates the characteristics and patterns of real-world data.
|
|
42
|
-
|
|
43
|
-
The project latest version incorporates a wide array of models, offering an extensive toolkit for generating synthetic time series data. This version includes features like:
|
|
44
|
-
|
|
45
|
-
* `GeometricBrownianMotion`
|
|
46
|
-
* `AR` (Auto Regressive)
|
|
47
|
-
* `NARMA` (Non-Linear Auto Regressive Moving Average)
|
|
48
|
-
* `Heston`
|
|
49
|
-
* `CIR` (Cox–Ingersoll–Ross)
|
|
50
|
-
* `LevyStable`
|
|
51
|
-
* `MeanReverting` (Ornstein–Uhlenbeck)
|
|
52
|
-
* `Merton`
|
|
53
|
-
* `Poisson`
|
|
54
|
-
* `Seasonal`
|
|
55
|
-
|
|
56
|
-
However, the `SyntheticaAdvenced` version elevates the capabilities further, integrating more sophisticated deep learning data-driven algorithms, such as `TimeGAN`.
|
|
57
|
-
|
|
58
|
-
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
59
|
-
|
|
60
|
-
### Built With
|
|
61
|
-
|
|
62
|
-
* `numpy = "^1.26.4"`
|
|
63
|
-
* `pandas = "^2.2.2"`
|
|
64
|
-
* `scipy = "^1.13.1"`
|
|
65
|
-
|
|
66
|
-
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
67
|
-
|
|
68
|
-
<!-- GETTING STARTED -->
|
|
69
|
-
## Installation
|
|
70
|
-
|
|
71
|
-
```sh
|
|
72
|
-
$ pip install python-synthetica
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
76
|
-
|
|
77
|
-
<!-- USAGE EXAMPLES -->
|
|
78
|
-
## Getting Started
|
|
79
|
-
|
|
80
|
-
Once you have cloned the repository, you can start using `Synthetica` to generate synthetic time series data. Here are some initial steps to help you kickstart your exploration:
|
|
81
|
-
|
|
82
|
-
```python
|
|
83
|
-
>>> import synthetica as sth
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
In this example, we are using the following parameters for illustration purposes:
|
|
87
|
-
|
|
88
|
-
* `length=252`: The length of the time series
|
|
89
|
-
* `num_paths=5`: The number of paths to generate
|
|
90
|
-
* `seed=123`: Reseed the `numpy` singleton `RandomState` instance for reproduction
|
|
91
|
-
|
|
92
|
-
**Initialize the model**: Using the `GeometricBrownianMotion` (GBM) model: This approach initializes the model with a specified path length, number of paths, and a fixed random seed:
|
|
93
|
-
|
|
94
|
-
```python
|
|
95
|
-
>>> model = sth.GeometricBrownianMotion(length=252, num_paths=5, seed=123)
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
**Generate random signals**: The transform method then generates the random signals accordingly:
|
|
99
|
-
|
|
100
|
-
```python
|
|
101
|
-
>>> model.transform() # Generate random signals
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
<p align="center"><img src="https://github.com/ActurialCapital/synthetica/blob/main/docs/static/gbm_random_transform.png" alt="chart-1" width="75%" height="75%"></p>
|
|
105
|
-
|
|
106
|
-
**Generate correlated paths**: This process ensures that the resulting features are highly positively correlated, leveraging the Cholesky decomposition method to achieve the desired `matrix` correlation structure:
|
|
107
|
-
|
|
108
|
-
```python
|
|
109
|
-
>>> model.transform(matrix) # Produces highly positively correlated features
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
<p align="center"><img src="https://github.com/ActurialCapital/synthetica/blob/main/docs/static/gbm_corr_transform.png" alt="chart-2" width="75%" height="75%"></p>
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
118
|
-
|
|
119
|
-
<!-- CONTRIBUTING -->
|
|
120
|
-
## Contributing
|
|
121
|
-
|
|
122
|
-
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.
|
|
123
|
-
|
|
124
|
-
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
|
|
125
|
-
Don't forget to give the project a star! Thanks again!
|
|
126
|
-
|
|
127
|
-
1. Fork the Project
|
|
128
|
-
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
|
|
129
|
-
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
|
|
130
|
-
4. Push to the Branch (`git push origin feature/AmazingFeature`)
|
|
131
|
-
5. Open a Pull Request
|
|
132
|
-
|
|
133
|
-
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
<!-- LICENSE -->
|
|
137
|
-
## License
|
|
138
|
-
|
|
139
|
-
Distributed under the BSD-3 License. See `LICENSE.txt` for more information.
|
|
140
|
-
|
|
141
|
-
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
|
142
|
-
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|