INTRODUCING StatBook v1!
Who needs R or MATLAB when you have ROBLOX
Featuring Hypothesis Testing such as Multiple Linear Regression and ANOVA, Kernel Density Estimation, Markov Chains, Gamma/Beta Distribution Generation, and more with just ONE LINE OF CODE!
Welcome to the ultimate guide for StatBook v1! Whether you’re looking for ways to generate random variables in a more diverse and/or dynamic way than math.random
does, looking to predict the next move of a player, consistently update analytics for in-game product sales, or simply curious about random variable generations and hypothesis testing, you’re in the right place!
Download [FREE]
Documentation *highly recommended!!!
GitHub
CDF Calculation link for Random Variable Generation *translate the page to english
Graph Functions for Custom Distribution Creation
Get Started
Instructions
How to Use StatBook_v1 Module
Follow these steps to integrate the StatBook_v1 module into your project:
Step 1: Download the Module
Download the StatBook_v1
module from this link.
Step 2: Place the Module
Place the downloaded StatBook_v1
module into ServerScriptService
within your Roblox Studio project.
Step 3: Import the Module in Your Script
When using the module in your script, add the following line to import it:
local StatBook = require(game.ServerScriptService.StatBook_v1)
Step 4: Use Functions from the Module
When you need to use a function from this library, always prepend the function call with “StatBook.”. For example:
local StatBook = require(game.ServerScriptService.StatBook_v1)
local result = StatBook.Median(list)
And you are all set!
By following these steps, you’ll be able to use all the statistical functions provided by the StatBook_v1 module in your Roblox game.
Summary of Capabilities
Hypothesis Testing
Summary
-
Inference on Numerical Samples: The
StatBook.inference(...)
function has the ability to run statistical inferences on samples with dependent and independent numerical data. These tests include one and two sample independent/dependent t-tests, sign test, rank-sum test, signed-rank test, ANOVA, and Friedman Test. Returns information about differences in distribution using statistics and p-values, as well as post-hoc tests for individual sample to sample associations in the 3+ sample comparison cases.
NOTE: All tests are two-tailed (except F and Chi-Square Tests). A one-tailed test option will come with StatBook V1.1.
-
Multiple Linear Regression with Mallow’s C(p) Forward Selection: The
StatBook.multipleLinearRegression(...)
function takes in a list of data points, with each datapoint containing betas/predictors, and the respective dependent variables associated with each data point. The function returns all sorts of information about the regression model, and also has the option to refine a hypothesized model via forward selection using Mallow’s C(p) as selection criteria. This function can be used in complement withStatBook.predictY(...)
to subsequently get the predicted y values of a datapoint given all predictors. -
Categorical Hypothesis Testing: Includes a specialized suite of tests like
oddsRatio
,oneSampleProportionCI
, andtwoProportionInference
for analyzing categorical data. It also includes a set of Chi-Square Tests such asgoodnessOfFit()
andchiSquareIndependence()
for comparing observed data with expected data in categories.
Random Variate Generation
Summary
-
Generate random variables from 15 different distributions: This feature allows users to generate random variables based on various statistical distributions. It includes commonly-used distributions like Normal, Poisson, Binomial, and also more specialized distributions like Chi-square, Weibull, and Log-normal. Users can input the parameters specific to each distribution to return a random variable.
-
Scale and Trim Distributions to get the values you need: A variety of 8 different distributions with greater flexibility over the bounds of the distribution you want to use and the range you want to map your output values to!
-
Create any Customized Distribution: This feature gives users the freedom to create their own probability distributions. The developer inputs a mathematical function (or a set of piecewise functions) in string using Lua syntax alongside its x range of use, which is for instance:
local piecewiseFunctions = {{"func1", func1xmin, func1xmax}, {"func2", func2xmin, func2xmax}}.
-
Then, the aggregate Probability Density function can be scaled between any range you want, so that random variates are pulled from that range and emulate the distribution specified in
piecewiseFunctions
. MORE SPECIFIC INFORMATION BELOW. -
Randomly sample a continuous variable from a discrete dataset: You can use a dataset of discrete values to generate random variables from a continuous representation of itself. The algorithm applies Kernel Density Estimation (KDE) to make each discrete datapoint contain a distribution (kernel) of itself, which the sum of these kernels creates a Probability Density Function, from which the algorithm randomly samples a value within the range of discrete data points, thus generating a “continuous” variable from a discrete set.
-
Simulate a sequence of actions through a Markov Chain: Markov Chain simulations are useful for modeling sequences of events where the outcome of one event affects the outcomes of subsequent events. Users can input the initial state and state transition probabilities to simulate various scenarios.
Basic Statistics
- Descriptive Statistics made simple: Provides fundamental statistical functions like mean, median, mode, and range, as well as more advanced metrics like standard deviation and variance.
Complex Functions
-
Find approximations to functions difficult to solve analytically: Advanced mathematical functions like
erf
,inverf
,gamma
, andhypergeometric2f1
are included for specialized statistical needs.
Matrix Operations
- Perform matrix operations: Includes matrix addition, subtraction, multiplication, transposition, and inversion.
Hypothesis Testing
How can you use these functions?
How can game developers use these functions?
-
Real-Time Analysis and Reaction: The model could even be used in real-time to adjust game parameters according to ongoing player behavior. Furthermore, you could predict and react to a players actions in-game in real-time.
-
Optimized Monetization/Play Time: If your game has in-app purchases, understanding what players value can help you offer more compelling packages.
-
Feature Selection: The function includes forward regression based on Mallows’s Cp, helping you identify which variables are the most important predictors, enabling you to focus on the most impactful game elements.
-
A/B Testing: If you have two versions of a feature, you can use the inference tests to see which version is more effective at achieving a specific outcome.
-
Player Personalization: Use the model to predict player behaviors and preferences, allowing for a more personalized gaming experience.
Functions
Inference on Numerical Samples (ANOVA, one/two sample independent/dependent t-tests, Wilcoxon rank tests, etc.)
Be sure to check this function out. It’s an important one!
inference(samples, independent, CL, mu0)
inference(samples, independent, CL, mu0)
Overview
Performs statistical inference tests based on the given data. The function will decide which test to use based on the number of samples, whether they are independent or not, and their distribution.
NOTE 1: ALL TESTS IN THIS MODULE ARE TWO-TAILED (besides F and Chi-Square tests). A future update with one-tailed options may come in a future update.
NOTE 2: There aren’t any two or more sample tests able to do a hypothesis test for a certain amount of difference between the means/medians of the samples. By default, all two or more sample tests check for a difference in distribution, that is, ( H_0: D_\mu) or ( D_\eta = 0 )
NOTE 3: If
dependent = true
then all samples must have the same amount of entries.
NOTE 4: It is highly recommended to return the warning value, as a
warning = true
value means the results may have a significant degree of inaccuracy due to computational limits.
Parameters
Parameter | Type | Description |
---|---|---|
samples |
Table | A table of samples containing the data to be tested. |
independent |
Boolean, Nil | Whether the samples are independent or not (Nil for 1-sample). |
CL |
Number, (Nil = 0.95) | Confidence level for the statistical tests (Nil = 0.95). |
mu0 |
Number, (Nil = 0) | The hypothetical mean tested against in 1-sample test. Defaults to 0 if Nil. |
Returns
A table possibly containing the following:
Key | Type | Description |
---|---|---|
pValue |
Number | The p-value of the test. |
rejectH0 |
Boolean | Whether to reject the null hypothesis. |
stat |
Number | The value of the test statistic. |
df |
Number, Table, Nil | Degrees of freedom (some tests have two (F), some not applicable). |
center |
Table → Number(s) | Contains the mean(s) or median(s) of the dataset(s). |
centerComp |
Number, Nil | Comparison value for the center (not applicable for some tests). |
lowerCI |
Number, Nil | The lower bound of the confidence interval for mean/median (NA for 3+ sample tests). |
upperCI |
Number, Nil | The upper bound of the confidence interval for the mean/median (NA for 3+ sample tests) |
dependent |
Boolean, Nil | Whether the test is for dependent samples (Nil for one-sample tests). |
parametric |
Boolean | Indicates if the test is parametric. |
nSamples |
Number | Number of samples in the test. |
testType |
String | Specifies the type of the test. |
statType |
String | Specifies the type of the test statistic. |
centerType |
String | Specifies what measure of central tendency is being tested. |
postHoc |
Table → Tables, Nil | Post-hoc tests with individual test data within each nested table (only for 3+ sample tests). |
postHocSig |
Table → Tables, Nil | Only contains Post-hoc tests with significant p-values (only for 3+ sample tests) |
warning |
Nil , True | Warnings if applicable (Nil if false or NA). |
postHoc
and postHocSig
subfields (only for 3+ sample tests)
A table possibly containing the following:
Key | Type | Description |
---|---|---|
group1 |
Number | The index of the first sample selected in the Post Hoc. |
group2 |
Number | The index of the second sample selected in the Post Hoc. |
pValue |
Number | The p-value of the Post Hoc test. |
alpha |
Number | The alpha needed for significance entailed by the Bonferonni correction. |
rejectH0 |
Boolean | Whether to reject the null hypothesis. |
stat |
Number | The value of the test statistic. |
df |
Number, Nil | Degrees of freedom (some not applicable). |
center |
Table → Numbers | Contains the means or medians of the datasets. |
centerComp |
Number | Comparison value for the center. |
lowerCI |
Number | The lower bound of the confidence interval for mean/median. |
upperCI |
Number | The upper bound of the confidence interval for the mean/median |
testType |
String | Specifies the type of the test. |
statType |
String | Specifies the type of the test statistic. |
centerType |
String | Specifies what measure of central tendency is being tested. |
warning |
Nil , True | Warnings if applicable (Nil if false or NA). |
Examples
Examples
One-Sample Test:
local samples = {
{12, 15, 14, 10, 13, 8, 13, 16, 8, 15, 22, 4, 7, 8}
}
-- in this case, either one-sample t-test or sign test (depends on normality of sample)
local CL = 0.95
local mu0 = 12
-- if we did not specify mu0, it would default to 0,
local result = StatBook.inference(samples, nil, CL, mu0)
print(result.pValue, result.stat)
Two-Sample Test:
local samples = {
{12, 15, 14, 10, 13, 6, 18},
{20, 24, 30, 27, 28, 19, 19}
}
local independent = false
-- in this case, either two-sample dep. t-test or signed-rank test (depends on normality of samples + Folded-F test)
local CL = 0.95
local result = StatBook.inference(samples, independent, CL)
print(result.pValue, result.centerComp, result.lowerCI, result.upperCI)
Three-Plus Sample Test:
local samples = {
{12, 15, 14, 10, 13, 14},
{20, 24, 30, 27, 28, 20},
{16, 25, 19, 20, 22, 18},
{23, 14, 10, 37, 8, 19}
}
local independent = true
-- in this case, either ANOVA test or Kruskal Wallis test (depends on normality of samples + Levene Test)
local CL = 0.95
local result = StatBook.inference(samples, independent, CL)
print(result.pValue, result.postHocSig.group1, result.postHocSig.group2, result.postHocSig.pValue)
Multiple Linear Regression w/ Mallow’s C(p) forward selection
Be sure to check this function out. It’s an important one! predictY() is also located inside this dropdown, as the two functions are very closely related
multipleLinearRegression(X, Y, forwardReg, diagnostics, CL) AND predictY(X, model, yHat, indices)
multipleLinearRegression(X, Y, forwardReg, diagnostics, CL)
Overview
The multipleLinearRegression
function performs multiple linear regression analysis on given data sets. This advanced function includes several features, such as forward regression based on Mallows’s Cp, diagnostic statistics, and calculation of Variance Inflation Factors (VIFs).
NOTE 1: All data point subtables within the ( X ) table must have the same amount of entries. No missing or nil values allowed.
NOTE 2: Before invoking the function, ensure that the ( X ) table is formatted correctly as a 2D table, where each inner table represents a row of the matrix. If ( X ) is not in this format, you may use the
module.matTranspose(matrix)
function to transpose ( X ) into a compatible layout.
NOTE 3: The
VIFs
table is only there as a warning. VIF values do not impact the regression model and do not automatically remove multi-collinear predictors. You will have to manually account for this if you choose to remove a predictor yourself and thus rerunmultipleLinearRegression
again.
Parameters
Parameter | Type | Description | Default |
---|---|---|---|
X |
table | The independent variables matrix (2D table). | Required |
Y |
table | The dependent variable vector (1D table). | Required |
forwardReg |
boolean | Enables or disables the forward regression process. | true |
diagnostics |
boolean | Enables or disables diagnostic statistics. | true |
CL |
number | Confidence level for t-tests and F-test. | 0.95 |
Returns (if diagnostics
~= true
)
Variable | Type | Description |
---|---|---|
yHat |
table → number(s) | Fitted values for the dependent variable. |
indices |
table → number(s) | Indices of betas retained in model from lmOrig to lmNew |
Returns (if diagnostics
= true
)
Variable | Type | Description | Subfields |
---|---|---|---|
lmNew |
table → tables | Model after forward selection with Mallow’s C(p) | yes |
lmOrig |
table → tables | Model before forward selection with Mallow’s C(p) | yes |
indices |
table → number(s) | Indices of betas retained in model from lmOrig to lmNew |
lmNew
and lmOrig
Subfields*
Variable | Type | Description | Sub-subfields |
---|---|---|---|
yHat |
table → number(s) | Fitted values for the dependent variable. | |
r2 |
number | ( R^2 ) value indicating the goodness of fit. | |
r2adj |
number | Adjusted ( R^2 ) accounting for # of predictors. | |
F |
number | F-statistic used for hypothesis testing. | |
pValueF |
number | p-value of the F-statistic. | |
BetaInfo |
table → table | Information about predictor coefficients. | yes |
VIFs * |
table → table | Indicates multicollinearity status. | yes |
* There isn’t a VIFs
subfield in lmOrig.
BetaInfo
Sub-subfields
Variable | Type | Description |
---|---|---|
predictorIndex |
table → number | The original index of the beta in question. |
rejectH0 |
table → boolean | Hypotheses test results for individual betas. |
t |
table → number | The t-statistic of the beta in question. |
pValue |
table → boolean | The p-value of the beta in question. |
VIFs
Sub-subfields
Variable | Type | Description |
---|---|---|
VIF |
table → number | Variance Inflation Factors of each beta. |
summaryVIF |
table → string | A description of potential multicollinearity |
Example Usage
-- regression with 6 datapoints and 3 predictors
local X = {{1, 4, 7}, {2, 3, 5}, {3, 2, 1}, {4, 2, 2}, {5, 8, 3}, {3, 6, 2}}
local Y = {3, 3, 2, 2, 4, 5}
local model = StatBook.multipleLinearRegression(X, Y)
print(model.lmNew.pValueF, model.lmOrig.pValueF, model.lmNew.BetaInfo.t, model.lmNew.BetaInfo.pValue) -- can return a lot more than that
-- rest is optional
local Xtest = {1, 5, 6}
local prediction = predictY(Xtest, model)
Subsequent Usage
After acquiring the model from module.multipleLinearRegression
, you can employ the module.predictY(X, model, yHat, indices)
function directly with the returned model
to predict new ( Y ) values based on new ( X ) values. The model
object contains all necessary coefficients and information for the prediction.
predictY(X, model, yHat, indices)
Overview
The predictY
function predicts the dependent variable ( Y ) based on the independent variable ( X ) and the given model. Optionally, it allows for specific fitted values ( \hat{y} ) and predictor indices to be specified.
Parameters
Parameter | Type | Description | Default |
---|---|---|---|
X |
Table | The input vector containing independent variable values. | - |
model |
Table | The regression model from multipleLinearRegression()
|
- |
yHat |
Table | Optional. The fitted values for the intercept and coefficients. | nil |
indices |
Table | Optional. The indices in the model to be used for prediction. | nil |
Returns
Return | Type | Description |
---|---|---|
YPred |
Number | The predicted value of the dependent variable ( Y ). |
Example
local X = {{1, 4, 7}, {2, 3, 5}, {3, 2, 1}, {4, 2, 2}, {5, 8, 3}, {3, 6, 2}}
local Y = {3, 3, 2, 2, 4, 5}
local model = StatBook.multipleLinearRegression(X, Y)
local Xtest = {1, 5, 6}
local YPred = module.predictY(Xtest, model)
print(YPred)
Random Variate Generation - Go Beyond math.random
!
Why should you use this?
Ever found yourself doing something similar to this?
Tedious Coding
function skewedRandom()
local randomNumber = math.random()
if randomNumber < 0.2 then
return 4
elseif randomNumber < 0.3 then
return 3
elseif randomNumber < 0.4 then
return 5
elseif randomNumber < 0.5 then
return 2
elseif randomNumber < 0.6 then
return 6
elseif randomNumber < 0.7 then
return 1
elseif randomNumber < 0.8 then
return 7
elseif randomNumber < 0.9 then
return 8
elseif randomNumber < 0.95 then
return 9
else
return 10
end
end
The standard library in Roblox’s Lua provides a basic random number generator through math.random()
. While this function is useful for generating uniformly distributed random numbers, it is limited when it comes to generating numbers from other statistical distributions like Normal, Exponential, Gamma, or as a matter of fact, any type of other possible distribution.
With StatBook, there are easier ways with infinitely many possible distributions.
Functions
Scalable Random Generation with Distributions
generateStandardNormalScaled(...)
generateStandardNormalScaled(desiredMin, desiredMax, LQpercent, UQpercent, lowerQuantile, upperQuantile)
Overview
The generateStandardNormalScaled()
function generates a scaled random number based on the standard normal distribution within the specified range.
Parameters
Parameter | Type | Description |
---|---|---|
desiredMin |
Number | The minimum desired value of the scaled random number. |
desiredMax |
Number | The maximum desired value of the scaled random number. |
LQpercent |
Number | Lower quantile percentage. Default is 0.001. |
UQpercent |
Number | Upper quantile percentage. Default is 0.999. |
lowerQuantile |
Number | Lower quantile value. Calculated by default if not provided. |
upperQuantile |
Number | Upper quantile value. Calculated by default if not provided. |
Returns
Return | Type | Description |
---|---|---|
random |
Number | A scaled random number in the range [desiredMin, desiredMax] . |
Example
local random = StatBook.generateStandardNormalScaled(-10, 10)
print(random) -- Value between -10 and 10
generateNormalScaled(...)
generateNormalScaled(mu, sigma, desiredMin, desiredMax, LQpercent, UQpercent, lowerQuantile, upperQuantile)
Overview
The generateNormalScaled()
function generates a scaled random number based on a normal distribution with a specified mean (( \mu )) and standard deviation (( \sigma )) within the desired range.
Parameters
Parameter | Type | Description |
---|---|---|
mu |
Number | The mean of the normal distribution. |
sigma |
Number | The standard deviation of the normal distribution. |
desiredMin |
Number | The minimum desired value of the scaled random number. |
desiredMax |
Number | The maximum desired value of the scaled random number. |
LQpercent |
Number | Lower quantile percentage. Default is 0.001. |
UQpercent |
Number | Upper quantile percentage. Default is 0.999. |
lowerQuantile |
Number | Lower quantile value. Calculated by default if not provided. |
upperQuantile |
Number | Upper quantile value. Calculated by default if not provided. |
Returns
Return | Type | Description |
---|---|---|
random |
Number | A scaled random number in the range [desiredMin, desiredMax] . |
Example
local random = StatBook.generateNormalScaled(0, 1, -10, 10)
print(random) -- Output will vary
generateLogNormalScaled(...)
generateLogNormalScaled(mu, sigma, desiredMin, desiredMax, LQpercent, UQpercent, lowerQuantile, upperQuantile)
Overview
The generateLogNormalScaled()
function generates a scaled random number based on a log-normal distribution with a specified mean (( \mu )) and standard deviation (( \sigma )) within the desired range.
Parameters
Parameter | Type | Description |
---|---|---|
mu |
Number | The mean of the log-normal distribution. |
sigma |
Number | The standard deviation of the log-normal distribution. |
desiredMin |
Number | The minimum desired value of the scaled random number. |
desiredMax |
Number | The maximum desired value of the scaled random number. |
LQpercent |
Number | Lower quantile percentage. Default is 0. |
UQpercent |
Number | Upper quantile percentage. Default is 0.999. |
lowerQuantile |
Number | Lower quantile value. Calculated by default if not provided. |
upperQuantile |
Number | Upper quantile value. Calculated by default if not provided. |
Returns
Return | Type | Description |
---|---|---|
random |
Number | A scaled random number in the range [desiredMin, desiredMax] . |
Example
local random = StatBook.generateLogNormalScaled(0, 1, 1, 100)
print(random) -- Output will vary
generateCauchyScaled(...)
generateCauchyScaled(x0, gamma, desiredMin, desiredMax, LQpercent, UQpercent, lowerQuantile, upperQuantile)
Overview
The function generates a scaled random number based on a Cauchy distribution with a specified location parameter (( x_0 )) and scale parameter (( \gamma )) within the desired range.
Parameters
Parameter | Type | Description |
---|---|---|
x0 |
Number | The location parameter of the Cauchy distribution. |
gamma |
Number | The scale parameter of the Cauchy distribution. |
desiredMin |
Number | The minimum desired value of the scaled random number. |
desiredMax |
Number | The maximum desired value of the scaled random number. |
LQpercent |
Number | Lower quantile percentage. Default is 0.001. |
UQpercent |
Number | Upper quantile percentage. Default is 0.999. |
lowerQuantile |
Number | Lower quantile value. Calculated by default if not provided. |
upperQuantile |
Number | Upper quantile value. Calculated by default if not provided. |
Returns
Return | Type | Description |
---|---|---|
random |
Number | A scaled random number in the range [desiredMin, desiredMax] . |
Example
local random = StatBook.generateCauchyScaled(0, 1, -10, 10)
print(random) -- Output will vary
generateExponentialScaled(...)
generateExponentialScaled(lambda, desiredMin, desiredMax, LQpercent, UQpercent, lowerQuantile, upperQuantile)
Overview
The function generates a scaled random number based on an Exponential distribution with a specified rate parameter (( \lambda )) within the desired range.
Parameters
Parameter | Type | Description |
---|---|---|
lambda |
Number | The rate parameter of the Exponential distribution. |
desiredMin |
Number | The minimum desired value of the scaled random number. |
desiredMax |
Number | The maximum desired value of the scaled random number. |
LQpercent |
Number | Lower quantile percentage. Default is 0. |
UQpercent |
Number | Upper quantile percentage. Default is 0.999. |
lowerQuantile |
Number | Lower quantile value. Calculated by default if not provided. |
upperQuantile |
Number | Upper quantile value. Calculated by default if not provided. |
Returns
Return | Type | Description |
---|---|---|
random |
Number | A scaled random number in the range [desiredMin, desiredMax] . |
Example
local random = StatBook.generateExponentialScaled(1, 0, 10)
print(random) -- Output will vary
generateGammaScaled(...)
generateGammaScaled(alpha, desiredMin, desiredMax, LQpercent, UQpercent, lowerQuantile, upperQuantile)
Overview
The function generates a scaled random number based on a Gamma distribution with a specified shape parameter (\( \alpha \)) within the desired range.
Parameters
Parameter | Type | Description |
---|---|---|
alpha |
Number | The shape parameter of the Gamma distribution. |
desiredMin |
Number | The minimum desired value of the scaled random number. |
desiredMax |
Number | The maximum desired value of the scaled random number. |
LQpercent |
Number | Lower quantile percentage. Default is 0. |
UQpercent |
Number | Upper quantile percentage. Default is 0.999. |
lowerQuantile |
Number | Lower quantile value. Calculated by default if not provided. |
upperQuantile |
Number | Upper quantile value. Calculated by default if not provided. |
Returns
Return | Type | Description |
---|---|---|
random |
Number | A scaled random number in the range [desiredMin, desiredMax] . |
Example
local random = StatBook.generateGammaScaled(2, 0, 10)
print(random) -- Output will vary
generateBetaScaled()
generateBetaScaled(alpha, beta, desiredMin, desiredMax, LQpercent, UQpercent, lowerQuantile, upperQuantile)
Overview
The function generates a scaled random number based on a Beta distribution with specified shape parameters (( \alpha ) and ( \beta )) within the desired range.
Parameters
Parameter | Type | Description |
---|---|---|
alpha |
Number | The first shape parameter of the Beta distribution. |
beta |
Number | The second shape parameter of the Beta distribution. |
desiredMin |
Number | The minimum desired value of the scaled random number. |
desiredMax |
Number | The maximum desired value of the scaled random number. |
LQpercent |
Number | Lower quantile percentage. Default is 0. |
UQpercent |
Number | Upper quantile percentage. Default is 1. |
lowerQuantile |
Number | Lower quantile value. Calculated by default if not provided. |
upperQuantile |
Number | Upper quantile value. Calculated by default if not provided. |
Returns
Return | Type | Description |
---|---|---|
scaledX |
Number | A scaled random number in the range [desiredMin, desiredMax] . |
Example
local scaledX = StatBook.generateBetaScaled(2, 5, 0, 1)
print(scaledX) -- Output will vary
Make a Customized Distribution out of Function(s)
customizedDistribution(piecewiseFunctions, desiredMin, desiredMax)
customizedDistribution(piecewiseFunctions, desiredMin, desiredMax)
Overview
The function generates a random number based on custom piecewise functions within the desired range.
Parameters
Parameter | Type | Description |
---|---|---|
piecewiseFunctions |
Table | A table containing subtables, each with a function string, ( x_{\text{min}} ), and ( x_{\text{max}} ) for each piecewise function. |
desiredMin |
Number | The minimum desired value of the scaled random number. |
desiredMax |
Number | The maximum desired value of the scaled random number. |
Returns
Return | Type | Description |
---|---|---|
randomX |
Number | A scaled random number in the range [desiredMin, desiredMax] . |
Example
local functions = {{"x^2", 0, 2}, {"2*x", 2, 4}}
local randomX = StatBook.customizedDistribution(functions, 0, 10)
print(randomX) -- Output will vary
Special Instructions for using Customized Distribution
To use the Customized Distribution feature, you need to provide input in the form of piecewise functions along with the desired minimum and maximum value range you want. Each piecewise function consists of three parts:
- A function string (
userFunctionString
), which is a string representation of the mathematical function you want to evaluate. Your function string can utilize any of the standard Lua math library functions, as they are available in the environment where the function is evaluated. - Minimum (
xMin
) and maximum (xMax
) x-values for the domain of the piecewise function.
Use Desmos to help create any distribution you want.
Example
local piecewiseFunctions = {
{"math.sqrt(-(5 * x + 1) + 1) * (x + 1)", -1, 0},
{"math.sqrt(-(-5 * x + 1) + 1) * (-x + 1)", 0, 1}
}
local desiredMin = -100
local desiredMax = 100
local result = StatBook.customizedDistribution(piecewiseFunctions, desiredMin, desiredMax)
Graph on Desmos
Randomly Generate Continuous Variable From Discrete Dataset w/ KDE
randomFromDataset(values, kernel, percentageOfFrameTime, bandwidth)
randomFromDataset(values, kernel, percentageOfFrameTime, bandwidth)
Overview
Generates a random number based on a given dataset using Kernel Density Estimation (KDE).
Parameters
Parameter | Type | Description |
---|---|---|
values |
Table | The dataset from which to generate the random number. |
kernel * |
String | The type of kernel to use for the KDE. Default is Gaussian. |
percentageOfFrameTime |
Number | The percentage of frame time allowed for the function to run. Default is 0.1. |
bandwidth |
Number | The bandwidth to use in the KDE. Calculated by default if not provided. |
*Options for
kernel
are: “Gaussian”, “Epanechnikov”, “Uniform”, “Triangular”, “Biweight”, “Cosine”, “Logistic”, and “Sigmoid”.
Returns
Return | Type | Description |
---|---|---|
xRandom |
Number | A random number generated based on the KDE of the dataset. |
Example
local dataset = {1, 2, 3, 3, 4, 4, 5, 6, 7}
local kernelType = "Gaussian"
local percentageOfFrameTime = 0.1
local randomValue = StatBook.randomFromDataset(dataset, kernelType, percentageOfFrameTime)
print(randomValue) -- Output will vary
Simulate a sequence of actions through a Markov Chain
markovChain(states, transitionProbs, startState, length, returnFullSequence)
markovChain(states, transitionProbs, startState, length, returnFullSequence)
Overview
Generates a sequence of states based on a Markov Chain model.
Parameters
Parameter | Type | Description |
---|---|---|
states |
Table | List of possible states in the Markov Chain. |
transitionProbs |
Table | Transition probability matrix between states. |
startState |
Any | The state to start the sequence from. |
length |
Number | The length of the sequence to be generated. |
returnFullSequence |
Boolean | Whether to return the full sequence or just the final state. Default is false . |
Returns
Return | Type | Description |
---|---|---|
sequence or sequence[length]
|
Table or Any | Returns the entire sequence if returnFullSequence is true; otherwise, returns the last state. |
Example
local states = {"Sunny", "Cloudy", "Rainy"}
local transitionProbs = {
Sunny = {Sunny = 0.8, Cloudy = 0.15, Rainy = 0.05},
Cloudy = {Sunny = 0.2, Cloudy = 0.6, Rainy = 0.2},
Rainy = {Sunny = 0.1, Cloudy = 0.3, Rainy = 0.6}
}
local startState = "Sunny"
local length = 10
local sequence = StatBook.markovChain(states, transitionProbs, startState, length, true)
print(sequence) -- Output will be a table representing the sequence
Do you see yourself using this module? Be honest : )
- Yes
- Maybe
- Probably Not
- No
0 voters