import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = np.linspace(-5, 5, 101)
y = norm.pdf(x)
ax.plot(x, y)
ax.set_title("normal distribution")
ax.set_xlabel("x")
ax.set_ylabel("y")
plt.show()2025-08-27
Gagolewski (2024)
Let’s get one thing straight: R is not just a statistical package. It is a general-purpose, high-level programming language that happens to be very powerful for numerical, data-intense computing activities of any kind. It offers extensive support for statistical, machine learning, data analysis, data wrangling, and data visualisation applications, but there is much more.
With its broad statistical support, R is a natural choice for oceanographers in the biological, chemical and geological sub-disciplines. However, some physical oceanographers have remained attached to Matlab, … Lately, this has been changing, as oceanographers turn to open-source systems such as Python and R. A particular strength of R is its provision of many powerful and well-vetted packages for handling specialized calculations. The oce package is a prime example.
array()や行列matrix()は列優先で数式と対応。func()l96 <- function(x, F) {
n <- length(x)
(x[c(2:n, 1)] - x[c(n-1, n, 1:(n-2))]) * x[c(n, 1:(n-1))] - x + F
}
rk4 <- function(f, x, dt, opts) {
k1 <- f(x, opts)
k2 <- f(x + 0.5 * dt * k1, opts)
k3 <- f(x + 0.5 * dt * k2, opts)
k4 <- f(x + dt * k3, opts)
x + (k1 + 2 * k2 + 2 * k3 + k4) * dt / 6
}
nj <- 40
nstep <- 1001
x.hist <- matrix(0, nj, nstep)
x <- rnorm(nj)
F <- 8
dt <- 0.05
for (i in 1:nstep-1) {
x <- rk4(l96, x, dt, F)
x.hist[,i] <- x
}
t <- seq(0, nstep*dt, length.out=nstep)
filled.contour(1:nj, t, x.hist, nlevel=11, main=paste("Lorenz 96 F=", F),
ylim=rev(range(t)), xlab="j", ylab="time")torch for R 自動微分を使って
L-BFGSで数値最適化
library(torch)
rosenbrock <- function(x, y, a = 1, b = 100) {
(a - x)^2 + b * (y - x^2)^2
}
x <- torch_tensor(c(-1, -1), requires_grad = TRUE)
optimizer <- optim_lbfgs(x, line_search_fn = "strong_wolfe")
calc_loss <- function() {
optimizer$zero_grad()
value <- rosenbrock(x[1], x[2])
cat("value is:", as.numeric(value), "\n")
value$backward()
value
}
num_iterations <- 2
tol <- 1e-10
xhist <- as.numeric(x)
for (i in 1:num_iterations) {
cat("\n", "iteration:", i, "\n")
optimizer$step(calc_loss)
cat("x=", as.numeric(x), "\n")
xhist <- rbind(xhist, as.numeric(x))
}
iteration: 1
value is: 404
value is: 62.32629
value is: 30.0694
value is: 2.630802
value is: 1.178554
value is: 1.15742
value is: 1.132393
value is: 1.00142
value is: 1.091282
value is: 0.6181912
value is: 0.8905501
value is: 0.6098283
value is: 0.5655912
value is: 0.3922533
value is: 0.2774411
value is: 1.417702
value is: 0.2190705
value is: 0.1747326
value is: 0.1380794
value is: 0.08087045
value is: 0.05257415
value is: 0.1490689
value is: 0.0400695
value is: 0.02954894
value is: 0.01238139
x= 0.9039377 0.8114879
iteration: 2
value is: 0.01238139
value is: 0.006821597
value is: 0.002873288
value is: 0.001204705
value is: 0.0006028978
value is: 4.76946e-05
value is: 2.687239e-06
value is: 5.929914e-08
value is: 1.555293e-09
value is: 3.588241e-13
x= 0.9999999 0.9999998
Li et al. (2021)
RMarkdownの進化形
データ同化夏の学校2025