Skip to content

Instantly share code, notes, and snippets.

View rsnemmen's full-sized avatar

Rodrigo Nemmen rsnemmen

View GitHub Profile
@rsnemmen
rsnemmen / randomvariate.py
Last active September 15, 2022 05:46
Rejection method for random number generation / Python
def randomvariate(pdf,n=1000,xmin=0,xmax=1):
"""
Rejection method for random number generation
===============================================
Uses the rejection method for generating random numbers derived from an arbitrary
probability distribution. For reference, see Bevington's book, page 84. Based on
rejection*.py.
Usage:
>>> randomvariate(P,N,xmin,xmax)
@rsnemmen
rsnemmen / uncertainty.py
Created October 23, 2014 21:43
Doing statistical error propagation with Python
import uncertainties as unc
import uncertainties.unumpy as unumpy
import numpy
import nemmen
# Defines x and y
x=numpy.linspace(0,10,50)
y=numpy.linspace(15,20,50)
# Defines the error arrays, values follow a normal distribution
@rsnemmen
rsnemmen / random_normal.py
Created October 23, 2014 21:45
Generates random numbers following a normal distribution with arbitrary mean and standard deviation
def random_normal(mean,std,n):
"""
Returns an array of n elements of random variables, following a normal
distribution with the supplied mean and standard deviation.
"""
import scipy
return std*scipy.random.standard_normal(n)+mean
@rsnemmen
rsnemmen / progressbar.py
Created October 23, 2014 21:47
Simple progress bar with Python (terminal)
import fish
import time
steps=input('How many steps? ')
# Progress bar initialization
peixe = fish.ProgressFish(total=steps)
for i in range(steps):
# Progress bar
@rsnemmen
rsnemmen / wclatex.sh
Created October 28, 2014 19:20
Count words in latex document (does not count latex commands)
# Count words in a latex file, without taking into account
# the latex constructs
detex $file | wc
@rsnemmen
rsnemmen / pdf2gray.pl
Created November 4, 2014 17:07
Perl script to convert a color PDF to grayscale
#!/usr/bin/perl -w
#
# Source: http://handyfloss.net/2008.09/making-a-pdf-grayscale-with-ghostscript/
# handyfloss blog
#
# Usage: pdf2gray.pl input_color.pdf
use strict;
my $infile = $ARGV[0];
my $outfile = $infile;
@rsnemmen
rsnemmen / rotationcurves.txt
Created November 19, 2014 19:02
Simplified galaxy rotation curves with and without dark matter and their visual impact, Mathematica
Galaxy rotation curves in Mathematica
Author: Rodrigo Nemmen
Visually illustrating the impact of the presence of dark matter in speeding up the
rotation of test particles in galaxies
Input
nϕ=15;
rmin=1;rmax=50; nr=40;
tmin=0; tmax=15Pi;
@rsnemmen
rsnemmen / screenshots.sh
Created March 6, 2015 14:36
Captures screenshots at regular time intervals and wakes up computer from screensaver
#!/bin/bash
#
# Captures regular screenshots and saves them to dropbox.
# Useful when I leave my linux box doing number crunching
# over long periods.
#
# t = number of seconds in-between screenshots
t=5400 # 1.5 h
@rsnemmen
rsnemmen / confband.py
Created April 1, 2015 19:15
Calculates the confidence band of a linear regression model at the desired confidence level, using analytical methods
def confband(xd,yd,a,b,conf=0.95,x=None):
"""
Calculates the confidence band of the linear regression model at the desired confidence
level, using analytical methods. The 2sigma confidence interval is 95% sure to contain
the best-fit regression line. This is not the same as saying it will contain 95% of
the data points.
Arguments:
- conf: desired confidence level, by default 0.95 (2 sigma)
- xd,yd: data arrays
@rsnemmen
rsnemmen / scatterfit.py
Created April 1, 2015 19:16
Computes the mean deviation of the data about the linear model
def scatterfit(x,y,a=None,b=None):
"""
Compute the mean deviation of the data about the linear model given if A,B
(y=ax+b) provided as arguments. Otherwise, compute the mean deviation about
the best-fit line.
x,y assumed to be Numpy arrays. a,b scalars.
Returns the float sd with the mean deviation.
Author: Rodrigo Nemmen