Skip to content

Instantly share code, notes, and snippets.

@HYChou0515
Last active February 8, 2023 15:12
Show Gist options
  • Save HYChou0515/a54f8de7daa4ea6d714cd39e8a978500 to your computer and use it in GitHub Desktop.
Save HYChou0515/a54f8de7daa4ea6d714cd39e8a978500 to your computer and use it in GitHub Desktop.
Use optimization and bayes rule to calculate probability for each step of flows.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "5ccb721a-5978-4809-a8ae-916ccf838ed3",
"metadata": {},
"outputs": [],
"source": [
"import scipy as sp\n",
"from scipy import optimize\n",
"import numpy as np\n",
"import pandas as pd\n",
"import itertools as it\n",
"\n",
"rng = np.random.default_rng(0)"
]
},
{
"cell_type": "markdown",
"id": "31b8a5fa-3bb2-4b6b-b896-0831d881b2a6",
"metadata": {
"tags": []
},
"source": [
"## Naive Bayes\n",
"This example shows if we see each step independent to other steps, we get some trouble.."
]
},
{
"cell_type": "markdown",
"id": "01f8f71d-ad94-4440-9603-269c9303afc2",
"metadata": {
"tags": []
},
"source": [
"### Indenpent flow"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9a7856fa-e090-49fe-bc08-ddb97e5fb09f",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"tool_prob = [[p/sum(ps) for p in ps] for ps in [[30,2,1],[3,1],[9,1,1]]]\n",
"# tool_prob = [[p/sum(ps) for p in ps] for ps in [[1,1,1],[1,1],[1,1,1]]]\n",
"defect_prob = [[.6,0,0],[0,.2],[0,0,0]]\n",
"n_trial = 10000"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "dfa4b87d-1fde-492d-abbc-d169a86c3d0b",
"metadata": {},
"outputs": [],
"source": [
"def trial():\n",
" assert len(tool_prob) == len(defect_prob)\n",
" assert [len(ps) for ps in tool_prob] == [len(ps) for ps in defect_prob]\n",
" good_prob = [[1-p for p in ps] for ps in defect_prob]\n",
" result = []\n",
" gp = 1.0\n",
" for step in range(len(tool_prob)):\n",
" tps = tool_prob[step]\n",
" gps = good_prob[step]\n",
" tid = rng.choice(np.arange(len(tps)), p=tps)\n",
" gp *= gps[tid]\n",
" result.append(tid)\n",
" result.append(1 if rng.random() >= gp else 0)\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e7ee07de-22b0-4f85-851d-f5c09bfa5e27",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" <th>2</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9995</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9996</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9997</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9998</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9999</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>10000 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" 0 1 2\n",
"0 0 0 0\n",
"1 0 1 0\n",
"2 0 1 0\n",
"3 0 0 0\n",
"4 0 0 0\n",
"... .. .. ..\n",
"9995 0 0 0\n",
"9996 0 1 2\n",
"9997 1 0 0\n",
"9998 0 0 0\n",
"9999 0 0 0\n",
"\n",
"[10000 rows x 3 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame([trial() for _ in range(n_trial)])\n",
"defect = df.iloc[:,-1]\n",
"df = df.drop(columns=df.columns[-1])\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "95b689a3-656d-4c4e-90d1-8ab77898ebe1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[0.6216989436619718, 0.0556464811783961, 0.04234527687296419],\n",
" [0.543721674220208, 0.6462829736211031],\n",
" [0.5704550985641275, 0.5609220636663008, 0.5678449258836944]]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"naive_bayes = []\n",
"for step in range(len(tool_prob)):\n",
" ps = []\n",
" for tid in range(len(tool_prob[step])):\n",
" dom = sum(df[step]==tid)+2\n",
" num = sum((df[step]==tid) & (defect==0))+1\n",
" if dom == 0:\n",
" ps.append(np.nan)\n",
" else:\n",
" ps.append(1-num/dom)\n",
" naive_bayes.append(ps)\n",
"naive_bayes"
]
},
{
"cell_type": "markdown",
"id": "0a9a7e23-2d87-48c5-8287-309a62fdaded",
"metadata": {},
"source": [
"We see that even tool_00 is worse than tool_11, but we estimated tool_11 worse than tool_00\n",
"\n",
"This is because some bad result from tool_11 should be blamed on tool_00.\n",
"\n",
"Using this method, probability across steps cannot be compared."
]
},
{
"cell_type": "markdown",
"id": "daa44038-0bd2-40d1-b45e-4f9d5b897670",
"metadata": {
"tags": []
},
"source": [
"### Predefined Flow"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d220ee43-46fa-4a70-afb7-fd2b1bbd5b3e",
"metadata": {},
"outputs": [],
"source": [
"tool_choice = [3,2,3]\n",
"all_flows = list(it.product(*(range(t) for t in tool_choice)))\n",
"flows = [*all_flows]\n",
"rng.shuffle(flows)\n",
"flows = flows[:8]\n",
"flow_prob = [1,1,3,1,2,14,1,1]\n",
"flow_prob = [p/sum(flow_prob) for p in flow_prob]\n",
"\n",
"defect_prob = [[.6,0,0],[0,.2],[0,0,0]]\n",
"n_trial = 10000"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "51afe661-d3a2-4331-8757-3e3aad457cc5",
"metadata": {},
"outputs": [],
"source": [
"def trial():\n",
" assert len(flow_prob) == len(flows)\n",
" good_prob = [[1-p for p in ps] for ps in defect_prob]\n",
" result = []\n",
" gp = 1.0\n",
" fl = rng.choice(flows, p=flow_prob)\n",
" for step, tid in enumerate(fl):\n",
" gp *= good_prob[step][tid]\n",
" result.append(tid)\n",
" result.append(1 if rng.random() >= gp else 0)\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "dd73ebb0-ff30-425c-b4a4-987e07c29000",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" <th>2</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9995</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9996</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9997</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9998</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9999</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>10000 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" 0 1 2\n",
"0 0 0 0\n",
"1 0 0 0\n",
"2 0 0 0\n",
"3 0 0 2\n",
"4 1 0 1\n",
"... .. .. ..\n",
"9995 0 0 0\n",
"9996 0 1 1\n",
"9997 0 0 0\n",
"9998 0 1 1\n",
"9999 0 0 0\n",
"\n",
"[10000 rows x 3 columns]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame([trial() for _ in range(n_trial)])\n",
"defect = df.iloc[:,-1]\n",
"df = df.drop(columns=df.columns[-1])\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "ca314f3d-bd00-4ab2-bf1c-6c7bd8b0adfe",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[0.6122881355932204, 0.0005882352941176672, 0.10718492343934038],\n",
" [0.42633443822151107, 0.3968127490039841],\n",
" [0.5628327149540249, 0.36520737327188935, 0.14392650561415443]]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"naive_bayes = []\n",
"for step in range(len(tool_choice)):\n",
" ps = []\n",
" for tid in range(tool_choice[step]):\n",
" dom = sum(df[step]==tid)+2\n",
" num = sum((df[step]==tid) & (defect==0))+1\n",
" if dom == 0:\n",
" ps.append(np.nan)\n",
" else:\n",
" ps.append(1-num/dom)\n",
" naive_bayes.append(ps)\n",
"naive_bayes"
]
},
{
"cell_type": "markdown",
"id": "842f4206-5bb5-4537-b204-b5df93a1ee3e",
"metadata": {},
"source": [
"## Optimization"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "c33a9667-4a8c-48b6-ab19-b8c6aeb18c97",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" <th>2</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>995</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>996</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>997</th>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>998</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>999</th>\n",
" <td>2</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1000 rows × 3 columns</p>\n",
"</div>"
],
"text/plain": [
" 0 1 2\n",
"0 0 0 2\n",
"1 0 0 2\n",
"2 0 0 0\n",
"3 0 0 0\n",
"4 0 0 0\n",
".. .. .. ..\n",
"995 0 0 2\n",
"996 0 0 0\n",
"997 2 0 0\n",
"998 0 0 0\n",
"999 2 0 0\n",
"\n",
"[1000 rows x 3 columns]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"n_trial = 1000\n",
"df = pd.DataFrame([trial() for _ in range(n_trial)])\n",
"defect = df.iloc[:,-1]\n",
"df = df.drop(columns=df.columns[-1])\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "90aa8115-baa6-4dcb-a971-6ce4e52b1f34",
"metadata": {},
"source": [
"`good_prob_each_flow` is the good probability for each flow based on the observed samples.\n",
"\n",
"Later we will use this as the target to assign each tool the bad probability through optimization."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "42405712-0cd2-4c6d-9e69-09c2a6792875",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bad trial/ total trial for each flow\n"
]
},
{
"data": {
"text/plain": [
"flows\n",
"(0, 0, 0) 0.439655\n",
"(0, 0, 2) 0.415094\n",
"(0, 1, 1) 0.279070\n",
"(1, 0, 1) 1.000000\n",
"(1, 0, 2) 1.000000\n",
"(2, 0, 0) 1.000000\n",
"(2, 0, 2) 1.000000\n",
"(2, 1, 2) 0.880597\n",
"Name: defect, dtype: float64"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"rearrange to a 1D array\n"
]
},
{
"data": {
"text/plain": [
"array([0.43965517, nan, 0.41509434, nan, 0.27906977,\n",
" nan, nan, 1. , 1. , nan,\n",
" nan, nan, 1. , nan, 1. ,\n",
" nan, nan, 0.88059701])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"flow_col_name = 'flows'\n",
"defect.name = 'defect'\n",
"_concats = pd.concat([df.apply(tuple, axis=1).rename(flow_col_name), defect], axis=1).groupby(flow_col_name)[defect.name]\n",
"df_good_prob_each_flow = 1-_concats.sum()/_concats.count()\n",
"print(\"bad trial/ total trial for each flow\")\n",
"display(df_good_prob_each_flow)\n",
"\n",
"print(\"rearrange to a 1D array\")\n",
"df_good_prob_each_flow = df_good_prob_each_flow.reset_index()\n",
"df_good_prob_each_flow[flow_col_name] = df_good_prob_each_flow[flow_col_name].apply(all_flows.index)\n",
"good_prob_each_flow = np.zeros(shape=(len(all_flows),))+np.nan\n",
"good_prob_each_flow[df_good_prob_each_flow[flow_col_name]] = df_good_prob_each_flow[defect.name]\n",
"good_prob_each_flow"
]
},
{
"cell_type": "markdown",
"id": "c3e36aba-e28c-40ab-8977-d83523d48ffa",
"metadata": {},
"source": [
"$A \\in \\{0,1\\}^{N \\times M}$ is an indicater matrix, where $N$ is the number of total possible flows, and $M$ is the number of all tools.\n",
"\n",
"Each row represent a possible choice of tools of a flow.\n",
"\n",
"For example, $A[1]$ is $[1,0,0,1,0,0,1,0]$, means flow[0] is tool_00 -> tool_10 -> tool_21.\n",
"\n",
"Note that the 2nd 1 in the row is in the 4th element of the row, which means the 1st tool in the 2nd step as the 1st step has 3 choices of tool."
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "82338f07-1408-4244-b240-e9dad1866e12",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 0., 0., 1., 0., 1., 0., 0.],\n",
" [1., 0., 0., 1., 0., 0., 1., 0.],\n",
" [1., 0., 0., 1., 0., 0., 0., 1.],\n",
" [1., 0., 0., 0., 1., 1., 0., 0.],\n",
" [1., 0., 0., 0., 1., 0., 1., 0.],\n",
" [1., 0., 0., 0., 1., 0., 0., 1.],\n",
" [0., 1., 0., 1., 0., 1., 0., 0.],\n",
" [0., 1., 0., 1., 0., 0., 1., 0.],\n",
" [0., 1., 0., 1., 0., 0., 0., 1.],\n",
" [0., 1., 0., 0., 1., 1., 0., 0.],\n",
" [0., 1., 0., 0., 1., 0., 1., 0.],\n",
" [0., 1., 0., 0., 1., 0., 0., 1.],\n",
" [0., 0., 1., 1., 0., 1., 0., 0.],\n",
" [0., 0., 1., 1., 0., 0., 1., 0.],\n",
" [0., 0., 1., 1., 0., 0., 0., 1.],\n",
" [0., 0., 1., 0., 1., 1., 0., 0.],\n",
" [0., 0., 1., 0., 1., 0., 1., 0.],\n",
" [0., 0., 1., 0., 1., 0., 0., 1.]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.zeros(shape=(len(all_flows),sum(tool_choice)))\n",
"for i, fl in enumerate(all_flows):\n",
" s = 0\n",
" for j, f in enumerate(fl):\n",
" A[i,s+f] = 1\n",
" s += tool_choice[j]\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "ff74b8f7-e784-4fe5-9e63-7d81c223d565",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 0., 0., 1., 0., 1., 0., 0.],\n",
" [1., 0., 0., 1., 0., 0., 0., 1.],\n",
" [1., 0., 0., 0., 1., 0., 1., 0.],\n",
" [0., 1., 0., 1., 0., 0., 1., 0.],\n",
" [0., 1., 0., 1., 0., 0., 0., 1.],\n",
" [0., 0., 1., 1., 0., 1., 0., 0.],\n",
" [0., 0., 1., 1., 0., 0., 0., 1.],\n",
" [0., 0., 1., 0., 1., 0., 0., 1.]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([-8.21764556e-01, -8.79249458e-01, -1.27629346e+00, 1.00000008e-09,\n",
" 1.00000008e-09, 1.00000008e-09, 1.00000008e-09, -1.27155174e-01])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# use the log-likelihood instead of product of probability\n",
"c = np.log(good_prob_each_flow+1e-9)\n",
"# drop flow doesn't exist in the samples for efficiency\n",
"A = A[~np.isnan(c)]\n",
"c = c[np.nonzero(~np.isnan(c))[0]]\n",
"display(A)\n",
"display(c)"
]
},
{
"cell_type": "markdown",
"id": "0c4cf8b7-be39-4ca4-a5c9-e81ba0bfb9b9",
"metadata": {},
"source": [
"The objective is based on $x \\in \\mathbb{R}^M$, the hypothesized bad probability for each tool.\n",
"\n",
"Given $x$, we can find the theoretical bad probability for each flow, and then compare with the sampled probability $c$."
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "47a3a5bb-9b31-44ef-ad33-b5104d9ef8d5",
"metadata": {},
"outputs": [],
"source": [
"def opt(x):\n",
" y = A@x-c\n",
" return np.sqrt(sum(y**2)/y.shape[0])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "1ca06a9d-57f8-4286-bbb7-f992ed411c5f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.6218390100158231"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# test\n",
"opt(np.zeros(A.shape[1]))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "63095461-3915-45c2-8098-472b474f778b",
"metadata": {},
"outputs": [],
"source": [
"res = optimize.minimize(opt, np.zeros(A.shape[1]), constraints=optimize.LinearConstraint(np.eye(A.shape[1]), lb=-np.inf, ub=0))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "046e274b-1b3f-40b0-9f4e-540eec078d27",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[0.6, 0, 0], [0, 0.2], [0, 0, 0]]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"defect_prob"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "b31656a7-cfe3-4d32-bbdd-c4ef8607a4a8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.40935805, 1. , 1. , 1. , 0.8084688 ,\n",
" 1. , 0.91830942, 1. ])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.exp(res.x)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "63a3f7a1-f6f7-4069-a2ba-eaad0a4851c6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment