-
Notifications
You must be signed in to change notification settings - Fork 11
/
utility_functions.pyx
34 lines (24 loc) · 1.25 KB
/
utility_functions.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import numpy as np
import scipy.special as sp
from libc.math cimport exp, log
from scipy.stats import norm
cimport cython
# compute the mean of the values above a given percentile (0 to 1) for a standard normal distribution
# this gives the surface scalar coefficient for a single updraft or nth updraft of n updrafts
cpdef double percentile_mean_norm(double percentile, Py_ssize_t nsamples):
cdef:
double [:] x = norm.rvs(size=nsamples)
double xp = norm.ppf(percentile)
return np.ma.mean(np.ma.masked_less(x,xp))
# compute the mean of the values between two percentiles (0 to 1) for a standard normal distribution
# this gives the surface scalar coefficients for 1 to n-1 updrafts when using n updrafts
cpdef double percentile_bounds_mean_norm(double low_percentile, double high_percentile, Py_ssize_t nsamples):
cdef:
double [:] x = norm.rvs(size=nsamples)
double xp_low = norm.ppf(low_percentile)
double xp_high = norm.ppf(high_percentile)
return np.ma.mean(np.ma.masked_greater(np.ma.masked_less(x,xp_low),xp_high))
cdef double interp2pt(double val1, double val2) nogil:
return 0.5*(val1 + val2)
cdef double logistic(double x, double slope, double mid) nogil:
return 1.0/(1.0 + exp( -slope * (x-mid)))