Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update .pre-commit-config.yaml #86

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v5.0.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: fix-encoding-pragma

- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.286'
rev: 'v0.8.4'
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
args: [ --fix, --exit-non-zero-on-fix, --ignore=F821]

- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.35.0
rev: v0.43.0
hooks:
- id: markdownlint-fix
# MD013: line too long
args: [--disable, MD013, "--"]

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.26.3
rev: 0.30.0
hooks:
- id: check-dependabot
args: ["--verbose"]
Expand Down
4,896 changes: 4,658 additions & 238 deletions book/docs/applications/notebooks/portfolio_optimization.ipynb

Large diffs are not rendered by default.

33 changes: 16 additions & 17 deletions book/docs/exercises/notebooks/13.20.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,43 @@
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"$$\n",
"\\rho_i = \\frac{\\partial \\log R(x)}{\\partial \\log x_i} =\n",
"\\frac{\\partial R(x)}{R(x)} \\frac{x_i}{\\partial x_i}, \\quad i=1, \\ldots, n.\n",
"$$"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Thus $\\rho_i$ gives the fractional increase in risk per fractional increase\n",
"in investment $i$.\n",
"We can express the risk contributions as"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"$$\n",
"\\rho_i = \\frac{x_i (\\Sigma x)_i} {x^T\\Sigma x}, \\quad i=1, \\ldots, n,\n",
"$$"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"from which we see that $\\sum_{i=1}^n \\rho_i = 1$.\n",
"For general $x$, we can have $\\rho_i <0$, which means that a small increase\n",
Expand All @@ -88,10 +91,7 @@
"\n",
"*Hint.*\n",
"Minimize $(1/2)x^T\\Sigma x - \\sum_{i=1}^n \\rho_i^\\mathrm{des} \\log x_i$."
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "markdown",
Expand All @@ -118,7 +118,6 @@
"outputs": [],
"source": [
"import numpy as np\n",
"import cvxpy as cp\n",
"\n",
"Sigma = np.array(\n",
" np.matrix(\n",
Expand Down
82 changes: 59 additions & 23 deletions book/docs/exercises/notebooks/13.21.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"source": [
"$$\n",
" \\begin{array}{ll}\n",
Expand All @@ -43,13 +49,16 @@
" & \\mathbb{1}^Tw = 1,\n",
" \\end{array}\n",
"$$"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"source": [
"where $w\\in \\mathbb{R}^n$ is the variable, $\\mu$ is the mean return,\n",
"$\\Sigma\\in \\mathbb{S}_{++}^n$ is the return covariance, and $\\gamma>0$ is the\n",
Expand All @@ -60,13 +69,16 @@
"portfolio by buying and selling assets.\n",
"We call the post-trade portfolio weights $\\tilde{w}_t$.\n",
"They are found by solving the (rebalancing) problem"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"source": [
"$$\n",
" \\begin{array}{ll}\n",
Expand All @@ -75,13 +87,16 @@
" & \\mathbb{1}^Tw = 1,\n",
" \\end{array}\n",
"$$"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"source": [
"with variable $w \\in \\mathbb{R}^n$,\n",
"where $\\kappa\\in\\mathbb{R}^n_+$ is the vector of (so-called linear) transaction costs\n",
Expand Down Expand Up @@ -112,10 +127,7 @@
"The standard model is that $r_t$ are IID random variables with\n",
"mean and covariance $\\mu$ and $\\Sigma$,\n",
"but this is not relevant in this problem."
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -166,16 +178,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"outputs": [
{
"ename": "NameError",
"evalue": "name 'ws' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[1], line 38\u001b[0m\n\u001b[1;32m 36\u001b[0m plt\u001b[38;5;241m.\u001b[39mfigure(figsize\u001b[38;5;241m=\u001b[39m(\u001b[38;5;241m13\u001b[39m, \u001b[38;5;241m5\u001b[39m))\n\u001b[1;32m 37\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m j \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(n):\n\u001b[0;32m---> 38\u001b[0m plt\u001b[38;5;241m.\u001b[39mplot(\u001b[38;5;28mrange\u001b[39m(T), \u001b[43mws\u001b[49m[:, j], colors[j])\n\u001b[1;32m 39\u001b[0m plt\u001b[38;5;241m.\u001b[39mplot(\u001b[38;5;28mrange\u001b[39m(T), [w_star[j]] \u001b[38;5;241m*\u001b[39m T, colors[j] \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m--\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 40\u001b[0m non_zero_trades \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mabs\u001b[39m(us[:, j]) \u001b[38;5;241m>\u001b[39m threshold\n",
"\u001b[0;31mNameError\u001b[0m: name 'ws' is not defined"
]
},
{
"data": {
"text/plain": [
"<Figure size 1300x500 with 0 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# data and code for multiperiod portfolio rebalancing problem\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"T = 100\n",
"n = 5\n",
Expand All @@ -196,7 +230,7 @@
"\n",
"## Generate returns\n",
"# call this function to generate a vector r of market returns\n",
"generateReturns = lambda: np.random.multivariate_normal(mu, Sigma)\n",
"# generateReturns = np.random.multivariate_normal(mu, Sigma)\n",
"\n",
"## Plotting code\n",
"# You must provide three objects:\n",
Expand All @@ -206,7 +240,6 @@
"# the trades at each period: w_t_tilde - w_t;\n",
"# - w_star: np.array of size n,\n",
"# the \"target\" solution w_star.\n",
"import matplotlib.pyplot as plt\n",
"\n",
"colors = [\"b\", \"r\", \"g\", \"c\", \"m\"]\n",
"plt.figure(figsize=(13, 5))\n",
Expand All @@ -224,6 +257,9 @@
"execution_count": null,
"metadata": {
"collapsed": true,
"jupyter": {
"outputs_hidden": true
},
"pycharm": {
"name": "#%%\n"
}
Expand All @@ -234,7 +270,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -248,9 +284,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
20 changes: 12 additions & 8 deletions book/docs/exercises/notebooks/17.3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"source": [
"Flux balance analysis is based on a very simple model of\n",
"the reactions going on in a cell, keeping track only of the gross rate of consumption and production\n",
Expand Down Expand Up @@ -50,10 +56,7 @@
"$$\n",
"Sv = 0, \\quad v \\succeq 0, \\quad v \\preceq v^\\mathrm{max}.\n",
"$$"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -175,13 +178,14 @@
},
"outputs": [],
"source": [
"# flake8: noqa: F821\n",
"# Check essential genes and synthetic lethals\n",
"G = np.zeros((n, n))\n",
"for i in range(n):\n",
" for j in range(i, n):\n",
" # Taking out genes i and j (i can equal j)\n",
" # TODO: your code here\n",
"\n",
" # TODO: replace pass with your code here, \n",
" pass\n",
"\n",
"print(G < 0.2*Gstar)"
]
Expand All @@ -203,9 +207,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
16 changes: 9 additions & 7 deletions book/docs/exercises/notebooks/3.20.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"source": [
"A vehicle (say, an airplane) travels along a fixed path of $n$\n",
"segments, between $n+1$ waypoints labeled $0, \\ldots, n$. Segment $i$ starts at waypoint $i-1$ and\n",
Expand All @@ -28,10 +34,7 @@
"You are given the data $d$ (segment travel distances), $s^\\mathrm{min}$ and $s^\\mathrm{max}$ (speed bounds), $\\tau^\\mathrm{min}$ and $\\tau^\\mathrm{max}$\n",
"(waypoint arrival time bounds), and the fuel use function $\\Phi: \\mathbf{R} \\rightarrow \\mathbf{R}$. You are to choose the speeds\n",
"$s_1, \\ldots, s_n$ so as to minimize the total fuel consumed in kg."
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -618,7 +621,6 @@
},
"outputs": [],
"source": [
"import cvxpy as cp\n",
"\n",
"# TODO: your code here\n",
"# store the optimal speeds in s\n"
Expand Down Expand Up @@ -660,9 +662,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 1
"nbformat_minor": 4
}
Loading
Loading