Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add t_cdf, t_distribution scalar functions #22507

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.trino.type.BlockTypeOperators.BlockPositionIsIdentical;
import io.trino.type.Constraint;
import org.apache.commons.math3.distribution.BetaDistribution;
import org.apache.commons.math3.distribution.TDistribution;
import org.apache.commons.math3.special.Erf;

import java.math.BigInteger;
Expand Down Expand Up @@ -702,6 +703,32 @@ public static double betaCdf(
return distribution.cumulativeProbability(value);
}

@Description("Student's t-distribution cumulative density function given the x and degrees of freedom")
@ScalarFunction
@SqlType(StandardTypes.DOUBLE)
public static double tCdf(
@SqlType(StandardTypes.DOUBLE) double x,
@SqlType(StandardTypes.DOUBLE) double degreesOfFreedom)
{
if (degreesOfFreedom < 1) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "degrees of freedom must be greater than or equal to 1");
}
return new TDistribution(degreesOfFreedom).cumulativeProbability(x);
}

@Description("Student's t-distribution given the x and degrees of freedom")
@ScalarFunction
@SqlType(StandardTypes.DOUBLE)
public static double tPdf(
@SqlType(StandardTypes.DOUBLE) double x,
@SqlType(StandardTypes.DOUBLE) double degreesOfFreedom)
{
if (degreesOfFreedom < 1) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "degrees of freedom must be greater than or equal to 1");
}
return new TDistribution(degreesOfFreedom).density(x);
}

@Description("Round to nearest integer")
@ScalarFunction("round")
@SqlType(StandardTypes.TINYINT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3547,6 +3547,33 @@ public void testInverseBetaCdf()
.hasMessage("p must be 0 >= p >= 1");
}

@Test
public void testTDistribution()
{
assertThat(assertions.function("t_cdf", "60", "1"))
.isEqualTo(0.9946953263673767);
assertThat(assertions.function("t_cdf", "10", "1"))
.isEqualTo(0.9682744825694464);
assertThat(assertions.function("t_cdf", "1", "10"))
.isEqualTo(0.8295534338489701);
assertThat(assertions.function("t_cdf", "-1.98", "2"))
.isEqualTo(0.09312625192178954);
assertThat(assertions.function("t_cdf", "0", "1"))
.isEqualTo(0.5);
assertThat(assertions.function("t_cdf", "100", "10"))
.isEqualTo(0.9999999999999999);

assertThat(assertions.function("t_pdf", "8", "3"))
.isEqualTo(7.369065209469264E-4);
assertThat(assertions.function("t_pdf", "1", "10"))
.isEqualTo(0.2303619892291386);

assertTrinoExceptionThrownBy(assertions.function("t_cdf", "60", "0")::evaluate)
.hasMessage("degrees of freedom must be greater than or equal to 1");
assertTrinoExceptionThrownBy(assertions.function("t_pdf", "60", "0")::evaluate)
.hasMessage("degrees of freedom must be greater than or equal to 1");
}

@Test
public void testBetaCdf()
{
Expand Down
11 changes: 11 additions & 0 deletions docs/src/main/sphinx/functions/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ SELECT cosine_similarity(MAP(ARRAY['a'], ARRAY[1.0]), MAP(ARRAY['a'], ARRAY[2.0]
```
:::

:::{function} t_pdf(x, df) -> double
Computes the Student's t-distribution probability density function for given x and
degrees of freedom (df). The x must be a real value and degrees of freedom must be
an integer and positive value.
:::

:::{function} wilson_interval_lower(successes, trials, z) -> double
Returns the lower bound of the Wilson score interval of a Bernoulli trial process
at a confidence specified by the z-score `z`.
Expand Down Expand Up @@ -271,3 +277,8 @@ Compute the Normal cdf with given mean and standard deviation (sd): P(N \< v; m
The mean and value v must be real values and the standard deviation must be a real
and positive value.
:::

:::{function} t_cdf(x, df) -> double
Compute the Student's t-distribution cumulative density function for given x and degrees of freedom (df).
The x must be a real value and degrees of freedom must be an integer and positive value.
:::
Loading