Skip to content

Instantly share code, notes, and snippets.

@wiso
wiso / covariance_to_correlation.py
Created March 20, 2018 08:33
Compute correlation matrix from covariance matrix using numpy
import numpy as np
def correlation_from_covariance(covariance):
v = np.sqrt(np.diag(covariance))
outer_v = np.outer(v, v)
correlation = covariance / outer_v
correlation[covariance == 0] = 0
return correlation
@wiso
wiso / RoundingError.ipynb
Created October 9, 2017 10:40
Example of rounding error issues
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@wiso
wiso / loop_rooargset.py
Last active July 14, 2017 14:10
Loop on RooFit RooArgSet with python
def loop_iterator(iterator):
object = iterator.Next()
while object:
yield object
object = iterator.Next()
def iter_collection(rooAbsCollection):
iterator = rooAbsCollection.createIterator()
return loop_iterator(iterator)
@wiso
wiso / newton optimization.py
Last active June 6, 2017 18:19 — forked from rajarsheem/newton optimization.py
Newton's optimization method for multivariate function in tensorflow (updated to tf 1.1.0)
import numpy as np
import tensorflow as tf
# Newton's optimization method for multivariate function in tensorflow
def cons(x):
return tf.constant(x, dtype=tf.float32)
def compute_hessian(fn, vars):
mat = []
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@wiso
wiso / safe_roofit.py
Created May 9, 2017 08:38
Simple wrapper to make Roofit workspace manipulation more safe with python
def safe_factory(func):
def wrapper(self, *args):
result = func(self, *args)
if not result:
raise ValueError('invalid factory input "%s"' % args)
return result
return wrapper
ROOT.RooWorkspace.factory = safe_factory(ROOT.RooWorkspace.factory)
@wiso
wiso / CartPole-v0.py
Last active January 20, 2017 23:51
Very simple random search
# from http://kvfrans.com/simple-algoritms-for-solving-cartpole/
import gym
from gym import wrappers
import numpy as np
env = gym.make('CartPole-v0')
def run_episode(env, parameters):
observation = env.reset()
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@wiso
wiso / ttest_cpp.ipynb
Created March 23, 2016 15:41
Quick implementation of the t-test in c++, compared with scipy
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@wiso
wiso / chi2_updated
Last active March 22, 2016 10:47
chi2
The data have been binned in such a way that every bin contains more than 10 events. For every bin the integral of the S+B postfit pdf has been computed ($E_i$).
In the table the value of the Pearson-$\chi^2 = \sum_i (E_i - O_i)^2 / E_i$ is reported with the number of bins of $m_{\gamma\gamma}$.
Note that the $\chi^2$ is taking into account only the physical pdf, and not the product of constraints. In fact it is difficult to compute the number of degrees of freedom. We have 5 NPs for the background (4 "$\alpha$" + normalization) plus all the NPs for the statistical fluctuations (100+). Since these parameters are constrained they don't count -1 in the sum of the degree of freedom (something between 0 and -1). To try to evaluate their contribution we can imagine to add the constraint pdf to the computation of the $\chi^2$. This means to add 1 "bin", to subtract 1 dof, and to add a contribution to the $\chi^2$. This can (?) be evaluated as
$$-2\log (pdf(x | x_{true})) + 2\log(pdf(x_{true}|x_{true}))$$
for e