Skip to content

Instantly share code, notes, and snippets.

@coldino
Created May 19, 2020 11:30
Show Gist options
  • Save coldino/3d555bbd1a00753b012d41befc8eac08 to your computer and use it in GitHub Desktop.
Save coldino/3d555bbd1a00753b012d41befc8eac08 to your computer and use it in GitHub Desktop.
Ark Mutation Chances
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Ark Mutation Chances\n",
"\n",
"Calculates, and then simulates, the chances of a given number of mutations occurring in Ark.\n",
"\n",
"Assumes BOTH parents can contribute mutations (i.e. both are <20 mutations).\n",
"\n",
"If only one parent can contribute mutations, all chances are halved."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"C = 0.025 # Chance to mutate on each roll\n",
"rolls = 3 # There are three chances to mutate each time"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Calculate the chances using the list of possible outcomes"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Outcome chances:\n"
]
},
{
"data": {
"text/plain": [
"[(0.975, 0.975, 0.975),\n",
" (0.975, 0.975, 0.025),\n",
" (0.975, 0.025, 0.975),\n",
" (0.975, 0.025, 0.025),\n",
" (0.025, 0.975, 0.975),\n",
" (0.025, 0.975, 0.025),\n",
" (0.025, 0.025, 0.975),\n",
" (0.025, 0.025, 0.025)]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Chance of exact number of mutations:\n",
" 0: 92.685937%\n",
" 1: 7.129687%\n",
" 2: 0.182813%\n",
" 3: 0.001563%\n",
"\n",
"Total: 0.9999999999999999 (should ~= 1)\n",
" (floating point accuracy makes this unlikely to be exact)\n",
"\n",
"Chance of 'at least N' mutations:\n",
" 1: 7.314063%\n",
" 2: 0.184375%\n",
" 3: 0.001563%\n"
]
}
],
"source": [
"from itertools import product\n",
"from functools import reduce\n",
"from operator import mul\n",
"\n",
"# Gather a list of possible outcomes\n",
"outcomes = list(product((1-C, C), repeat=rolls))\n",
"print(\"Outcome chances:\")\n",
"display(outcomes)\n",
"\n",
"# Add up chances for number of mutations in each outcome\n",
"mut_count_chances = {n: 0 for n in range(rolls+1)}\n",
"for outcome in outcomes:\n",
" muts = sum(1 for chance in outcome if chance == C)\n",
" outcome_chance = reduce(mul, outcome, 1)\n",
" mut_count_chances[muts] += outcome_chance\n",
" \n",
"print(\"Chance of exact number of mutations:\")\n",
"for muts,chance in mut_count_chances.items():\n",
" print(f\" {muts}: {chance*100:9.6f}%\")\n",
"\n",
"print(f\"\\nTotal: {sum(mut_count_chances.values())} (should ~= 1)\")\n",
"print(\" (floating point accuracy makes this unlikely to be exact)\\n\")\n",
"\n",
"# Add chance of greater mutations for 'at least N mutations'\n",
"at_least_chances = dict()\n",
"for muts in range(1, rolls+1):\n",
" at_least_chances[muts] = sum(mut_count_chances[n] for n in range(muts, rolls+1))\n",
" \n",
"print(\"Chance of 'at least N' mutations:\")\n",
"for muts,chance in at_least_chances.items():\n",
" print(f\" {muts}: {chance*100:9.6f}%\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Simulate 10 million runs to verify"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Mutations in simulation:\n",
" 0: 92.686610%\n",
" 1: 7.128360%\n",
" 2: 0.183400%\n",
" 3: 0.001630%\n"
]
}
],
"source": [
"from collections import Counter\n",
"from random import random\n",
"\n",
"counter = Counter()\n",
"\n",
"runs = 10_000_000\n",
"for i in range(runs):\n",
" muts = sum(random() <= C for roll in range(rolls))\n",
" counter.update({muts: 1})\n",
" \n",
"print(\"Mutations in simulation:\")\n",
"for roll in range(rolls+1):\n",
" print(f\" {roll}: {counter[roll]/runs*100:9.6f}%\")"
]
}
],
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment