Miscellaneous functions reused across the toolbox to carry out simple numerical operations

Note: A few helper functions included in the MATLAB version of the PCIT toolbox are not present here. When it comes to numerical helper functions logsumexp.m and round_to.m, this is because a Python version of the function is already included in a required scientific programming library such as scipy or numpy. When it comes to visualization helper functions savesamesize.m and jbfill.m, it's because equivalent Python functions are either unnecessary or not applicable in the context of matplotlib's library. In either case, equivalent functionality is achieved without implementation of an additional toolbox function.

likratiotest[source]

likratiotest(l1, l0, k1, k0)

Performs the likelihood ratio test for nested models.

Arguments:

  • L1: log-likelihood for alternative model
  • L0: log-likelihood for the null model
  • K1: number of parameters for alternative model
  • K0: number of parameters for null model (K1 > K0)

Returns:

  • D: deviate score: -2*log(L1-L0)
  • p: p-value from chi-squared test with degrees of freedom = K1-K0

This function implements the $\beta_1$ likelihood-ratio test described in Section 4.10 of the P-CIT Toolbox Manual.

If we want to build confidence that this function is an exact reproduction of the MATLAB implementation, we can ask whether our version returns (-20, 1) for the arguments (l1=10.0, l0=20.0, k1=8.0, k0=5.0) as the MATLAB version does:

likratiotest(l1=10.0, l0=20.0, k1=8.0, k0=5.0)

(-20.0, 1.0)

truncated_normal[source]

truncated_normal(a, b, mu, sigma, n)

Generates N samples from a truncated normal distribution with mean=mu, sigma=sigma and with bounds A and B.

We use this function in our toolbox to add noise from a truncated Gaussian distribution.

Arguments:

  • A: lower bound
  • B: upper bound
  • mu: mean
  • sigma: sigma, standard deviation
  • N: number of samples

Returns array of length N containing mean + truncated Gaussian noise with mean=mu, sigma=sigma, between bounds A and B

As an example, we can generate and visualize the result of applying the function to some arbitrary paremeters:

import matplotlib.pyplot as plt

# generate a sample
sample = truncated_normal(a=.7, b=1.3, mu=1.0, sigma=.1, n=10000.0)

# visualize its distribution
n_bins = 100
plt.hist(sample, bins=n_bins)
plt.title('truncated_normal(a=.7, b=1.3, mu=1.0, sigma=.1, n=10000.0)')
plt.savefig('figures/truncated_normal_example.svg')
plt.show()

sample

array([0.88296168, 1.22801193, 0.8565991 , ..., 0.87280798, 1.09364587,
       0.96110738])

scale_data[source]

scale_data(data, lower=-1.0, upper=1.0)

Scale the elements of all the column vectors in Data within the range of [Lower Upper]; default range is [-1 1]

We use this function in our toolbox to scale the predictor variable between 0 and 1.

Arguments:

  • Data: data, numeric vector
  • Lower: lower range, numeric
  • Upper: upper range, numeric

Returns:

  • scaled: 1xN array containing scaled data

As an example we can execute the function with a few arbitrary parameters:

scale_data([8.3256, 1000.0, 23.0, 564.0], 0, 1)

As specified, it returns the vector scaled between 0 and 1.

array([[0. , 1. , 0.0147976 , 0.56033956]])

Notably, the output is 2-dimensional rather than a vector; this enforces consistency in the shape of the function's output regardless of the shape of the input. It also helps ensure compatibility with other code translated from the MATLAB implementation of the toolbox.