julia on Boyoon's blog
https://boyoon-c.github.io/tags/julia/
Recent content in julia on Boyoon's blogBoyoon's bloghttps://boyoon-c.github.io/%3Clink%20or%20path%20of%20image%20for%20opengraph,%20twitter-cards%3E
https://boyoon-c.github.io/%3Clink%20or%20path%20of%20image%20for%20opengraph,%20twitter-cards%3E
Hugo -- gohugo.ioSat, 22 Apr 2023 00:00:00 +0000Julia Essentials
https://boyoon-c.github.io/posts/21/
Sat, 22 Apr 2023 00:00:00 +0000https://boyoon-c.github.io/posts/21/; We add ; to suppress output returned. For example let’s compare two codes with and without ; at the end. x=2 would return 2 in the console as an output while x=2; will not return anything in the console.
enumerate() enumerate() returns both the index and the value of an element in an array.
collect(enumerate(countries)) Output:
(1, "Korea") (2, "Japan") (3, "China") countries = ("Korea", "Japan", "China") for (index, country) in enumerate(countries) println("Country $(country) in index $(index)") end Output:Matrix Decomposition
https://boyoon-c.github.io/posts/20/
Fri, 21 Apr 2023 00:00:00 +0000https://boyoon-c.github.io/posts/20/Matrix decomposition example Let N be the number of possible simulated paths, T be the number of time periods, and P be the number of parameters to estimate. For now, let’s suppose that N = 4, T=2 and P=2. Below is an example with T-by-N matrix. The rows indicates different time periods, and the columns indicate different simulated paths \(n \in \{1,2,3,4\} \). For example,
$$ A = \begin{bmatrix} a_{11}\theta + b_{11}\rho & a_{12}\theta + b_{12}\rho & a_{13}\theta + b_{13}\rho & a_{14}\theta + b_{14}\rho \newline \beta(a_{21}\theta + b_{21}\rho) & \beta(a_{22}\theta + b_{22}\rho) & \beta(a_{23}\theta + b_{23}\rho) & \beta(a_{24}\theta + b_{24}\rho) \end{bmatrix} \newline $$ When n =1, $$ \begin{bmatrix} 1 & \beta \end{bmatrix} \begin{bmatrix} a_{11} & b_{11} \newline a_{21} & b_{21} \end{bmatrix} \begin{bmatrix} \theta \newline \rho \end{bmatrix} $$ When n =2, $$ \begin{bmatrix} 1 & \beta \end{bmatrix} \begin{bmatrix} a_{12} & b_{12} \newline a_{22} & b_{22} \end{bmatrix} \begin{bmatrix} \theta \newline \rho \end{bmatrix} $$ When n =3, $$ \begin{bmatrix} 1 & \beta \end{bmatrix} \begin{bmatrix} a_{13} & b_{13} \newline a_{23} & b_{23} \end{bmatrix} \begin{bmatrix} \theta \newline \rho \end{bmatrix} $$ When n =4, $$ \begin{bmatrix} 1 & \beta \end{bmatrix} \begin{bmatrix} a_{14} & b_{14} \newline a_{24} & b_{24} \end{bmatrix} \begin{bmatrix} \theta \newline \rho \end{bmatrix} $$Quantitative Economics with Julia
https://boyoon-c.github.io/posts/19/
Thu, 20 Apr 2023 00:00:00 +0000https://boyoon-c.github.io/posts/19/for loop in, ∈, = Below three for loops return the same output. Note that in, ∈, = can be used interchangeably to indicate that i loops over the following array, 1:2:10 which indicates that the array ranges from 1 to 10 with an increment of 2.
for i in 1:2:10 println(i) end for i ∈ 1:2:10 println(i) end for i = 1:2:10 println(i) end Output:
1 3 5 7 9 eachindex I also introduce in this post eachindex, which returns the index of a given vector.DataFrames.jl (3) and Pipe
https://boyoon-c.github.io/posts/16/
Wed, 19 Apr 2023 00:00:00 +0000https://boyoon-c.github.io/posts/16/Selecting and transforming columns When selecting variables of a data frame, we do add : to indicate that it’s a variable name. Thus for example, if we want to select column with variable x, we use :x inside select().
First let’s construct some 500 by 3 dimensional data frame called df that contains three columns each of which has name of A, B, and C.
using DataFrames df = DataFrame( A=1:2:1000, B= repeat(1:10, inner=50), C= 1:500) Each of below codes produces 500 by 1 dataframe, a data frame that only contains column A.SymPy.jl
https://boyoon-c.github.io/posts/17/
Sun, 16 Apr 2023 00:00:00 +0000https://boyoon-c.github.io/posts/17/SymPy.jl When functions are linear in parameters, we can decompose a matrix of polynomials into a matrix of coefficients and a matrix of variables. To achieve this, we can use the SymPy package.
First we need to ensure that Julia recognizes variables and treats them as symbols.
using SymPy x, y = symbols("x, y") Then we can simply invoke thecoeff() method to extract coefficients from the polynomials.
p = x + 0.DataFrames.jl (2)
https://boyoon-c.github.io/posts/15/
Wed, 12 Apr 2023 00:00:00 +0000https://boyoon-c.github.io/posts/15/Taking a Subset We can subset a dataframe using index. For example we can subset specific columns and rows by following codes.
# column subset (subset df to only include column A and C) df[:, [:A, :C]] # row subset (subset df to only include 1st, 3rd, and 5th rows) df[[1,3,5], :] We could also use some selectors such as Not, Between, Colsand All. Following code removes columns whose names match r"x".DataFrames.jl (1)
https://boyoon-c.github.io/posts/14/
Tue, 11 Apr 2023 00:00:00 +0000https://boyoon-c.github.io/posts/14/Create dataframe To create a dataframe, simply use DataFrame function after loding DataFrames.jl.
Following code produces a dataframe composed of three variables, A, B, and C, each of which has 500 rows. Each row of column A increments by 2 starting from integer 1. Column B repeats 50 times of each integer starting from 1. Coumn C lists integers from 1 to 500.
using DataFrames df = DataFrame(A=1:2:1000, B= repeat(1:10, inner=50), C= 1:500) first, last The first and last functions act as head and tail function in R, which allow us to view the first or the last couple of rows of the dataset.Basics of Julia
https://boyoon-c.github.io/posts/02/
Thu, 30 Mar 2023 00:00:00 +0000https://boyoon-c.github.io/posts/02/This post is about basics of Julia. This is an exercise before running the simulation for my job market paper using Julia