double discretegeometricaverage(int T, int M, int S0, int K, double sigma, double r){ double dt=(double)T/M; double T1=T-(M*(M-1)*(4*M+1))/(6.*M*M)*dt; double T2=T-(M-1)/2.*dt; double A=exp(-r*(T-T2)-sigma*sigma*(T2-T1)/2.); double d=(log((double)S0/K)+(r-0.5*sigma*sigma)*T2)/(sigma*sqrt(T1)); return S0*A*gsl_cdf_gaussian_P(d+sigma*sqrt(T1),1)-K*exp(-r*T)*gsl_cdf_gaussian_P(d,1); }
double Model::n1PDF(double x, void *params) { Eigen::VectorXd d = *(Eigen::VectorXd *)params; if (fabs(x) < 1e-10) { return 0; } // fptpdf and fptcdf assume these parameter values (i.e., x0max = 1, chi = 2, // sdI = 1), so don't change them. const double x0max = 1; const double chi = 2; const double sdI = 1; double res = fptpdf(x, x0max, chi, d(0), sdI); for (unsigned int i = 1; i < d.size(); i++) { res *= 1 - fptcdf(x, x0max, chi, d(i), sdI); } double normaliser = 1; for (unsigned int i = 0; i < d.size(); i++) { normaliser *= gsl_cdf_gaussian_P(-d(i), sdI); } return res / (1 - normaliser); }
void Xtest_eval(Xtest *xtest) { /* * This routine evaluates the p-value from the xtest data. * x, y, sigma all must be filled in by the calling routine. */ /* xtest->pvalue = 0.5*gsl_sf_erfc((xtest->y - xtest->x)/(sqrt(2.0)*xtest->sigma)); xtest->pvalue = 1 - 0.5*gsl_sf_erfc((xtest->y - xtest->x)/(sqrt(2.0)*xtest->sigma)); xtest->pvalue = gsl_sf_erfc(fabs(xtest->y - xtest->x)/(sqrt(2.0)*xtest->sigma)); */ xtest->pvalue = gsl_cdf_gaussian_P(xtest->y - xtest->x,xtest->sigma); if(verbose == D_XTEST || verbose == D_ALL){ printf("# Xtest_eval(): x = %10.5f y = %10.5f sigma = %10.5f\n", xtest->x, xtest->y, xtest->sigma); printf("# Xtest_eval(): p-value = %10.5f\n",xtest->pvalue); } }
/* ** Draw randomly from gaussian distribution for every cell */ void gaussian(float mean, float stdev, char type) { printf("# Dysfunction gaussian(%0.4f, %0.4f) of ",mean, stdev); switch (type) { case DYSFUNCTION_HEIGHT : printf("height\n"); break; case DYSFUNCTION_WIDTH : printf("spatial\n"); break; case DYSFUNCTION_FIRE : printf("fire\n"); break; } printf("# Note indices start at 0, and dimensions of gcell array are %d * %d\n",X,Y); for(int x = 0 ; x < X ; x++) for(int y = 0 ; y < Y ; y++) { float value; if (stdev == 0) value = mean; else { // XXX simple linear search with function calls. Can be sped up heaps! value = (double)rand()/(double)RAND_MAX; double p; for(p = -mean ; p < 1 - mean; p += 0.00001) if (gsl_cdf_gaussian_P(p, stdev) >= value) break; value = mean + p; } switch (type) { case DYSFUNCTION_HEIGHT : printf("%3d %3d h %f\n",x,y,value); break; case DYSFUNCTION_WIDTH : printf("%3d %3d w %f\n",x,y,value); break; case DYSFUNCTION_FIRE : printf("%3d %3d f %f\n",x,y,value); break; } } }//gaussian()
double Model::fptpdf(const double &z, const double &x0max, const double &chi, const double &d, const double &sddrift) { const double zs = z * sddrift; const double zu = z * d; const double chiminuszu = chi - zu; const double chizu = chiminuszu / zs; const double chizumax = (chiminuszu - x0max) / zs; const double tmp1 = d * (gsl_cdf_gaussian_P(chizu, 1) - gsl_cdf_gaussian_P(chizumax, 1)); const double tmp2 = sddrift * (gsl_ran_gaussian_pdf(chizumax, 1) - gsl_ran_gaussian_pdf(chizu, 1)); double res = (tmp1 + tmp2) / x0max; return res; }
int main() { double bottom_tail; bottom_tail = gsl_cdf_gaussian_P(-1.96, 1); printf("Area between [-1.96, 1.96]: %g\n", 1-2*bottom_tail); return 0; }
double getpValue(double x,double mu,double a,double b,double ei) { double sigma=a*exp(-0.5*x)+b; ei-=mu; double p=0; ei=fabs(ei); p=gsl_cdf_gaussian_P(ei,sigma); p=1-p; return p; }
double Gaussian::Cdf(size_t dim_, double val_) const{ double nDevs = (val_ - GetMean(dim_))/GetStDev(dim_); if (nDevs > fCdfCutOff) return 1; if(nDevs < -1 * fCdfCutOff) return 0; return gsl_cdf_gaussian_P(val_ - GetMean(dim_), GetStDev(dim_)); }
double Model::fptcdf(const double &z, const double &x0max, const double &chi, const double &d, const double &sddrift) { const double zs = z * sddrift; const double zu = z * d; const double chiminuszu = chi - zu; const double xx = chiminuszu - x0max; const double chizu = chiminuszu / zs; const double chizumax = xx / zs; const double tmp1 = zs * (gsl_ran_gaussian_pdf(chizumax, 1) - gsl_ran_gaussian_pdf(chizu, 1)); const double tmp2 = xx * gsl_cdf_gaussian_P(chizumax, 1) - chiminuszu * gsl_cdf_gaussian_P(chizu, 1); double res = 1 + (tmp1 + tmp2) / x0max; return res; }
double continuousgeometricaverage(int T, int szero, int K, double sigma, double r){ double d=(log((double)szero/K)+0.5*(r-0.5*sigma*sigma)*T)/(sigma*sqrt(T/3.)); return szero*exp(-0.5*(r+1./6*sigma*sigma)*T)*gsl_cdf_gaussian_P(d+sigma*sqrt(T/3.),1)-K*exp(-r*T)*gsl_cdf_gaussian_P(d,1); }
int main(int argc, char **argv){ distlist distribution = Normal; char msg[10000], c; int pval = 0, qval = 0; double param1 = GSL_NAN, param2 =GSL_NAN, findme = GSL_NAN; char number[1000]; sprintf(msg, "%s [opts] number_to_lookup\n\n" "Look up a probability or p-value for a given standard distribution.\n" "[This is still loosely written and counts as beta. Notably, negative numbers are hard to parse.]\n" "E.g.:\n" "%s -dbin 100 .5 34\n" "sets the distribution to a Binomial(100, .5), and find the odds of 34 appearing.\n" "%s -p 2 \n" "find the area of the Normal(0,1) between -infty and 2. \n" "\n" "-pval Find the p-value: integral from -infinity to your value\n" "-qval Find the q-value: integral from your value to infinity\n" "\n" "After giving an optional -p or -q, specify the distribution. \n" "Default is Normal(0, 1). Other options:\n" "\t\t-binom Binomial(n, p)\n" "\t\t-beta Beta(a, b)\n" "\t\t-f F distribution(df1, df2)\n" "\t\t-norm Normal(mu, sigma)\n" "\t\t-negative bin Negative binomial(n, p)\n" "\t\t-poisson Poisson(L)\n" "\t\t-t t distribution(df)\n" "I just need enough letters to distinctly identify a distribution.\n" , argv[0], argv[0], argv[0]); opterr=0; if(argc==1){ printf("%s", msg); return 0; } while ((c = getopt (argc, argv, "B:b:F:f:N:n:pqT:t:")) != -1){ switch (c){ case 'B': case 'b': if (optarg[0]=='i') distribution = Binomial; else if (optarg[0]=='e') distribution = Beta; else { printf("I can't parse the option -b%s\n", optarg); exit(0); } param1 = atof(argv[optind]); param2 = atof(argv[optind+1]); findme = atof(argv[optind+2]); break; case 'F': case 'f': distribution = F; param1 = atof(argv[optind]); findme = atof(argv[optind+1]); break; case 'H': case 'h': printf("%s", msg); return 0; case 'n': case 'N': if (optarg[0]=='o'){ //normal param1 = atof(argv[optind]); param2 = atof(argv[optind+1]); findme = atof(argv[optind+2]); } else if (optarg[0]=='e'){ distribution = Negbinom; param1 = atof(argv[optind]); param2 = atof(argv[optind+1]); findme = atof(argv[optind+2]); } else { printf("I can't parse the option -n%s\n", optarg); exit(0); } break; case 'p': if (!optarg || optarg[0] == 'v') pval++; else if (optarg[0] == 'o'){ distribution = Poisson; param1 = atof(argv[optind]); findme = atof(argv[optind+1]); } else { printf("I can't parse the option -p%s\n", optarg); exit(0); } break; case 'q': qval++; break; case 'T': case 't': distribution = T; param1 = atof(argv[optind]); findme = atof(argv[optind+1]); break; case '?'://probably a negative number if (optarg) snprintf(number, 1000, "%c%s", optopt, optarg); else snprintf(number, 1000, "%c", optopt); if (gsl_isnan(param1)) param1 = -atof(number); else if (gsl_isnan(param2)) param2 = -atof(number); else if (gsl_isnan(findme)) findme = -atof(number); } } if (gsl_isnan(findme)) findme = atof(argv[optind]); //defaults, as promised if (gsl_isnan(param1)) param1 = 0; if (gsl_isnan(param2)) param2 = 1; if (!pval && !qval){ double val = distribution == Beta ? gsl_ran_beta_pdf(findme, param1, param2) : distribution == Binomial ? gsl_ran_binomial_pdf(findme, param2, param1) : distribution == F ? gsl_ran_fdist_pdf(findme, param1, param2) : distribution == Negbinom ? gsl_ran_negative_binomial_pdf(findme, param2, param1) : distribution == Normal ? gsl_ran_gaussian_pdf(findme, param2)+param1 : distribution == Poisson ? gsl_ran_poisson_pdf(findme, param1) : distribution == T ? gsl_ran_tdist_pdf(findme, param1) : GSL_NAN; printf("%g\n", val); return 0; } if (distribution == Binomial){ printf("Sorry, the GSL doesn't have a Binomial CDF.\n"); return 0; } if (distribution == Negbinom){ printf("Sorry, the GSL doesn't have a Negative Binomial CDF.\n"); return 0; } if (distribution == Poisson){ printf("Sorry, the GSL doesn't have a Poisson CDF.\n"); return 0; } if (pval){ double val = distribution == Beta ? gsl_cdf_beta_P(findme, param1, param2) : distribution == F ? gsl_cdf_fdist_P(findme, param1, param2) : distribution == Normal ? gsl_cdf_gaussian_P(findme-param1, param2) : distribution == T ? gsl_cdf_tdist_P(findme, param1) : GSL_NAN; printf("%g\n", val); return 0; } if (qval){ double val = distribution == Beta ? gsl_cdf_beta_Q(findme, param1, param2) : distribution == F ? gsl_cdf_fdist_Q(findme, param1, param2) : distribution == Normal ? gsl_cdf_gaussian_Q(findme-param1, param2) : distribution == T ? gsl_cdf_tdist_Q(findme, param1) : GSL_NAN; printf("%g\n", val); } }