Skip to content

Commit 489a5e3

Browse files
committed
Update spacing, line-wrapping, and syntax highlighting
1 parent b6dad47 commit 489a5e3

2 files changed

Lines changed: 53 additions & 76 deletions

File tree

docs/src/tutorials/curve-fit.md

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,12 @@ Before using any packages, let's perform a linear fit from scratch using some li
6868

6969
The equation of a line can be written in matrix form as
7070
```math
71-
\quad
7271
\begin{pmatrix}
73-
N & \sum y_i \\
74-
\sum y_i & \sum y_{i}^2
72+
N & \sum y_i \\
73+
\sum y_i & \sum y_i^2
7574
\end{pmatrix}
76-
\begin{pmatrix}
77-
c_1 \\
78-
c_2 \\
79-
\end{pmatrix}=
75+
\begin{pmatrix} c_1 \\ c_2 \end{pmatrix}
76+
=
8077
\begin{pmatrix}
8178
\sum y_i \\
8279
\sum y_i x_i
@@ -88,27 +85,20 @@ where $c_1$ and $c_2$ are the intercept and slope.
8885
Multiplying both sides by the inverse of the first matrix gives
8986

9087
```math
91-
\quad
92-
\begin{pmatrix}
93-
c_1 \\
94-
c_2 \\
95-
\end{pmatrix}=
96-
\begin{pmatrix}
97-
N & \sum y_i \\
98-
\sum y_i & \sum y_{i}^2
88+
\begin{pmatrix} c_1 \\ c_2 \end{pmatrix}
89+
= \begin{pmatrix}
90+
N & \sum y_i \\
91+
\sum y_i & \sum y_i^2
9992
\end{pmatrix}^{-1}
100-
\begin{pmatrix}
101-
\sum y_i \\
102-
\sum y_i x_i
103-
\end{pmatrix}
93+
\begin{pmatrix} \sum y_i \\ \sum y_i x_i \end{pmatrix}
10494
```
10595

10696
We can write the right-hand side matrix and vector (let's call them `A` and `b`) in Julia notation like so:
107-
```julia
97+
```julia-repl
10898
julia> A = [
10999
length(x) sum(x)
110100
sum(x) sum(x.^2)
111-
]
101+
]
112102
2×2 Matrix{Int64}:
113103
21 1050
114104
1050 71750
@@ -181,22 +171,20 @@ Now, we'll use SciML's problem-algorithm-solve workflow to solve our optimizatio
181171
# Define the initial parameter values for slope and intercept
182172
u0 = [1.0, 1.0]
183173
# Pass through the data we want to fit
184-
data = [x,y]
174+
data = [x, y]
185175

186176
# Create an OptimizationProblem object to hold the function, initial
187177
# values, and data.
188178
using Optimization
189-
prob = OptimizationProblem(objective,u0,data)
179+
prob = OptimizationProblem(objective, u0, data)
190180

191181
# Import the optimization backend we want to use
192182
using OptimizationOptimJL
193183

194-
# Minimize the function. Optimization.jl uses the SciML common solver
195-
# interface. Pass the problem you want to solve (optimization problem
196-
# here) and a solver to use.
197-
# NelderMead() is a derivative-free method for finding a function's
198-
# local minimum.
199-
sol = solve(prob,NelderMead())
184+
# Minimize the function. Optimization.jl uses the SciML common solver interface.
185+
# Pass the problem you want to solve (optimization problem here) and a solver to use.
186+
# NelderMead() is a derivative-free method for finding a function's local minimum.
187+
sol = solve(prob, NelderMead())
200188

201189
# Exctract the best-fitting parameters
202190
slope, intercept = sol.u
@@ -231,10 +219,10 @@ end
231219

232220
```julia
233221
u0 = [1.0, 1.0, 1.0]
234-
data = [x,y]
235-
prob = OptimizationProblem(objective,u0,data)
222+
data = [x, y]
223+
prob = OptimizationProblem(objective, u0, data)
236224
using OptimizationOptimJL
237-
sol = solve(prob,NelderMead())
225+
sol = solve(prob, NelderMead())
238226
u = sol.u
239227

240228
yfit = u[1] .* x.^2 .+ u[2] .* x .+ u[3]
@@ -251,8 +239,8 @@ First, you can use automatic differentiation and a higher order optimization alg
251239
```julia
252240
using ForwardDiff
253241
optf = OptimizationFunction(objective, Optimization.AutoForwardDiff())
254-
prob = OptimizationProblem(optf,u0,data)
255-
@time sol = solve(prob,BFGS()) # another good algorithm is Newton()
242+
prob = OptimizationProblem(optf, u0, data)
243+
@time sol = solve(prob, BFGS()) # another good algorithm is Newton()
256244
```
257245
You can also write an "in-place" version of `objective` that doesn't allocate new arrays with each iteration.
258246

@@ -325,30 +313,26 @@ Quantiles
325313
```
326314

327315

328-
```
316+
```julia
329317
intercept = chain["intercept"]
330318
slope = chain["slope"]
331319
σ₂ = chain["σ₂"]
332320

333-
plot(x, x .* slope' .+ intercept';
334-
label="",
335-
color=:gray,
336-
alpha=0.05
337-
)
321+
plot(x, x .* slope' .+ intercept'; label="", color=:gray, alpha=0.05)
338322
scatter!(x, y, xlabel="x", ylabel="y", label="data", color=1)
339323
```
340324
![](../assets/tutorials/curve-fit/bayesian-lin-regression.svg)
341325

342326
Each gray curve is a sample from the posterior distribution of this model. To examine the model parameters and their covariance in greater detail, we can make a corner plot using the PairPlots.jl package. We'll need a few more samples for a nice plot, so re-run the NUTS sampler with more iterations first.
343-
```
327+
```julia
344328
Random.seed!(1234)
345329
chain = sample(model, NUTS(0.65), 25_000)
346330

347331
using PairPlots
348332
table = (;
349-
intercept= chain["intercept"],
350-
slope= chain["slope"],
351-
σ= sqrt.(chain["σ₂"])
333+
intercept = chain["intercept"],
334+
slope = chain["slope"],
335+
σ = sqrt.(chain["σ₂"])
352336
)
353337
PairPlots.corner(table)
354338
```
@@ -414,18 +398,13 @@ Quantiles
414398
u3 0.9635 1.7155 2.1211 2.5172 3.3960
415399
```
416400

417-
418-
```
401+
```julia
419402
u1 = chain["u1"]
420403
u2 = chain["u2"]
421404
u3 = chain["u3"]
422405
posterior = u1' .* x.^2 .+ u2' .* x .+ u3'
423406

424-
plot(x, posterior;
425-
label="",
426-
color=:gray,
427-
alpha=0.1
428-
)
407+
plot(x, posterior; label="", color=:gray, alpha=0.1)
429408
scatter!(x, y, xlabel="x", ylabel="y", label="data", color=1)
430409
```
431410
![](../assets/tutorials/curve-fit/bayesian-quad-regression.svg)
@@ -440,7 +419,7 @@ table = (;
440419
u_1 = chain["u1"],
441420
u_2 = chain["u2"],
442421
u_3 = chain["u3"],
443-
σ= sqrt.(chain["σ₂"])
422+
σ = sqrt.(chain["σ₂"])
444423
)
445424
PairPlots.corner(table)
446425
```

docs/src/tutorials/tabular-data.md

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ If you will be using these tools as part of a larger project, it's strongly reco
3030
If you're using [Pluto notebooks](https://plutojl.org), installing and recording package versions in a project are handled for you automatically.
3131

3232

33-
3433
## Downloading the data
3534

3635
The table in question is hosted alongside the [article](https://iopscience.iop.org/article/10.3847/1538-4365/abf93c). Go to Table 4 and click the link at the bottom to download it in FITS format. You'll need to uncompress the archive to see the `HGCA_vEDR3.fits` file.
@@ -135,7 +134,6 @@ julia> describe(df)
135134
34 │ nonlinear_dpmdec 0.000311498 -4.1194 1.92019e-7 16.0394 0 Float32
136135
35 │ chisq 566.555 3.11559e-5 3.35103 3.67633e5 0 Float32
137136
6 rows omitted
138-
139137
```
140138

141139

@@ -180,14 +178,14 @@ Let's now visualize these stars as they appear in the plane of the sky. We'll co
180178
```julia-repl
181179
julia> using Plots
182180
julia> scatter(
183-
nearby.gaia_ra,
184-
nearby.gaia_dec;
185-
marker_z = log10.(nearby.chisq),
186-
colorbartitle="log10 χ²", # typed as \chi <tab> \^2 <tab>
187-
label = "",
188-
xlabel = "right ascension (°)", # typed as \degree <tab>
189-
ylabel = "declination (°)",
190-
)
181+
nearby.gaia_ra,
182+
nearby.gaia_dec;
183+
marker_z = log10.(nearby.chisq),
184+
colorbartitle="log10 χ²", # typed as \chi <tab> \^2 <tab>
185+
label = "",
186+
xlabel = "right ascension (°)", # typed as \degree <tab>
187+
ylabel = "declination (°)",
188+
)
191189
```
192190

193191
Let's improve this plot by using a different map projection. We can make this conversion using [AstroLib.jl](https://juliaastro.org/AstroLib/stable/).
@@ -218,20 +216,20 @@ Finally, we'll make the plot and tweak some formatting options:
218216

219217
```julia-repl
220218
julia> scatter(
221-
newx,
222-
newy;
223-
marker_z = log10.(nearby.chisq),
224-
color = :turbo,
225-
colorbartitle="log10 χ²", # typed as \chi <tab> \^2 <tab>
226-
label = "",
227-
xlabel = "right ascension (°)", # typed as \degree <tab>
228-
ylabel = "declination (°)",
229-
background=:transparent,
230-
foreground=:gray,
231-
framestyle=:box,
232-
markerstrokewidth=0,
233-
grid=:none
234-
)
219+
newx,
220+
newy;
221+
marker_z = log10.(nearby.chisq),
222+
color = :turbo,
223+
colorbartitle="log10 χ²", # typed as \chi <tab> \^2 <tab>
224+
label = "",
225+
xlabel = "right ascension (°)", # typed as \degree <tab>
226+
ylabel = "declination (°)",
227+
background = :transparent,
228+
foreground = :gray,
229+
framestyle = :box,
230+
markerstrokewidth = 0,
231+
grid = :none,
232+
)
235233
```
236234
![Plot of nearby stars with significant acceleration](../assets/tutorials/tabular-data/starplot-1.svg)
237235

0 commit comments

Comments
 (0)