alexpotato 3 hours ago

> After running 10 million simulated draws, I got an answer of 0.0323, which matches the real answer very closely.

If you enjoyed reading about how you can approximate complex statistical values using basic programming, I highly recommend Jake Vanderplas "Statistics for Hackers" [0][1].

It converts the sometimes hard to grok statistics terms and formulas into some very simple loops and basic operations.

Can't recommend it enough.

0 - https://speakerdeck.com/pycon2016/jake-vanderplas-statistics...

1 - https://www.youtube.com/watch?v=Iq9DzN6mvYA

_Bruno42 2 hours ago

I was working on a retirement planning simulation this week with Opus and it added this simulation into my tool! Had never heard of Monte-Carlo before but was fairly impressed.

charlieyu1 1 hour ago

If you are doing combinatorial sampling like estimating pi or doing probabilities with SET, stratification is a great idea

stevenhubertron 2 hours ago

I use a Monte-Carlo for my retirement planning. This did a good job of telling me not to trust it but didn't really give me a better option. Anyone have one?

  • Jtsummers 2 hours ago

    > This did a good job of telling me not to trust it

    What do you mean? The retirement planning with Monte-Carlo gave a better result than the base assumption of 4% returns and 2% inflation. The information MC provides let's you, when taking into account your risk threshold, whether you're satisfied with your savings or not by giving you more realistic odds of how long the money will last.

  • lastofus 1 hour ago

    There's no sure fire option for making sure you don't run out of money, as nothing can 100% predict the future. The best we have is the assumption that the future will look kinda like the past over long periods of time.

    If you acknowledge that assumption, then you have to choose what model of the historical market you want to use a predictor for the future.

    You can use exact market data, test retiring out at every single possible year at every possible asset allocation and see what happens. This is the basis of the 4% rule from Bill Bingham.

    You can pull random years out of history and string them together a bunch of times and see what happens with a given asset allocation. This is a naive Monte Carlo simulation.

    You can pull blocks of years and string them together, to acknowledge that there are repeating trends in years (what happens in one years influences the next) and do a Monte Carlo simulation which is a bit less naive.

    There are more esoteric models overly smart people have constructed, each of which will give you slightly different answers for a safe withdraw rate.

    At the end of the day though we are all just crossing our fingers, and hoping for a black swan catastrophe to not happen.

shoo 1 hour ago

Here's a few related ideas:

1) rejection sampling - this is the name to the general technique used in the first example, to estimate pi by sampling from a uniform distribution over a unit square (easy) and then rejecting the samples that fall outside of the distribution you're actually interested in (unit disk).

Another simple example of rejection sampling is if you want to randomly pick between 5 ordered options and only have a d6. Roll the d6. If you roll a 6, reject the roll & re-roll. If you roll 1-5, that's your choice.

2) the Metropolis-Hastings algorithm -- this is a Markov chain Monte Carlo algorithm. It can be useful in some applications to sample from a distribution that is difficult to sample from.

Suppose you have some system described by a state vector x & you know that the probability of the system being in state x, p(x), is proportional to q(x), where q is some function you can compute. If you could integrate q over x, to give Z := integral_{x in X} q(x) dx then you'd have p(x) = Z^-1 q(x)

For a concrete example, x might be a state vector of a physical system -- say a bunch of molecules in a box, where you can compute the energy of any given state u(x) and know the probability of x, p(x), is proportional to exp(-u(x)).

Metropolis-Hastings is neat as it gives you a way to sample from p even if you cannot compute Z or sample from q --- you just need to have some way to sample a proposed a new candidate state x' from x (this proposal distribution doesn't need to have any relationship to the distributions p & q you're actually interested in). Can often think of the proposed candidate state x' as some kind of local mutation of x. Provided you can compute q(x') / q(x), you can use that ratio as a probability of accepting the move to replace x with candidate x'.

By the algorithm depending on the ratio of probabilities of two different states p(x') / p(x) = q(x') / q(x), it lets you sample from p even if you can't compute Z or have any direct way to sample from p or q. *

Metropolis-Hastings & Markov Chain Monte Carlo methods are definitely in the territory of clever 'what to do if you don't know what to do' methods --- if there's some other more direct & practical way of computing the thing you're trying to compute, use that instead! But if there's not, & you have some gnarly problem, maybe MCMC might be the best option available.

* caveat caveat caveat: provided you let the sampler run long enough for it to 'burn in', & perhaps restart it a few times from different initial states x_0 to reduce the chance of it getting trapped in some unrepresentative local optima

positron26 2 hours ago

> Can't differentiate the function? Fuck gradients. Here's the entire fucking posterior.

- Monte Carlo