Programming using Python language
There is no need to be a Python expert to use the Calculator, just follow the elements below:
-
Assigning a constant numerical value
-
Assigning a constant variable (alpha, categorical)
-
Copy of an Auxiliary Variable
-
Transformation example
-
If statement
The general Python syntax for a simple if statement is:
If the condition is true, then do the indented statements. If the condition is not true, then skip the indented statements.
-
Conditions
-
Logical operations
-
If-else statement
The general Python if-else syntax is:
These statement blocks can have any number of statements, and can include about any kind of statement.
-
Character strings
Test if a character "abc" string is included in another character string v1:
Compare two character strings (alphabetical order):
Length of a character string:
Get the 3rd character of the string:
Note: The first string character is coded as 0.
-
External Functions - numpy package
Numpy is the fundamental package for scientific computing with Python. The package is loaded as np in the calculator, meaning all functions here should by prefixed "np." like "np.ceil". Most of these functions are compatible with the use of numpy arrays and treat all the samples at once efficiently.
ceil(x) # Returns the ceiling of x as a float, the smallest integer value greater than or equal to x.
fabs(x) # Returns the absolute value of x.
floor(x) # Returns the floor of x as a float, the largest integer value less than or equal to x.
isinf(x) # Checks if the float x is positive or negative infinity.
isnan(x) # Checks if the float x is NaN (not a number). For more information on NaNs, see the IEEE 754 standards.
exp(x) # Returns e**x.
log(x) # Returns the natural logarithm of x to base e.
log10(x) # Returns the bas 10 logarithm of x. This is usually more accurate than log(x,10).
power(x,y) # Returns x raised to the power y.
sqrt(x) # Returns the square root of x.
arccos(x) # Returns the arc cosine of x in radians.
arcsin(x) # Returns the arc sine of x in radians.
cos(x) # Returns the coseine of x in radians.
hypot(x) # Returns the Euclidian norm, sqrt(x*x+y*y). This is the length of the vector from the origin point (x,y).
sin(x) # Returns the sine of x in radians.
tan(x) # Returns the tangent of x in radians.
degrees(x) # Converts angle x from radians to degrees.
radians(x) # Converts angles x from degrees to radians.
pi # the mathematical constant 3.14.1592..., to available precision.
e # The mathematical constant e=2.718281..., to available precision.
nan # The "not a number" constant value. To only be used for a variable initialization.
inf # The infinite value.
- inf # The -infinite value. -
Random functions - numpy package
beta(a, b[, size]) # Draw samples from a Beta distribution.
binomial(n, p[, size]) # Draw samples from a binomial distribution.
chisquare(df[, size]) # Draw samples from a chi-square distribution.
exponential([scale, size]) # Draw samples from an exponential distribution.
f(dfnum, dfden[, size]) # Draw samples from an F distribution.
gamma(shape[, scale, size]) # Draw samples from a Gamma distribution.
geometric(p[, size]) # Draw samples from the geometric distribution.
gumbel([loc, scale, size]) # Draw samples from a Gumbel distribution.
laplace([loc, scale, size]) # Draw samples from the Laplace or double exponential distribution with specified location (or mean) and scale (decay).
logistic([loc, scale, size]) # Draw samples from a logistic distribution.
lognormal([mean, sigma, size]) # Draw samples from a log-normal distribution.
logseries(p[, size]) # Draw samples from a logarithmic series distribution.
multinomial(n, pvals[, size]) # Draw samples from a multinomial distribution.
multivariate_normal(mean, cov[, size, ...]) # Draw random samples from a multivariate normal distribution.
negative_binomial(n, p[, size]) # Draw samples from a negative binomial distribution.
normal([loc, scale, size]) # Draw random samples from a normal (Gaussian) distribution.
pareto(a[, size]) # Draw samples from a Pareto II or Lomax distribution with specified shape.
poisson([lam, size]) # Draw samples from a Poisson distribution.
power(a[, size]) # Draws samples in [0, 1] from a power distribution with positive exponent a - 1.
rayleigh([scale, size]) # Draw samples from a Rayleigh distribution.
standard_cauchy([size]) # Draw samples from a standard Cauchy distribution with mode = 0.
standard_exponential([size]) # Draw samples from the standard exponential distribution.
standard_gamma(shape[, size]) # Draw samples from a standard Gamma distribution.
standard_normal([size]) # Draw samples from a standard Normal distribution (mean=0, stdev=1).
standard_t(df[, size]) # Draw samples from a standard Student's t distribution with df degrees of freedom.
triangular(left, mode, right[, size]) # Draw samples from the triangular distribution over the interval [left, right].
uniform([low, high, size]) # Draw samples from a uniform distribution.
vonmises(mu, kappa[, size]) # Draw samples from a von Mises distribution.
wald(mean, scale[, size]) # Draw samples from a Wald, or inverse Gaussian, distribution.
weibull(a[, size]) # Draw samples from a Weibull distribution.
zipf(a[, size]) # Draw samples from a Zipf distribution. -
Other packages are available in our Python distribution:
- Our Python distribution contains several packages. The very first one is numpy that provides support for numerical arrays to the Calculator and many a lot of mathematical and array-based operations. Its documentation is available online at http://numpy.org.
- Pandas, a package that adds the notion of DataFrame to Numpy (see https://pandas.pydata.org).
- matplotlib, a graphic package allowing to make graphics and display them or storing them as images (see https://matplotlib.org).
- SciPy library provides efficient routines for numerical integration, interpolation, linear algebra and statistics (see https://scipy.org/scipylib/index.html).