Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nck2/8059f771316cb863717fbafbb45ad5ab to your computer and use it in GitHub Desktop.
Save nck2/8059f771316cb863717fbafbb45ad5ab to your computer and use it in GitHub Desktop.
확률계산 및 시뮬레이션
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 확률 계산"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Q: 주머니 속에 숫자 1이 적힌 카드가 세 장, 숫자 2가 적힌 카드가 3장, 3이 적힌 카드가 3장이 들어있다. \n",
"이 주머니에서 임의로 세장의 카드를 동시에 뽑을 때, 카드에 적힌 숫자의 합이 3의 배수가 될 확률은?"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.35714285714285715"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5/14 #답"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 시뮬레이션"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"card=[1,1,1,2,2,2,3,3,3]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"import random"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"random.shuffle(card)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 3, 1, 2, 3, 1, 1, 2, 2]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"card"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 3, 1]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"card[:3]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"count=100000"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sus=0"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"성공확률은 0.35674이다\n"
]
}
],
"source": [
"for i in range(count):\n",
" random.shuffle(card)\n",
" if sum(card[:3]) % 3 ==0:\n",
" sus=sus+1\n",
"print(\"성공확률은 {}이다\".format(sus/count))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 전수조사"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"carddic={'a1':1,'a2':1,'a3':1,'b1':2,'b2':2,'b3':2,'c1':3,'c2':3,'c3':3}"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'c1', 'c2', 'c3'])"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"carddic.keys()"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values([1, 1, 1, 2, 2, 2, 3, 3, 3])"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"carddic.values()"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_items([('a1', 1), ('a2', 1), ('a3', 1), ('b1', 2), ('b2', 2), ('b3', 2), ('c1', 3), ('c2', 3), ('c3', 3)])"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"carddic.items()"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import itertools"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<itertools.permutations at 0x110098ca8>"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"itertools.permutations(carddic.keys())"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"362880"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(list(itertools.permutations(carddic.keys())))"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"362880"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"9*8*7*6*5*4*3*2*1"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"per_with_eq=itertools.permutations(carddic.values())"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [],
"source": [
"a=set((per_with_eq))"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1680"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1680은 같은 것이 있는 순열의 수임."
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"504"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(list(itertools.permutations(carddic.keys(),3)))"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"504"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"9*8*7"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"84"
]
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(list(itertools.combinations(carddic.keys(),3)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 84는 9C3 의 개수임 , 즉 위의 list가 근원사건의 집합이라고 볼 수 있음"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"standard=list(itertools.combinations(carddic.keys(),3))"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('a1', 'a2', 'a3'),\n",
" ('a1', 'a2', 'b1'),\n",
" ('a1', 'a2', 'b2'),\n",
" ('a1', 'a2', 'b3'),\n",
" ('a1', 'a2', 'c1'),\n",
" ('a1', 'a2', 'c2'),\n",
" ('a1', 'a2', 'c3'),\n",
" ('a1', 'a3', 'b1'),\n",
" ('a1', 'a3', 'b2'),\n",
" ('a1', 'a3', 'b3'),\n",
" ('a1', 'a3', 'c1'),\n",
" ('a1', 'a3', 'c2'),\n",
" ('a1', 'a3', 'c3'),\n",
" ('a1', 'b1', 'b2'),\n",
" ('a1', 'b1', 'b3'),\n",
" ('a1', 'b1', 'c1'),\n",
" ('a1', 'b1', 'c2'),\n",
" ('a1', 'b1', 'c3'),\n",
" ('a1', 'b2', 'b3'),\n",
" ('a1', 'b2', 'c1'),\n",
" ('a1', 'b2', 'c2'),\n",
" ('a1', 'b2', 'c3'),\n",
" ('a1', 'b3', 'c1'),\n",
" ('a1', 'b3', 'c2'),\n",
" ('a1', 'b3', 'c3'),\n",
" ('a1', 'c1', 'c2'),\n",
" ('a1', 'c1', 'c3'),\n",
" ('a1', 'c2', 'c3'),\n",
" ('a2', 'a3', 'b1'),\n",
" ('a2', 'a3', 'b2'),\n",
" ('a2', 'a3', 'b3'),\n",
" ('a2', 'a3', 'c1'),\n",
" ('a2', 'a3', 'c2'),\n",
" ('a2', 'a3', 'c3'),\n",
" ('a2', 'b1', 'b2'),\n",
" ('a2', 'b1', 'b3'),\n",
" ('a2', 'b1', 'c1'),\n",
" ('a2', 'b1', 'c2'),\n",
" ('a2', 'b1', 'c3'),\n",
" ('a2', 'b2', 'b3'),\n",
" ('a2', 'b2', 'c1'),\n",
" ('a2', 'b2', 'c2'),\n",
" ('a2', 'b2', 'c3'),\n",
" ('a2', 'b3', 'c1'),\n",
" ('a2', 'b3', 'c2'),\n",
" ('a2', 'b3', 'c3'),\n",
" ('a2', 'c1', 'c2'),\n",
" ('a2', 'c1', 'c3'),\n",
" ('a2', 'c2', 'c3'),\n",
" ('a3', 'b1', 'b2'),\n",
" ('a3', 'b1', 'b3'),\n",
" ('a3', 'b1', 'c1'),\n",
" ('a3', 'b1', 'c2'),\n",
" ('a3', 'b1', 'c3'),\n",
" ('a3', 'b2', 'b3'),\n",
" ('a3', 'b2', 'c1'),\n",
" ('a3', 'b2', 'c2'),\n",
" ('a3', 'b2', 'c3'),\n",
" ('a3', 'b3', 'c1'),\n",
" ('a3', 'b3', 'c2'),\n",
" ('a3', 'b3', 'c3'),\n",
" ('a3', 'c1', 'c2'),\n",
" ('a3', 'c1', 'c3'),\n",
" ('a3', 'c2', 'c3'),\n",
" ('b1', 'b2', 'b3'),\n",
" ('b1', 'b2', 'c1'),\n",
" ('b1', 'b2', 'c2'),\n",
" ('b1', 'b2', 'c3'),\n",
" ('b1', 'b3', 'c1'),\n",
" ('b1', 'b3', 'c2'),\n",
" ('b1', 'b3', 'c3'),\n",
" ('b1', 'c1', 'c2'),\n",
" ('b1', 'c1', 'c3'),\n",
" ('b1', 'c2', 'c3'),\n",
" ('b2', 'b3', 'c1'),\n",
" ('b2', 'b3', 'c2'),\n",
" ('b2', 'b3', 'c3'),\n",
" ('b2', 'c1', 'c2'),\n",
" ('b2', 'c1', 'c3'),\n",
" ('b2', 'c2', 'c3'),\n",
" ('b3', 'c1', 'c2'),\n",
" ('b3', 'c1', 'c3'),\n",
" ('b3', 'c2', 'c3'),\n",
" ('c1', 'c2', 'c3')]"
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"standard"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def desc(se):\n",
" ''' 값의 합이 3 배수인지 판단하는 함수'''\n",
" return sum(list(map(lambda a : carddic[a],se)))%3 ==0"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"desc(('b3', 'c1', 'c2'))"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"30\n"
]
}
],
"source": [
"su=0\n",
"for i in standard:\n",
" if desc(i):\n",
" su=su+1\n",
"print(su)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"즉 해당경우는 30가지이며, 원하는 확률은 30/84 이다."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment