Trying to use Cumulative Distribution Function in GSL - c

Hey guys, I'm trying to compute the cumulative distribution function of the standard normal distribution for a formula in C using the GSL (Gnu Statistics Library)
I've installed and included gsl but am having trouble understanding how to use it.
I think the function I need is:
double gsl_ran_lognormal (const gsl_rng * r, double zeta, double sigma)
The formula I have only has one number that I would pass into a cdf function so I'm not quite sure what to do here. (This is probably because of my crappy understanding of statistics)
I would appreciate it anyone could lend me a hand on how to get the cdf using gsl with one input variable.
Documentation only says:
This function returns a random variate from the lognormal distribution. The distribution function is,
p(x) dx = {1 \over x \sqrt{2 \pi \sigma^2} } \exp(-(\ln(x) - \zeta)^2/2 \sigma^2) dx
for x > 0.
Basically, could someone explain what gsl_rng, zeta, and sigma should be?
EDIT: Ok, I think that zeta should be 0 (mu) and sigma should be 1 (std dev) to make it normal? Is that right? What is gsl_rng?

gsl_rng is a pointer to an initialized (and possible custom seeded) random number generator.
See for example http://www.csse.uwa.edu.au/programming/gsl-1.0/gsl-ref_16.html

Tyler,
I hope your problem is solved already. I am not a programming guru myself but I try to help. I think there are several points.
What you need is gsl_cdf_gaussian_P. The other thing (gsl_ran_lognormal) is inappropriate for two reasons.
1)It is a random number generator and not a cumulative distribution. That means it gives you numbers following a particular distribution, rather than a probability, as you need it.
2)It refers to the lognormal distribution, while you want the normal one.
Once you have a normal, cumulative distribution you can put the mean to 0 and the variance to unity to make it standard normal.
I hope this clarifies the situation. If not, I am here again in the morning.

Your function is for generating a random number with a lognormal distribution. If you are looking for the cumulative distribution you need to look in the "Special Functions" section of the GSL manual, section 7.15.

Related

Eigenvectors, eigenvalues fixed point calculation in C

** Edited **
I tried changing the mentioned Jacobi algorithm to fixed point using libfixmath but I am not getting right results. What did I miss??
edited Jacobi code
makefile
C newbie here. I somehow got my self in the deep and i cant find my way out. If you could help that would be awesome!
The situation: I am trying to implement an ICA algorithm in C. I did so using floating point arithmetic (double, float). Now I want this code to transform it to fixed point so I can import it on an ARM microcontroller of 32 bits (thats why i cant use double, float etc).
I have found four libraries that I think can help me:
http:// sourceforge.net/projects/avrfix/files/
http:// www.dsprelated.com/showcode/40.php
http:// sourceforge.net/p/fixedptc/code/ci/default/tree/
libfixmath
I didnt use 1. 2. or 3. I am currently trying libfixmath because allmost all calculations are done with matrices.
My problem is when trying to find the eigenvalues and eigenvectors of a covariance matrix (positive symmetic 3x3 matrix). I searched around for libs or functions that do eigendecomposition or SVD etc. but i didnt find anything.
How to you do that sort of calculation in fixed point??
Are there any functions/libs that i didnt find out? Do I have to alter the eigen function that I have in floating-point (line by line converting to fixed point - i.e fix16_from_dbl() )?
My current eigen function (not mine of course i think it is from Numerical Recipes)
jacobi.h
Other relevant question:
Here's StackOverflow fixed SVD
****Is my first question if there is anything to correct in my question please say so....dont eat me alive! :)
** Edited **
libfixmath does Cholesky1, wiki2 and QR decomposition.
I tried to play with maths and produce the eigenvectors or eigenvalue of a matrix with this data (data from the above functions) but I failed.
If anyone knows how to do that then problem solved.
*Should I post it to math stack website?
1 http:// rosettacode.org/wiki/Cholesky_decomposition
2 http:// en.wikipedia.org/wiki/Cholesky_decomposition#The_Cholesky.E2.80.93Banachiewicz_and_Cholesky.E2.80.93Crout_algorithms
How about the GNU Scientific Library? It has a number of functions related to eigenvalue decomposition
You could use something like IQMath in order to convert from floating-point to fixed-point. There are also a number of answers for a similar question here

GCD computation issues with GNU MP Library

I have a question about GNU MP, could you please help me how to proceed with that.
I using "The GNU Multiple Precision Arithmetic Library" Edition 5.1.1 on Windows.
(MinGW\gcc + MSYS)
There is an mpz_gcd function exist to calculate "gcd" of two integers.
void mpz_gcd (mpz_t rop, mpz_t op1, mpz_t op2);
As far as I get from documentation, there are couple of algorithms implemented in GNU MP for computation of Greatest Common Divisor. Among them:
Binary GCD
Lehmer’s algorithm
Subquadratic GCD
The algorithm that is used seems to be selected automatically, based on the input size of integers.
Currently, the binary algorithm is used for GCD only when N < 3.
For inputs larger than GCD_DC_THRESHOLD, GCD is computed via the HGCD (Half GCD) function
as a generalization to Lehmer’s algorithm.
So, I guess there are at least three different approaches to get gcd(a, b).
The main problem for me: I want to specify which algorithm to use by myself.
I would compare time execution of these algorithms on the random large input (i.e. 10^5 bits) to find out some common trends: what is that point where using "Binary GCD" becomes worse than "Lehmer's method", is "HGCD-Lehmer generalization" is really better than straightforward Lehmer, etc.
Is there any simple way to specify the algorithm you want to use? Any way to pull out this algorithm from the library, any way to modify some "#define" variables. Is it possible to do something like I want without library recompilation? I'm just beginner there and I don't feel able to figure out all sort of things inside the library.
P.S. Probably somebody would be interested what's will come out of that.
I've got some code on the github: https://github.com/int000h/gcd_gcc
This is a good time to be reading source code. GMP is open source—take advantage of that!
In mpn/generic/gcd.c you'll find the function which selects the GCD algorithm (this is actually a public function, it appears in the documentation):
mp_size_t
mpn_gcd (mp_ptr gp, mp_ptr up, mp_size_t usize, mp_ptr vp, mp_size_t n)
{
...
if (ABOVE_THRESHOLD (n, GCD_DC_THRESHOLD)) {
...
You can see that there are three main branches to the function, each ending with a return statement. Each branch corresponds to a different GCD algorithm. You can copy and paste the code into your own application and modify it so you can specify exactly which algorithm you want. Tips:
You can get rid of the #ifdefs. Assume TUNE_GCD_P is not defined.
This is an mpn_* function instead of an mpz_* function. It's lower-level: you have to explicitly allocate space for outputs, for example. You may also wish to copy the code from the higher-level function, mpz_gcd().
You'll need to extract prototypes for the internal functions, like mpn_hgcd_matrix_adjust(). Just copy the prototypes out of the GMP source code. Don't worry, internal functions appear to be exported from the shared library (they generally shouldn't be, but they are, so you're fine).
No need to recompile the library, but you will need to do a little bit of work here.

calculating double integrals in R quickly

I'm looking for a solution for a double integral that is faster than
integrate(function(y) {
sapply(y, function(y) {
integrate(function(x) myfun(x,y), llim, ulim)$value
})
}, llim, ulim)
with eg
myfun <- function(x,y) cos(x+y)
llim <- -0.5
ulim <- 0.5
I found an old paper that referred to a FORTRAN program called quad2d, but I couldn't find anything else but some help pages for matlab for the rest. So I'm looking for a C or FORTRAN library that can do double integrals quick (i.e. without the sapply loop), and that can be called from R. All ideas are very much appreciated, as long as they're all GPL compatible.
If the solution involves calling other functions from the libraries that are already shipped with R, I'd love to hear from them as well.
The cubature package does 2D (and N-D) integration using an adaptive algorithm. It should outperform simpler approaches for most integrands.
The pracma package that Joshua pointed out contains a version of quad2d.
quad2d(myfun, llim, ulim, llim, ulim)
This gives the same answer, within numerical tolerance, as your loop, using the example function.
By default, with your example function, quad2d is slower than the loop. If you drop n down, you can make it faster, but I guess it depends upon how smooth your function is, and how much accuracy you are willing to sacrifice for speed.

generating random number with a specific distribution in c

i need a library with functions for generating random number, given average, standard deviation and using one of three distribution - exponential, normal or unified.
even one of the three would help.
i'm looking for something like this - http://www.codeproject.com/KB/recipes/zigurat.aspx, but in c.
thanks
May I recommend the GNU Scientific Library either for use or for inspiration? It has several Random Number Distributions and is designed to be used from C and C++.
uniform:
Generate a random number in the range [0,1] with uniform distribution:
double X=((double)rand()/(double)RAND_MAX);
Exponentional
generating an exponentional random variable with parameter lambda:
-ln(U)/lambda (where U~Uniform[0,1]).
normal:
the simplest way [though time consuming] is using the central limit theorem, [sum enough uniformly distributed numbers] but there are other methods in the wikipedia page such as the box muller transform that generates 2 independent random variables: X,Y~N(0,1)
X=sqrt(-2ln(U))*cos(2*pi*V)
Y=sqrt(-2ln(U))*sin(2*pi*V)
where U,V~UNIFORM[0,1]
transforming from X~N(0,1) to Z~N(m,s^2) is simple: Z = s*X + m
Though you CAN generate these random numbers, I stand by #Amigable Clark Kant suggestion to use an existing library.

Eigenvector (Spectral) Decomposition

I am trying to find a program in C code that will allow me to compute a eigenvalue (spectral) decomposition for a square matrix. I am specifically trying to find code where the highest eigenvalue (and therefore its associated eigenvalue) are located int the first column.
The reason I need the output to be in this order is because I am trying to compute eigenvector centrality and therefore I only really need to calculate the eigenvector associated with the highest eigenvalue. Thanks in advance!
In any case I would recommend to use a dedicated linear algebra package like Lapack (Fortran but can be called from C) or CLapack. Both are free and offer routines for almost any eigenvalue problem. If the matrix is large it might be preferable to exploit its sparseness e.g. by using Arpack. All of these libraries tend to sort the eigenvectors according to the eigenvalues if they can (real or purely imaginary eigenvalues).
See the book "Numerical recipes in C"
And the #1 google hit (search: eigenvalue decomposition code C#)
http://crsouza.blogspot.com/2010/06/generalized-eigenvalue-decomposition-in.html
does not help?

Resources