Popular recipes by alexander baker http://code.activestate.com/recipes/users/4166679/2016-04-13T11:53:21-07:00ActiveState Code RecipesHow to build dobble as a Mixed Integer program. (Python)
2016-04-13T11:53:21-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/580641-how-to-build-dobble-as-a-mixed-integer-program/
<p style="color: grey">
Python
recipe 580641
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/mixed/">mixed</a>, <a href="/recipes/tags/program/">program</a>).
</p>
<p>A simple script to replicate the cards and symbols for the dobble game.</p>
Simple FIFO trading model for pnl (Python)
2015-02-10T13:34:48-08:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/579024-simple-fifo-trading-model-for-pnl/
<p style="color: grey">
Python
recipe 579024
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/analysis/">analysis</a>, <a href="/recipes/tags/pnl/">pnl</a>, <a href="/recipes/tags/trading/">trading</a>).
Revision 2.
</p>
<p>Simple approach to calculating FIFO pnl.</p>
Simple way to find number of perfect square numbers in a range. (Python)
2015-03-05T15:59:30-08:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/579031-simple-way-to-find-number-of-perfect-square-number/
<p style="color: grey">
Python
recipe 579031
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/complex/">complex</a>, <a href="/recipes/tags/fun/">fun</a>, <a href="/recipes/tags/interview/">interview</a>, <a href="/recipes/tags/maths/">maths</a>, <a href="/recipes/tags/range/">range</a>).
</p>
<p>The strategy here is not to iterate through the set of possible integer values and check for is_perfect_square()
each time but to translate the upper and lower values to either complex or real space of square numbers.</p>
<pre class="prettyprint"><code> # O(n) complexity
len([x for x in range(0, 100) if x!= 0 and float(math.sqrt(x)).is_integer()])
so if a and b positive then we count the number of integer values between upper and lower sqrt() values
if either a or b are negative then we need to use the complex number space for the sqrt() results. In this case
we are still counting integer values either along the imaginary or real axis, the result is then a simple sum
if both a and b are negative we need to make sure that when walking down the same imaginary axis we dont count
the same inteters twice, in this case we can take the min(a,b) to get max distinct set of integer values.
</code></pre>
Simple model to predict UK Interest Rates (Python)
2014-06-13T14:29:36-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578891-simple-model-to-predict-uk-interest-rates/
<p style="color: grey">
Python
recipe 578891
by <a href="/recipes/users/4166679/">alexander baker</a>
.
</p>
<p>A common model used in the financial industry for modelling the short rate (think overnight rate, but actually an infinitesimally short amount of time) is the Vasicek model. #
Although it is unlikely to perfectly fit the yield curve, it has some nice properties that make it a good model to work with.
The dynamics of the Vasicek model are describe below.</p>
<p>In this model, the parameters are constants, and the random motion is generated by the Q measure Brownian motion .
An important property of the Vasicek model is that the interest rate is mean reverting to , and the tendency to revert is controlled by .
Also, this process is a diffusion process, hence Markovian, which will lead to some nice closed form formulas.
Finally, the future value of the interest rate is normally distributed with the distribution .</p>
Monte Carlo Engine : Stochastic Interest Rate Predictor (Python)
2014-04-27T16:42:29-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578867-monte-carlo-engine-stochastic-interest-rate-predic/
<p style="color: grey">
Python
recipe 578867
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/carlo/">carlo</a>, <a href="/recipes/tags/interest/">interest</a>, <a href="/recipes/tags/model/">model</a>, <a href="/recipes/tags/monte/">monte</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/rate/">rate</a>).
Revision 2.
</p>
<p>This is a simple Monte Carlo Engine model, based on some ideas from the Quantopian folks. The idea is that the model and the engine are separate. I have integrated a simple example from an earlier post. The idea was to try and build some game theory simulations using simple models that were separate from the Engine. </p>
Hartree Fock SCF (Python)
2013-04-30T09:09:20-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578515-hartree-fock-scf/
<p style="color: grey">
Python
recipe 578515
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/physics/">physics</a>).
Revision 4.
</p>
<p>Quantum Chemistry technique to calculate various interesting operators for HeH.</p>
Monte Carlo Engine : How to find the optimised wager for next bet, following a recent loss. (Python)
2014-04-28T08:41:49-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578869-monte-carlo-engine-how-to-find-the-optimised-wager/
<p style="color: grey">
Python
recipe 578869
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/optimise/">optimise</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tree/">tree</a>).
</p>
<p>Simple Engine to help understand how to best wager your next bet, given that you just made a loss. The engine uses the modified Powell method to optimise the weight to apply to your wager on the next position.</p>
<p>{'My Simple Heads And Tails Model': <BackTest.Simulation object at 0x0583D410>}
participants [100] survivors [90.0%] losers [10.0%] weight [0.073858]
solving for r: [ 0.07385806]
simulations 100, trials 100 starting pot 1000
calling initialise
{'My Simple Heads And Tails Model': <BackTest.Simulation object at 0x0583D430>}
participants [100] survivors [86.0%] losers [14.0%] weight [0.072220]
solving for r: [ 0.07221954]
Optimization terminated successfully.
Current function value: 8.000000
Iterations: 2
Function evaluations: 30
highest survivability following loss, multiply wager by 7.2949 %</p>
<h5 id="">.</h5>
<p>Ran 2 tests in 25.545s</p>
<p>OK</p>
Monte Carlo Engine : Simple Head Tail Model (Python)
2014-04-27T17:25:25-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578868-monte-carlo-engine-simple-head-tail-model/
<p style="color: grey">
Python
recipe 578868
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/carlo/">carlo</a>, <a href="/recipes/tags/monte/">monte</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/strategy/">strategy</a>).
Revision 2.
</p>
<p>Wanted to implement something that shows how the simple framework can be used to investigate a basic heads and tail game. The betting strategy can be varied simply by changing the logic in the ontrail() method. This currently tests the double or quits strategy. Run it to see how many </p>
Find the largest run of Heads or Tails with tolerance (Python)
2013-12-04T10:55:51-08:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578779-find-the-largest-run-of-heads-or-tails-with-tolera/
<p style="color: grey">
Python
recipe 578779
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/interview/">interview</a>, <a href="/recipes/tags/question/">question</a>).
</p>
<ul>
<li>You need to find the </li>
</ul>
Locate and see the biggest cross in a random matrix (Python)
2013-09-13T13:31:03-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578657-locate-and-see-the-biggest-cross-in-a-random-matri/
<p style="color: grey">
Python
recipe 578657
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/search/">search</a>).
</p>
<p>Interesting search space problem.</p>
Conway's Game of Life In Python (Python)
2013-06-13T15:33:41-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578559-conways-game-of-life-in-python/
<p style="color: grey">
Python
recipe 578559
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/life/">life</a>, <a href="/recipes/tags/numpy/">numpy</a>, <a href="/recipes/tags/of/">of</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>Conway's Game of Life In Python</p>
A simple KD Tree example with custom Euclidean distance ball query. (Python)
2013-01-24T10:28:24-08:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578434-a-simple-kd-tree-example-with-custom-euclidean-dis/
<p style="color: grey">
Python
recipe 578434
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>In computer science, a k-d tree (short for k-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space. k-d trees are a useful data structure for several applications, such as searches involving a multidimensional search key (e.g. range searches and nearest neighbor searches). k-d trees are a special case of binary space partitioning trees.</p>
<p><a href="http://en.wikipedia.org/wiki/K-d_tree" rel="nofollow">http://en.wikipedia.org/wiki/K-d_tree</a></p>
zero curve bootstrapping and forward curve generation (Python)
2012-09-10T08:07:56-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578257-zero-curve-bootstrapping-and-forward-curve-generat/
<p style="color: grey">
Python
recipe 578257
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/sympy/">sympy</a>).
</p>
<p>example of a bootstrapping and forward curve generation
this can be used to build a set of curves for different currencies</p>
Script that compares various Interest Rate term structure models. (Python)
2012-09-10T08:02:34-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578256-script-that-compares-various-interest-rate-term-st/
<p style="color: grey">
Python
recipe 578256
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>A common model used in the financial industry for modelling the short rate (think overnight rate, but actually an infinitesimally short amount of time) is the Vasicek model. #
Although it is unlikely to perfectly fit the yield curve, it has some nice properties that make it a good model to work with.
The dynamics of the Vasicek model are describe below.</p>
<p>In this model, the parameters are constants, and the random motion is generated by the Q measure Brownian motion .
An important property of the Vasicek model is that the interest rate is mean reverting to , and the tendency to revert is controlled by .
Also, this process is a diffusion process, hence Markovian, which will lead to some nice closed form formulas.
Finally, the future value of the interest rate is normally distributed with the distribution .</p>
Convolving Gaussian (Python)
2009-05-19T05:23:48-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/576764-convolving-gaussian/
<p style="color: grey">
Python
recipe 576764
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/mathematics/">mathematics</a>).
</p>
<p>Convolving Gaussian</p>
Brownian Motion of Stock (Python)
2009-05-19T05:16:29-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/576760-brownian-motion-of-stock/
<p style="color: grey">
Python
recipe 576760
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/finance/">finance</a>).
</p>
<p>Brownian Motion of a Stock</p>
Lancos Gamma Approximation (Python)
2009-05-19T05:19:15-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/576763-lancos-gamma-approximation/
<p style="color: grey">
Python
recipe 576763
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/gamma/">gamma</a>, <a href="/recipes/tags/mathematics/">mathematics</a>).
</p>
<p>Lancos Gamme Approximation</p>
Newton Raphson Root Finding (Python)
2009-05-19T05:18:00-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/576762-newton-raphson-root-finding/
<p style="color: grey">
Python
recipe 576762
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/numerical/">numerical</a>, <a href="/recipes/tags/scipy/">scipy</a>).
</p>
<p>Newton Raphson Root Finding</p>
Quick Numerical Integration (Python)
2009-05-19T05:24:37-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/576765-quick-numerical-integration/
<p style="color: grey">
Python
recipe 576765
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/integration/">integration</a>, <a href="/recipes/tags/scipy/">scipy</a>).
</p>
<p>Quick Numerical Integration</p>
Energy Distrubution of Black Body (Python)
2009-05-19T05:17:03-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/576761-energy-distrubution-of-black-body/
<p style="color: grey">
Python
recipe 576761
by <a href="/recipes/users/4166679/">alexander baker</a>
(<a href="/recipes/tags/black/">black</a>, <a href="/recipes/tags/body/">body</a>, <a href="/recipes/tags/energy/">energy</a>, <a href="/recipes/tags/physics/">physics</a>, <a href="/recipes/tags/pylab/">pylab</a>, <a href="/recipes/tags/scipy/">scipy</a>).
</p>
<p>Energy Distrubution of Black Body</p>