From 2a11910a4a8abf1c9edd8e7d1541e58e17e051f9 Mon Sep 17 00:00:00 2001 From: Kasper Johansson Date: Mon, 16 Oct 2023 09:55:17 -0700 Subject: [PATCH] edits --- .gitignore | 3 + experiments/playground.ipynb | 1257 +++++++++++++++++++++++++++------- 2 files changed, 1006 insertions(+), 254 deletions(-) diff --git a/.gitignore b/.gitignore index 1924cc0e..3b13ac61 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ .vscode .ruff_cache +# Big data file +assets.csv + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/experiments/playground.ipynb b/experiments/playground.ipynb index 4c4cdba3..74d893a6 100644 --- a/experiments/playground.ipynb +++ b/experiments/playground.ipynb @@ -1,438 +1,1187 @@ { "cells": [ - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "dict1 = {\"a\":1, \"b\":2}\n", - "dict2 = {\"a\":10, \"b\":20, \"c\":3, \"d\":4}" - ] - }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "import cvxpy as cp\n", - "import numpy as np" + "import pandas as pd" ] }, { "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [], - "source": [ - "a = cp.Parameter(shape=1)" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], - "source": [ - "a.value = np.array([0])" - ] - }, - { - "cell_type": "code", - "execution_count": 25, + "execution_count": 2, "metadata": {}, "outputs": [ { - "ename": "ValueError", - "evalue": "Invalid dimensions (2,) for Parameter value.", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[25], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43ma\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalue\u001b[49m \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mzeros(\u001b[38;5;241m2\u001b[39m)\n", - "File \u001b[0;32m~/Documents/Stanford/Research/My papers/markowitz/code/cvxmarkowitz/.venv/lib/python3.9/site-packages/cvxpy/expressions/constants/parameter.py:87\u001b[0m, in \u001b[0;36mParameter.value\u001b[0;34m(self, val)\u001b[0m\n\u001b[1;32m 85\u001b[0m \u001b[39m@value\u001b[39m\u001b[39m.\u001b[39msetter\n\u001b[1;32m 86\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mvalue\u001b[39m(\u001b[39mself\u001b[39m, val):\n\u001b[0;32m---> 87\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_value \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_validate_value(val)\n", - "File \u001b[0;32m~/Documents/Stanford/Research/My papers/markowitz/code/cvxmarkowitz/.venv/lib/python3.9/site-packages/cvxpy/expressions/leaf.py:394\u001b[0m, in \u001b[0;36mLeaf._validate_value\u001b[0;34m(self, val)\u001b[0m\n\u001b[1;32m 392\u001b[0m val \u001b[39m=\u001b[39m intf\u001b[39m.\u001b[39mconvert(val)\n\u001b[1;32m 393\u001b[0m \u001b[39mif\u001b[39;00m intf\u001b[39m.\u001b[39mshape(val) \u001b[39m!=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mshape:\n\u001b[0;32m--> 394\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\n\u001b[1;32m 395\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mInvalid dimensions \u001b[39m\u001b[39m%s\u001b[39;00m\u001b[39m for \u001b[39m\u001b[39m%s\u001b[39;00m\u001b[39m value.\u001b[39m\u001b[39m\"\u001b[39m \u001b[39m%\u001b[39m\n\u001b[1;32m 396\u001b[0m (intf\u001b[39m.\u001b[39mshape(val), \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__class__\u001b[39m\u001b[39m.\u001b[39m\u001b[39m__name__\u001b[39m)\n\u001b[1;32m 397\u001b[0m )\n\u001b[1;32m 398\u001b[0m projection \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mproject(val)\n\u001b[1;32m 399\u001b[0m \u001b[39m# ^ might be a numpy array, or sparse scipy matrix.\u001b[39;00m\n", - "\u001b[0;31mValueError\u001b[0m: Invalid dimensions (2,) for Parameter value." + "name": "stderr", + "output_type": "stream", + "text": [ + "/var/folders/mr/mp0qg4bx6sj56pg6fs3s65kc0000gr/T/ipykernel_57832/1697221536.py:1: DtypeWarning: Columns (9) have mixed types. Specify dtype option on import or set low_memory=False.\n", + " data = pd.read_csv('data/assets.csv')\n" ] } ], "source": [ - "a.value = np.zeros(2)" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "argument[1].is_incr(0)" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "4.0" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.array([1, 1]) @ np.array([[1.0, 0.5], [0.5, 2.0]]) @ np.array([1, 1])" - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1.1287249999999998" - ] - }, - "execution_count": 55, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "0.7**2 + 0.75**2 + 0.1**2*(0.5**2 + 0.1**2 + 0.2**2) + (0.2*0.7+0.1*0.75)**2 + 0.3**2*(0.5**2 + 0.1**2 + 0.2**2)" + "data = pd.read_csv('data/assets.csv')" ] }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 3, "metadata": {}, "outputs": [ { "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PERMNOdateTICKERCOMNAMPERMCOBIDLOASKHIPRCVOLRETBIDASKSHROUTNUMTRD
0100012000-01-03EWSTENERGY WEST INC79538.43758.56258.56251721.00.0073538.43758.56252450.02.0
1100012000-01-04EWSTENERGY WEST INC79538.43758.43758.43751080.0-0.0145998.43758.56252450.02.0
2100012000-01-05EWSTENERGY WEST INC79538.43758.56258.56251711.00.0148158.43758.56252450.05.0
3100012000-01-06EWSTENERGY WEST INC79538.50008.50008.5000580.0-0.0072998.43758.56252450.02.0
4100012000-01-07EWSTENERGY WEST INC79538.43758.56258.43751406.0-0.0073538.43758.56252450.04.0
\n", + "
" + ], "text/plain": [ - "1.098725" + " PERMNO date TICKER COMNAM PERMCO BIDLO ASKHI PRC \\\n", + "0 10001 2000-01-03 EWST ENERGY WEST INC 7953 8.4375 8.5625 8.5625 \n", + "1 10001 2000-01-04 EWST ENERGY WEST INC 7953 8.4375 8.4375 8.4375 \n", + "2 10001 2000-01-05 EWST ENERGY WEST INC 7953 8.4375 8.5625 8.5625 \n", + "3 10001 2000-01-06 EWST ENERGY WEST INC 7953 8.5000 8.5000 8.5000 \n", + "4 10001 2000-01-07 EWST ENERGY WEST INC 7953 8.4375 8.5625 8.4375 \n", + "\n", + " VOL RET BID ASK SHROUT NUMTRD \n", + "0 1721.0 0.007353 8.4375 8.5625 2450.0 2.0 \n", + "1 1080.0 -0.014599 8.4375 8.5625 2450.0 2.0 \n", + "2 1711.0 0.014815 8.4375 8.5625 2450.0 5.0 \n", + "3 580.0 -0.007299 8.4375 8.5625 2450.0 2.0 \n", + "4 1406.0 -0.007353 8.4375 8.5625 2450.0 4.0 " ] }, - "execution_count": 52, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "0.7**2 + 0.75**2 + (0.2*0.7+0.1*0.75)**2" + "data.head()" ] }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 5, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1.128725" - ] - }, - "execution_count": 57, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "1.098725+0.030000000000000006" + "# make data.RET float or NaN\n", + "data.RET = pd.to_numeric(data.RET, errors='coerce')" ] }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 6, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0.030000000000000006" - ] - }, - "execution_count": 58, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "0.1**2*(0.5**2 + 0.1**2 + 0.2**2) + 0.3**2*(0.5**2 + 0.1**2 + 0.2**2)" + "# prices for each permco\n", + "prices = data.pivot_table(index='date', columns='PERMNO', values='PRC')\n", + "volumes = data.pivot_table(index='date', columns='PERMNO', values='VOL')\n", + "returns = data.pivot_table(index='date', columns='PERMNO', values='RET')\n", + "\n", + "shares = data.pivot_table(index='date', columns='PERMNO', values='SHROUT')\n", + "market_caps = prices * shares\n", + "\n", + "bids = data.pivot_table(index='date', columns='PERMNO', values='BID')\n", + "asks = data.pivot_table(index='date', columns='PERMNO', values='ASK')\n", + "spreads = (asks - bids) / prices\n", + "\n", + "bidlos = data.pivot_table(index='date', columns='PERMNO', values='BIDLO')\n", + "askhis = data.pivot_table(index='date', columns='PERMNO', values='ASKHI')\n", + "spreads2 = (askhis - bidlos) / prices" ] }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 13, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0.17320508075688776" - ] - }, - "execution_count": 59, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "np.sqrt(0.030000000000000006)" + "market_cap_start = market_caps.iloc[0]\n", + "\n", + "biggest_150 = market_cap_start.sort_values(ascending=False).iloc[:150]\n", + "\n", + "returns_new = returns[biggest_150.index].dropna(axis=1)\n", + "volumes_new = volumes[returns_new.columns]\n", + "spreads_new = spreads[returns_new.columns]\n", + "spreads_new2 = spreads2[returns_new.columns]" ] }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 78, "metadata": {}, "outputs": [ { "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PERMNOdateTICKERCOMNAMPERMCOBIDLOASKHIPRCVOLRETBIDASKSHROUTNUMTRD
1226245118502000-01-03XOMEXXON MOBIL CORP2067877.875080.750078.18756728599.0-0.02948078.125078.50003461574.0NaN
1226246118502000-01-04XOMEXXON MOBIL CORP2067876.500078.187576.81257255399.0-0.01758676.625077.00003461574.0NaN
1226247118502000-01-05XOMEXXON MOBIL CORP2067877.812581.750081.00008742500.00.05451680.625081.00003461574.0NaN
1226248118502000-01-06XOMEXXON MOBIL CORP2067880.187585.812585.00009730699.00.04938384.500085.56253461574.0NaN
1226249118502000-01-07XOMEXXON MOBIL CORP2067884.625086.250084.93758302799.0-0.00073584.062585.56253461574.0NaN
.............................................
1232027118502022-12-23XOMEXXON MOBIL CORP20678106.9000108.7400108.680011539448.00.026445108.6800108.69004118293.0NaN
1232028118502022-12-27XOMEXXON MOBIL CORP20678108.8300110.4650110.190011962058.00.013894110.1800110.19004118293.0NaN
1232029118502022-12-28XOMEXXON MOBIL CORP20678107.9020110.0000108.380010702075.0-0.016426108.3300108.34004118293.0NaN
1232030118502022-12-29XOMEXXON MOBIL CORP20678107.9900109.7200109.200010534028.00.007566109.2100109.22004118293.0NaN
1232031118502022-12-30XOMEXXON MOBIL CORP20678108.6100110.5000110.300011799640.00.010073110.3100110.32004118293.0NaN
\n", + "

5787 rows × 14 columns

\n", + "
" + ], "text/plain": [ - "1.1337249999999999" + " PERMNO date TICKER COMNAM PERMCO BIDLO \\\n", + "1226245 11850 2000-01-03 XOM EXXON MOBIL CORP 20678 77.8750 \n", + "1226246 11850 2000-01-04 XOM EXXON MOBIL CORP 20678 76.5000 \n", + "1226247 11850 2000-01-05 XOM EXXON MOBIL CORP 20678 77.8125 \n", + "1226248 11850 2000-01-06 XOM EXXON MOBIL CORP 20678 80.1875 \n", + "1226249 11850 2000-01-07 XOM EXXON MOBIL CORP 20678 84.6250 \n", + "... ... ... ... ... ... ... \n", + "1232027 11850 2022-12-23 XOM EXXON MOBIL CORP 20678 106.9000 \n", + "1232028 11850 2022-12-27 XOM EXXON MOBIL CORP 20678 108.8300 \n", + "1232029 11850 2022-12-28 XOM EXXON MOBIL CORP 20678 107.9020 \n", + "1232030 11850 2022-12-29 XOM EXXON MOBIL CORP 20678 107.9900 \n", + "1232031 11850 2022-12-30 XOM EXXON MOBIL CORP 20678 108.6100 \n", + "\n", + " ASKHI PRC VOL RET BID ASK \\\n", + "1226245 80.7500 78.1875 6728599.0 -0.029480 78.1250 78.5000 \n", + "1226246 78.1875 76.8125 7255399.0 -0.017586 76.6250 77.0000 \n", + "1226247 81.7500 81.0000 8742500.0 0.054516 80.6250 81.0000 \n", + "1226248 85.8125 85.0000 9730699.0 0.049383 84.5000 85.5625 \n", + "1226249 86.2500 84.9375 8302799.0 -0.000735 84.0625 85.5625 \n", + "... ... ... ... ... ... ... \n", + "1232027 108.7400 108.6800 11539448.0 0.026445 108.6800 108.6900 \n", + "1232028 110.4650 110.1900 11962058.0 0.013894 110.1800 110.1900 \n", + "1232029 110.0000 108.3800 10702075.0 -0.016426 108.3300 108.3400 \n", + "1232030 109.7200 109.2000 10534028.0 0.007566 109.2100 109.2200 \n", + "1232031 110.5000 110.3000 11799640.0 0.010073 110.3100 110.3200 \n", + "\n", + " SHROUT NUMTRD \n", + "1226245 3461574.0 NaN \n", + "1226246 3461574.0 NaN \n", + "1226247 3461574.0 NaN \n", + "1226248 3461574.0 NaN \n", + "1226249 3461574.0 NaN \n", + "... ... ... \n", + "1232027 4118293.0 NaN \n", + "1232028 4118293.0 NaN \n", + "1232029 4118293.0 NaN \n", + "1232030 4118293.0 NaN \n", + "1232031 4118293.0 NaN \n", + "\n", + "[5787 rows x 14 columns]" ] }, - "execution_count": 54, + "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "0.035 + 1.098725" + "data[data[\"PERMNO\"]==11850]" ] }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([[ 0.62640616],\n", - " [ 0.37359384],\n", - " [-2.86631143]])" + "0.03409149818558839" ] }, - "execution_count": 50, + "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "Sigma = np.array([[2,0.4], [0.4, 3]])\n", - "d = np.array([0.15, 0.3])\n", - "\n", - "ones = np.ones(2).reshape(-1,1)\n", - "\n", - "A1 = np.hstack([2*(Sigma + np.outer(d,d)), ones])\n", - "A2 = np.hstack([ones.T, np.zeros((1,1))])\n", - "A = np.vstack([A1, A2])\n", - "\n", - "b = np.vstack([np.zeros((2,1)), np.ones((1,1))])\n", - "w_star = np.linalg.solve(A, b)\n", - "w_star" + "(spreads_new > 0.01).sum().sum() / spreads_new.size" ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([[1. , 0.5],\n", - " [0.5, 2. ]])" + "0.0015" ] }, - "execution_count": 23, + "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "np.array([[1.0, 0.5], [0.5, 2.0]])" + "0.01**2*15" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "True" + "0.00887549680317954" ] }, - "execution_count": 16, + "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "obj.is_dcp()" + "spreads_new.isna().sum().sum() / spreads_new.size" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 22, "metadata": {}, "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n", - "True\n" - ] - }, { "data": { "text/plain": [ - "True" + "date\n", + "2000-01-03 0.000536\n", + "2000-01-04 0.000555\n", + "2000-01-05 0.000549\n", + "2000-01-06 0.000568\n", + "2000-01-07 0.000561\n", + " ... \n", + "2022-12-23 0.000168\n", + "2022-12-27 0.000169\n", + "2022-12-28 0.000043\n", + "2022-12-29 0.000207\n", + "2022-12-30 0.000042\n", + "Name: 10107, Length: 5787, dtype: float64" ] }, - "execution_count": 2, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "n = 10\n", - "L = cp.Parameter(\n", - " shape=(n, n),\n", - " name=\"chol\",\n", - " value=np.zeros((n, n)),\n", - ")\n", - "w = cp.Variable(n)\n", - "\n", - "e = cp.Parameter(shape=n, value=np.zeros(n), nonneg=True)\n", - "\n", - "\n", - "argument = cp.hstack([L@w, cp.sum(w)])\n", - "obj = cp.norm2(argument)\n", - "\n", - "# obj = cp.norm2(L@w)\n", - "print(argument.is_affine())\n", - "for i in range(11):\n", - " print(argument[i].is_convex())\n", - " print(argument.is_incr(i))\n", - " \n", - "\n", - "\n", - "# obj.is_convex()\n", - "\n", - "prob = cp.Problem(cp.Minimize(obj))\n", - "prob.is_dcp()" + "spreads_new.iloc[:,0]" ] }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 49, "metadata": {}, "outputs": [ { "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PERMNO10107120607607655976593281185012490705191010466093...45751221032157347941347466139987055857535361356573
date
2000-01-030.0005360.0008330.0005780.0056070.0007180.0047960.0129730.0070920.0005290.021192...0.0052880.0065150.0090090.0063540.0008940.0067260.0007020.0003290.0065840.005871
2000-01-040.0005550.0069350.0006130.0058370.0007540.0048820.0178470.0098770.0005800.021186...0.0069200.0065930.0069280.0048540.0009340.0094790.0029720.0003320.0136050.006148
2000-01-050.0005490.0004360.0006150.0079370.0007470.0046300.0086210.0147600.0000000.030598...0.0043070.0064720.0089290.0065900.0009480.0071170.0007300.0042130.0051550.006073
2000-01-060.0005680.0042900.0006250.0078510.0007940.0125000.0173910.006928-0.0013020.022857...0.0054090.0083950.0087530.0066560.0009160.009512-0.0007160.0012090.0675680.008155
2000-01-070.0005610.0107390.0005900.0072990.0007620.0176600.0044050.0138890.0012090.008499...0.0104850.0060240.0085290.0066610.0009220.0093020.0006720.0003680.0070920.007663
\n", + "

5 rows × 80 columns

\n", + "
" + ], "text/plain": [ - "True" + "PERMNO 10107 12060 76076 55976 59328 11850 \\\n", + "date \n", + "2000-01-03 0.000536 0.000833 0.000578 0.005607 0.000718 0.004796 \n", + "2000-01-04 0.000555 0.006935 0.000613 0.005837 0.000754 0.004882 \n", + "2000-01-05 0.000549 0.000436 0.000615 0.007937 0.000747 0.004630 \n", + "2000-01-06 0.000568 0.004290 0.000625 0.007851 0.000794 0.012500 \n", + "2000-01-07 0.000561 0.010739 0.000590 0.007299 0.000762 0.017660 \n", + "\n", + "PERMNO 12490 70519 10104 66093 ... 45751 22103 \\\n", + "date ... \n", + "2000-01-03 0.012973 0.007092 0.000529 0.021192 ... 0.005288 0.006515 \n", + "2000-01-04 0.017847 0.009877 0.000580 0.021186 ... 0.006920 0.006593 \n", + "2000-01-05 0.008621 0.014760 0.000000 0.030598 ... 0.004307 0.006472 \n", + "2000-01-06 0.017391 0.006928 -0.001302 0.022857 ... 0.005409 0.008395 \n", + "2000-01-07 0.004405 0.013889 0.001209 0.008499 ... 0.010485 0.006024 \n", + "\n", + "PERMNO 21573 47941 34746 61399 87055 85753 \\\n", + "date \n", + "2000-01-03 0.009009 0.006354 0.000894 0.006726 0.000702 0.000329 \n", + "2000-01-04 0.006928 0.004854 0.000934 0.009479 0.002972 0.000332 \n", + "2000-01-05 0.008929 0.006590 0.000948 0.007117 0.000730 0.004213 \n", + "2000-01-06 0.008753 0.006656 0.000916 0.009512 -0.000716 0.001209 \n", + "2000-01-07 0.008529 0.006661 0.000922 0.009302 0.000672 0.000368 \n", + "\n", + "PERMNO 53613 56573 \n", + "date \n", + "2000-01-03 0.006584 0.005871 \n", + "2000-01-04 0.013605 0.006148 \n", + "2000-01-05 0.005155 0.006073 \n", + "2000-01-06 0.067568 0.008155 \n", + "2000-01-07 0.007092 0.007663 \n", + "\n", + "[5 rows x 80 columns]" ] }, - "execution_count": 99, + "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "a = cp.Parameter()\n", - "\n", - "w = cp.Variable()\n", - "\n", - "prob = cp.Problem(cp.Minimize(cp.square(a*w)))\n", - "\n", - "prob.is_dpp()" + "spreads_new.head()" ] }, { "cell_type": "code", - "execution_count": 91, + "execution_count": 50, "metadata": {}, "outputs": [ { "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PERMNO10107120607607655976593281185012490705191010466093...45751221032157347941347466139987055857535361356573
date
2000-01-030.0568360.0300000.0618850.0523360.0474140.0367710.0340540.0555560.1148150.052980...0.0152020.0217160.0315320.0357430.0581400.0795960.0351120.1275480.0740740.069472
2000-01-040.0432850.0273080.0514710.0252920.0678220.0219690.0323480.0469140.1265240.096045...0.0740480.0131870.0496540.0210360.0392160.0699050.0616640.1048440.0399660.052254
2000-01-050.0615050.0309370.0712970.0347220.0642750.0486110.0657330.0418200.1017160.018081...0.0545590.0248110.0502230.0214170.0293840.0415180.0255660.1776690.0644330.035425
2000-01-060.0500000.0296040.0325000.0294410.0658730.0661760.0472830.0635100.1074220.042857...0.0676130.0440710.0404810.0241260.0485350.0404280.0300750.1805000.0481420.020387
2000-01-070.0443070.0322180.0625740.0647810.0564020.0191320.0644270.0393520.0961310.031161...0.0458720.0471890.0405120.0241470.0322580.0313950.0691740.1586160.0815600.047893
\n", + "

5 rows × 80 columns

\n", + "
" + ], "text/plain": [ - "(11,)" + "PERMNO 10107 12060 76076 55976 59328 11850 \\\n", + "date \n", + "2000-01-03 0.056836 0.030000 0.061885 0.052336 0.047414 0.036771 \n", + "2000-01-04 0.043285 0.027308 0.051471 0.025292 0.067822 0.021969 \n", + "2000-01-05 0.061505 0.030937 0.071297 0.034722 0.064275 0.048611 \n", + "2000-01-06 0.050000 0.029604 0.032500 0.029441 0.065873 0.066176 \n", + "2000-01-07 0.044307 0.032218 0.062574 0.064781 0.056402 0.019132 \n", + "\n", + "PERMNO 12490 70519 10104 66093 ... 45751 22103 \\\n", + "date ... \n", + "2000-01-03 0.034054 0.055556 0.114815 0.052980 ... 0.015202 0.021716 \n", + "2000-01-04 0.032348 0.046914 0.126524 0.096045 ... 0.074048 0.013187 \n", + "2000-01-05 0.065733 0.041820 0.101716 0.018081 ... 0.054559 0.024811 \n", + "2000-01-06 0.047283 0.063510 0.107422 0.042857 ... 0.067613 0.044071 \n", + "2000-01-07 0.064427 0.039352 0.096131 0.031161 ... 0.045872 0.047189 \n", + "\n", + "PERMNO 21573 47941 34746 61399 87055 85753 \\\n", + "date \n", + "2000-01-03 0.031532 0.035743 0.058140 0.079596 0.035112 0.127548 \n", + "2000-01-04 0.049654 0.021036 0.039216 0.069905 0.061664 0.104844 \n", + "2000-01-05 0.050223 0.021417 0.029384 0.041518 0.025566 0.177669 \n", + "2000-01-06 0.040481 0.024126 0.048535 0.040428 0.030075 0.180500 \n", + "2000-01-07 0.040512 0.024147 0.032258 0.031395 0.069174 0.158616 \n", + "\n", + "PERMNO 53613 56573 \n", + "date \n", + "2000-01-03 0.074074 0.069472 \n", + "2000-01-04 0.039966 0.052254 \n", + "2000-01-05 0.064433 0.035425 \n", + "2000-01-06 0.048142 0.020387 \n", + "2000-01-07 0.081560 0.047893 \n", + "\n", + "[5 rows x 80 columns]" ] }, - "execution_count": 91, + "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "argument.shape" + "spreads_new2.head()" ] }, { "cell_type": "code", - "execution_count": 94, + "execution_count": 62, "metadata": {}, "outputs": [ { "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
PERMNO1010712060
date
2000-01-03112.0000149.1875
2000-01-04112.2500144.0625
2000-01-05109.3750142.5625
2000-01-06108.3750142.6250
2000-01-07107.3125147.0000
\n", + "
" + ], "text/plain": [ - "(20,)" + "PERMNO 10107 12060\n", + "date \n", + "2000-01-03 112.0000 149.1875\n", + "2000-01-04 112.2500 144.0625\n", + "2000-01-05 109.3750 142.5625\n", + "2000-01-06 108.3750 142.6250\n", + "2000-01-07 107.3125 147.0000" ] }, - "execution_count": 94, + "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "np.random.uniform(size=20).shape" + "bidlos[[10107, 12060]].head()" ] } ],