from python.f01 import *
%matplotlib inline

interact(plot_norm)

"""
Explanation: Session 01: An introduction to modelling
What is the point of mathematical modelling?

"Mathematical models are not helpful for biology because biology is just too complex."

Counter-arguments:

mathematics is good for predicting complex behaviours
emergent properties
cost (time)
cost (money)
systems that we couldn't otherwise access
systems that we couldn't otherwise record

Aims

to predict
quantify
you can't measure everything
to justify expense
cost
useful
test
plan
ethics

Scale

quantum
atoms
angstroms
molecular
multi-molecular
proteins
multi-proteins, channels, ribosomes
organelles
cellular
populations of cells
organs/tissues
multi-organ
organisms, whole-body
host-parasite
communities
populations
interacting populations
ecosystems
planetary ecosystems

Approach

verbal / text
internal (brain)
diagrams
physical models (plastic kits)
physical models (replica)
statistical
chemical reaction schemes
ordinary differential equations
stochastic
partial
Monte-Carlo
algebraic equations
numerical/theoretical
logical
spatial
Markov chains, MCMC
rule-based
game of life
game theory
agent based
engineering

Simplifications
Simplifying things that you know are true.

1D/2D
assume things are small
ignore redundancy
assuming nothing else is happening
complexity doesn't change
classical physics

Assumptions
Assuming things that you don't know enough about.

things distributed to normal distribution
assuming constant
consistancy
A interacts with B

Quotes

[T]he best material model for a cat is another, or preferably the same, cat. -- Norbert Wiener / Arturo Rosenblueth

Scope is difficult to determine, external elements will always have an influence. Organisms have different phenotypes.

The purpose of models is not to fit the data but to sharpen the questions. -- Samuel Karlin
To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. -- Ronald Fisher

Modelling should be a component of the scientific process, and not simply a last minute addition.

With four parameters I can fit an elephant, and with five I can make him wiggle his trunk. -- John von Neumann

'The parameter problem', given enough freedom it's easy for a system to be fitted to any data -- so how do we know the predictive power is good.

If the difficulty of a physiological problem is mathematical in essence, ten physiologists ignorant of mathematics will get precisely as far as one physiologist ignorant of mathematics, and no further. If a physiologist who knows no mathematics works together with a mathematician who knows no physiology, the one will be unable to state his problem in term that the other can manipulate, and the second will be unable to put the answers in any form that the first can understand. -- Norbert Wiener

Communication between scientists is difficult due to different terminologies, etc.
Monte-Carlo modelling

Monte Carlo originated as a form of emergency first aid, in answer to the question: What to do until the mathematician arrives? — George Dyson
Monte Carlo methods (or Monte Carlo experiments) are a broad class of computational algorithms that rely on repeated random sampling to obtain numerical results. They are often used in physical and mathematical problems and are most useful when it is difficult or impossible to use other mathematical methods. Monte Carlo methods are mainly used in three distinct problem classes: optimization, numerical integration, and generating draws from a probability distribution. — Wikipedia

Example, the normal distribution
End of explanation
"""


n_individuals = 10000

# generate number of fish in each diet
exp_fish = 20
n_fish = np.random.poisson(exp_fish, size=n_individuals)

# plot histogram of number of fish consumed
plot_n_fish(n_fish)

# assign fish to diet based on probabilities
fish  = ['herring', 'mackerel', 'sardine', 'squid']
prob  = [0.3,       0.2,        0.45,       0.05]
diets = [list(np.random.choice(fish, p=prob, size=n)) for n in n_fish]

# calculate calories
calories = {'herring': 45, 'mackerel': 200,
            'sardine':25, 'squid': 175}
intakes = [sum(calories[f] for f in diet) for diet in diets]

# plot histogram
plot_intakes(intakes)

print("Mean intake: %d" % np.mean(intakes))
print("Std. Dev.:   %d" % np.std(intakes))

"""
Explanation: 'Rule based' modelling
We're worried about the survival of dolphins due to reducing fish stocks.
We have an estimate for the number of fish a dolphin consumes in one day (from observational evidence).
We also know the proportion of the types of food dolphins consume, and the average calorific value of each fish.

Food     | Probability    | Calories
---------| -------------- | --------
Herring  | $\frac{6}{20}$ | 45 kcal
Mackerel | $\frac{4}{20}$ | 200 kcal
Sardine  | $\frac{9}{20}$ | 25 kcal
Squid    | $\frac{1}{20}$ | 175 kcal
End of explanation
"""


# https://youtu.be/rLUAoTjdmZ8
from IPython.display import YouTubeVideo
YouTubeVideo("rLUAoTjdmZ8", width=600, height=400)

"""
Explanation: Exercise, Hermit crabs
End of explanation
"""


# Jupyter notebook setup
from IPython.core.display import HTML
HTML(open("../styles/custom.css", "r").read())

"""
Explanation: Notebook setup
End of explanation
"""


