Skip to content

Commit

Permalink
rearranging stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
tschm committed Jun 21, 2023
1 parent 498250a commit f8707c5
Show file tree
Hide file tree
Showing 21 changed files with 76 additions and 109 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.idea

.DS_Store

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
8 changes: 4 additions & 4 deletions book/docs/notebooks/demo.ipynb

Large diffs are not rendered by default.

113 changes: 48 additions & 65 deletions book/docs/notebooks/factormodel.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@
"import numpy as np\n",
"import cvxpy as cvx\n",
"\n",
"from cvx.risk.linalg import pca\n",
"from cvx.linalg import pca\n",
"from cvx.risk.factor import FactorModel"
]
},
{
"cell_type": "markdown",
"id": "72f447e8-635d-4ff0-b4ec-d2c856078ccd",
"id": "7a6b8122",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"pycharm": {
"name": "#%% md\n"
}
Expand All @@ -53,9 +57,13 @@
},
{
"cell_type": "code",
"execution_count": 2,
"id": "feafbc51-f23e-4435-ae09-7d207fcd7136",
"execution_count": null,
"id": "a22b009a",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"pycharm": {
"name": "#%%\n"
}
Expand All @@ -68,8 +76,12 @@
},
{
"cell_type": "markdown",
"id": "093b0aed-c5ca-4d1e-8259-70224c64bb35",
"id": "00c84f55",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"pycharm": {
"name": "#%% md\n"
}
Expand All @@ -80,13 +92,16 @@
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f9e3e0a1-d16d-4976-81ed-4ec3815cb5ee",
"execution_count": null,
"id": "54e6f9d1",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"pycharm": {
"name": "#%%\n"
},
"tags": []
}
},
"outputs": [],
"source": [
Expand All @@ -95,8 +110,12 @@
},
{
"cell_type": "markdown",
"id": "0d3c82c4-beed-4e76-b7c8-4e005afe1abc",
"id": "8e79d788",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"pycharm": {
"name": "#%% md\n"
}
Expand All @@ -107,25 +126,18 @@
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9f2c55ae-3a66-4791-a1f0-8acaa1b37623",
"execution_count": null,
"id": "bc5e8afd",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0.00923407730537884"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"model = FactorModel(assets=len(returns.columns), k=10)\n",
"\n",
Expand All @@ -139,8 +151,12 @@
},
{
"cell_type": "markdown",
"id": "c1bebdb0-7197-4823-8530-e4da3a03f91f",
"id": "85da2abd",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"pycharm": {
"name": "#%% md\n"
}
Expand All @@ -151,43 +167,18 @@
},
{
"cell_type": "code",
"execution_count": 8,
"id": "2dd75849-e600-4f26-bbac-e63d06eb1335",
"execution_count": null,
"id": "7e17daf0",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"GOOG 0.000841\n",
"AAPL 0.000463\n",
"FB 0.000462\n",
"BABA 0.003246\n",
"AMZN 0.000594\n",
"GE 0.003120\n",
"AMD 0.143065\n",
"WMT 0.001252\n",
"BAC 0.001290\n",
"GM 0.000356\n",
"T 0.000359\n",
"UAA 0.186590\n",
"SHLD 0.534772\n",
"XOM 0.000519\n",
"RRC 0.036929\n",
"BBY 0.082946\n",
"MA 0.000675\n",
"PFE 0.000576\n",
"JPM 0.001600\n",
"SBUX 0.000344\n",
"dtype: float64\n",
"0.000170523449914986\n"
]
}
],
"outputs": [],
"source": [
"w = cvx.Variable(20)\n",
"y = cvx.Variable(10)\n",
Expand All @@ -205,14 +196,6 @@
"assert np.isclose(w.value.sum(), 1.0)\n",
"assert np.all(w.value > -0.01)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f9097c1b-e07c-4049-b57e-16a26c311c4d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
26 changes: 3 additions & 23 deletions book/docs/notebooks/large.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
{
"cell_type": "code",
"execution_count": 6,
"id": "327acf27",
"id": "8321b700",
"metadata": {
"collapsed": false,
"jupyter": {
Expand All @@ -142,7 +142,7 @@
{
"cell_type": "code",
"execution_count": 7,
"id": "048557ff",
"id": "0fbd6deb",
"metadata": {
"collapsed": false,
"jupyter": {
Expand All @@ -157,7 +157,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"495 ms ± 71 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
"440 ms ± 85.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
Expand All @@ -174,26 +174,6 @@
" \n",
" problem.solve(ignore_dpp=True)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c534a9db",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
},
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# check the solution\n",
"assert np.isclose(w.value.sum(), 1.0)\n",
"assert np.all(w.value > -0.01)"
]
}
],
"metadata": {
Expand Down
12 changes: 6 additions & 6 deletions book/docs/notebooks/sample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
"import cvxpy as cp\n",
"\n",
"from cvx.risk.sample import SampleCovariance\n",
"from cvx.risk.random import rand_cov\n",
"from cvx.random import rand_cov\n",
"\n",
"n = 6"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"metadata": {
"collapsed": false,
"jupyter": {
Expand All @@ -67,7 +67,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"metadata": {
"collapsed": false,
"jupyter": {
Expand All @@ -82,7 +82,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"358 ms ± 15.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
"342 ms ± 13.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
Expand All @@ -100,7 +100,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 4,
"metadata": {
"collapsed": false,
"jupyter": {
Expand All @@ -115,7 +115,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"4.13 s ± 53.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
"3.92 s ± 51.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion cvx/risk/factor/factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import cvxpy as cvx
import numpy as np

from cvx.risk.linalg import cholesky
from cvx.linalg import cholesky
from cvx.risk.model import Bounds
from cvx.risk.model import RiskModel

Expand Down
6 changes: 4 additions & 2 deletions cvx/risk/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ def estimate(self, weights, **kwargs):
Estimate the variance given the portfolio weights
"""

@abstractmethod
def update(self, **kwargs):
"""
Update the data in the risk model
"""

@abstractmethod
def constraints(self, weights, **kwargs):
"""
Return the constraints for the risk model
Expand All @@ -38,12 +40,12 @@ def constraints(self, weights, **kwargs):

@dataclass
class Bounds(RiskModel):
m: int = 0

def estimate(self, weights, **kwargs):
"""No estimation for bounds"""
raise NotImplementedError("No estimation for bounds")

m: int = 0

def __post_init__(self):
self.parameter["lower"] = cp.Parameter(
shape=self.m,
Expand Down
2 changes: 1 addition & 1 deletion cvx/risk/sample/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import cvxpy as cvx
import numpy as np

from cvx.risk.linalg import cholesky
from cvx.linalg import cholesky
from cvx.risk.model import Bounds
from cvx.risk.model import RiskModel

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import numpy as np

from cvx.risk.linalg import cholesky
from cvx.risk.random import rand_cov
from cvx.linalg import cholesky
from cvx.random import rand_cov


def test_cholesky():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pandas as pd
import pytest

from cvx.risk.linalg import pca
from cvx.linalg import pca


@pytest.fixture()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
import pytest

from cvx.risk.linalg import valid
from cvx.linalg import valid


def test_valid():
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import numpy as np

from cvx.risk.random import rand_cov
from cvx.random import rand_cov


def test_rand_cov():
Expand Down
Loading

0 comments on commit f8707c5

Please sign in to comment.