Julia Statistics Guide — Distributions, Hypothesis Testing, and Statistical Models
In this tutorial, you will learn about Julia Statistics Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Julia statistics uses Distributions.jl for probability distributions (Normal, Binomial, Poisson), HypothesisTests.jl for statistical tests, StatsBase.jl for descriptive stats, and GLM.jl for linear and generalized linear regression models.
Descriptive Statistics
using StatsBase
data = [1.2, 3.4, 5.6, 7.8, 9.1]
mean(data) # 5.42
median(data) # 5.6
std(data) # standard deviation
var(data) # variance
skewness(data) # skewness
kurtosis(data) # kurtosis
# Quantiles
quantile(data, [0.25, 0.5, 0.75])
Probability Distributions
using Distributions
# Normal distribution
d = Normal(0, 1) # mean=0, std=1
pdf(d, 0) # 0.3989 (density at 0)
cdf(d, 1.96) # 0.975 (cumulative)
rand(d, 100) # 100 random samples
# Binomial
d = Binomial(10, 0.5)
pdf(d, 5) # P(X=5)
rand(d, 1000) # 1000 samples
# Poisson
d = Poisson(3)
pdf(d, 2) # P(X=2)
Hypothesis Testing
using HypothesisTests
# One-sample t-test
data = [1.2, 3.4, 5.6, 7.8]
t = OneSampleTTest(data, 4.0)
println(t) # full test output
pvalue(t) # p-value
confint(t) # 95% confidence interval
# Two-sample t-test
group1 = [2.1, 3.4, 4.2]
group2 = [5.6, 7.8, 9.1]
t = TwoSampleTTest(group1, group2)
# Chi-squared test
observed = [10, 20, 30]
ChisqTest(observed)
Linear Models
using GLM
using DataFrames
# Create data
df = DataFrame(
x = rand(100),
y = 2.0 .* rand(100) .+ 1.0 .+ randn(100) * 0.1
)
# Fit linear model
model = lm(@formula(y ~ x), df)
# Results
coef(model) # coefficients
stderror(model) # standard errors
r²(model) # R-squared
predict(model) # fitted values
Common Mistakes
1. Assuming normality
Many tests assume normal distribution. Use Shapiro-Wilk test (SwTest) to check normality first.
2. Multiple comparison problem
Running many t-tests inflates Type I error. Use Bonferroni correction: BonferroniCorrection().
3. Ignoring outliers
Outliers affect mean and standard deviation. Use robust statistics (median, MAD) or remove outliers with trim or winsor.
Practice Questions
1. How do you generate random numbers from a normal distribution?
rand(Normal(μ, σ), n) generates n samples from N(μ, σ²).
2. How do you perform a t-test?
OneSampleTTest(data, μ0) tests if the mean differs from μ0.
3. How do you fit a linear regression?
lm(@formula(y ~ x), data) from GLM.jl fits y = a + b*x.
FAQ
{{< faq question="What is the difference between mean and median?" >}} Mean is sensitive to outliers. Median is robust. For symmetric distributions they're equal. {{< /faq >}}
{{< faq question="How do I handle missing data?" >}}
Use skipmissing to exclude missing values, or impute from Impute.jl for filling missing values.
{{< /faq >}}
{{< faq question="Can I do Bayesian statistics in Julia?" >}} Yes. Turing.jl provides probabilistic programming with MCMC sampling. AdvancedHMC.jl for Hamiltonian Monte Carlo. {{< /faq >}}
What's Next
Now learn about advanced visualization.
| Topic | Description | Link |
|---|---|---|
| Visualization | Advanced visualization | {{< ref "24-visualization" >}} |
| Optimization | Numerical optimization | {{< ref "25-optimization" >}} |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro