Giter VIP home page Giter VIP logo

nlib's Introduction

Annotated Algorithms in Python (3.8)

With applications in Physics, Biology, and Finance

The complete book in PDF is now available under a Creative Commons BY-NC-ND License:

DOWNLOAD BOOK IN PDF

The book is also available in printed form from Amazon:

Amazon

The nlib library

The book builds a numerical library from the ground up, called src/nlib.py. It is a pure python library for numerical computations. It doesn't require numpy.

Usage

>>> from nlib import *

Linear algebra example

>>> A = Matrix([[1,2],[4,9]])
>>> print 1/A 
>>> print (A+2)*A
>>> B = Matrix(2,2,lambda i,j: i+j**2)

Fitting

>>> points = [(x0,y0,dy0), (x1,y1,dy1), (x2,y2,dy2), ...]
>>> coefficients, chi2, fitting_function = fit_least_squares(points,POLYNOMIAL(2))
>>> for x,y,dy in points:
>>>     print x, y, '~', fitting_function(x)

Solvers

>>> from math import sin
>>> def f(x): return sin(x)-1+x
>>> x0 = solve_newton(f, 0.0, ap=0.01, rp=0.01, ns=100)
>>> print 'f(%s)=%s ~ 0' % (x0, f(x0))

(ap is target absolute precision, rp is target relative precision, ns is max number of steps)

Optimizers

>>> def f(x): return (sin(x)-1+x)**2
>>> x0 = optimize_newton(f, 0.0, ap=0.01, rp=0.01, ns=100)
>>> print 'f(%s)=%s ~ min f' % (x0, f(x0))    
>>> print 'f'(%s)=%s ~ 0' % (x0, D(f)(x0))    

Statistics

>>> x = [random.random() for k in range(100)]
>>> print 'mu     =', mean(x)
>>> print 'sigma  =', sd(x)
>>> print 'E[x]   =', E(lambda x:x,    x)
>>> print 'E[x^2] =', E(lambda x:x**2, x)
>>> print 'E[x^3] =', E(lambda x:x**3, x)
>>> y = [random.random() for k in range(100)]
>>> print 'corr(x,y) = ', correlation(x,y)
>>> print 'cov(x,y)  = ', covariance(x,y)

Finance

>>> google = YStock('GOOG')
>>> current = google.current()
>>> print current['price']                                                                          
>>> print current['market_cap']                                                                
>>> for day in google.historical():
>>>     print day['date'], day['adjusted_close'], day['log_return']

Persistant Storage

>>> d = PersistentDictionary(path='test.sqlite')
>>> d['key'] = 'value'
>>> print d['key']
>>> del d['key']

d works like a drop-in preplacement for any normal Python dictionary except that the data is stored in a sqlite database in a file called "test.sqlite" so it is still there if you re-start the program. Kind of like the shelve module but shelve files cannot safely be accessed by multiple threads/processes unless locked and locking the entire file is not efficient.

Neural Network

>>> pat = [[[0,0], [0]], [[0,1], [1]], [[1,0], [1]], [[1,1], [0]]]
>>> n = NeuralNetwork(2, 2, 1)
>>> n.train(pat)
>>> n.test(pat)
[0, 0] -> [0.00...]
[0, 1] -> [0.98...]
[1, 0] -> [0.98...]
[1, 1] -> [-0.00...]

Plotting

>>> data = [(x0,y0), ...]
>>> Canvas(title='my plot').plot(data, color='red').save('myplot.png')

nlib plotting requires matplotlib/numpy for the Canvas object only plots are chainable. methods: .plot, .hist, .errorbar, .ellipses

Complete list of functions/classes

CONSTANT
CUBIC
Canvas
Cholesky
Cluster
D
DD
Dijkstra
DisjointSets
E
Ellipse
HAVE_MATPLOTLIB
Jacobi_eigenvalues
Kruskal
LINEAR
MCEngine
MCG
Markowitz
MarsenneTwister
Matrix
NeuralNetwork
POLYNOMIAL
PersistentDictionary
Prim
PrimVertex
QUADRATIC
QUARTIC
QuadratureIntegrator
RandomSource
StringIO
Trader
YStock
bootstrap
breadth_first_search
compute_correlation
condition_number
confidence_intervals
continuum_knapsack
correlation
covariance
decode_huffman
depth_first_search
encode_huffman
fib
fit
fit_least_squares
gradient
hessian
integrate
integrate_naive
integrate_quadrature_naive
invert_bicgstab
invert_minimum_residual
is_almost_symmetric
is_almost_zero
is_positive_definite
jacobian
lcs
leapfrog
make_maze
mean
memoize
memoize_persistent
needleman_wunsch
norm
optimize_bisection
optimize_golden_search
optimize_newton
optimize_newton_multi (multi-dimentional optimizer)
optimize_newton_multi_imporved
optimize_secant
partial
random
resample
sd
solve_bisection
solve_fixed_point
solve_newton
solve_newton_multi (multi-dimensional solver)
solve_secant
variance

License

Created by Massimo Di Pierro ([email protected]) @2016 BSDv3 License

nlib's People

Contributors

adl1995 avatar e5l avatar hay avatar lowks avatar mac-chaffee avatar mdipierro avatar tianrenli92 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nlib's Issues

Does it numba?

Although probably very slow in pure Python the benefit of not using numpy is that the code can potentially be JIT compiled via numba or pypy to speed things up. Just curious if this has been explored at all and how it benchmarks against numpy.

Exponential sampling is wrong

Formula (6.67, 6.68). Statement that -log(1-u) is equivalent to -log(u) is true iff u is U(0,1) in the (0,1) range. In reality Python U(0,1) as well as C/C++ U(0,1) are defined in the interval [0,1). Thus, -log(1-u) will always work while -log(u) sometimes will produce FP exceptions when 0 is sampled

Monte Carlo/Statistics symbols consistency

In general, convention for Statistics and Monte Carlo is as following:

  • Distribution parameters are marked with Greek letters/symbols
  • Sampling results are marked with Latin letters/symbols

$\mu$ vs $m$, and $\sigma$ vs $s$
For example,

distribution parameters, in Greek

mu = 0.0
sigma = 1.0
v = np.random.normal(mu, sigma, 1000)

Sampling parameters, in Latin

m = np.mean(v)
s = np.std(v)

Update license link

License link in the README doesn't seem to work as the URL has been moved. Consider it to change it to it's updated location.

Python 3 support

It would be great to have python 3 supported. If it isn't a high priority for you and you don't know of any major issues with porting it to support python 3, I wouldn't mind helping with that.

Whitespace around equations and table style

Several equations and align environments have additional newlines before and/or after them.
If one makes the following substitutions to comment out those newlines: \n\n\begin{equation} -> \n%\n\begin{equation}, \end{equation}\n\n -> \end{equation}%\n\n (and similarly for align and alignat) -- commenting out the newlines prevents additional whitespace but keeps the source readable -- then the whitespace is reduced significantly:

ex6-comb

On making these changes, the book becomes 384 pages long instead of 388 pages.

Also, there is a \usepackage{booktabs} in the preamble but the tables drawn do not follow the booktabs manual; lots of horizontal and vertical lines are used. Here's what a table edited according to the booktabs style looks like compared to one of the current tables:

ex7-new

I will submit a PR if you like the suggested changes.

YStock not working?

Following code returns an empty array.

google = YStock('GOOG')
google.historical(start=datetime.date(2016,1,1), stop=datetime.date(2016,5,30))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.