diff --git a/experiments/playground.ipynb b/experiments/playground.ipynb deleted file mode 100644 index 4c4cdba3..00000000 --- a/experiments/playground.ipynb +++ /dev/null @@ -1,466 +0,0 @@ -{ - "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" - ] - }, - { - "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, - "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." - ] - } - ], - "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)" - ] - }, - { - "cell_type": "code", - "execution_count": 52, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1.098725" - ] - }, - "execution_count": 52, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "0.7**2 + 0.75**2 + (0.2*0.7+0.1*0.75)**2" - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1.128725" - ] - }, - "execution_count": 57, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "1.098725+0.030000000000000006" - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0.030000000000000006" - ] - }, - "execution_count": 58, - "metadata": {}, - "output_type": "execute_result" - } - ], - "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)" - ] - }, - { - "cell_type": "code", - "execution_count": 59, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0.17320508075688776" - ] - }, - "execution_count": 59, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.sqrt(0.030000000000000006)" - ] - }, - { - "cell_type": "code", - "execution_count": 54, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1.1337249999999999" - ] - }, - "execution_count": 54, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "0.035 + 1.098725" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[ 0.62640616],\n", - " [ 0.37359384],\n", - " [-2.86631143]])" - ] - }, - "execution_count": 50, - "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" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[1. , 0.5],\n", - " [0.5, 2. ]])" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.array([[1.0, 0.5], [0.5, 2.0]])" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "obj.is_dcp()" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "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" - ] - }, - "execution_count": 2, - "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()" - ] - }, - { - "cell_type": "code", - "execution_count": 99, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 99, - "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()" - ] - }, - { - "cell_type": "code", - "execution_count": 91, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(11,)" - ] - }, - "execution_count": 91, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "argument.shape" - ] - }, - { - "cell_type": "code", - "execution_count": 94, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(20,)" - ] - }, - "execution_count": 94, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.random.uniform(size=20).shape" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3.9.12 ('.venv': poetry)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.12" - }, - "orig_nbformat": 4, - "vscode": { - "interpreter": { - "hash": "61cc297eca37719bb19fab84c89bdabd630ebd76dfae75f14ab889350806e9ac" - } - } - }, - "nbformat": 4, - "nbformat_minor": 2 -}