Close Navigation
Learn more about IBKR accounts
The Brass Tacks of Julia Programming – Part III

The Brass Tacks of Julia Programming – Part III

Posted June 15, 2022
Anshul Tayal
QuantInsti

See Part I for instructions on basic arithmetic operations and Part II for working with matrices

Using R and Python code in Julia

There can be times where you want to use your R or Python code as the libraries to perform certain operations may not be available in Julia.

However, using 2 languages to perform a certain operation can be a challenge. Hence, Julia provides an option to use R and Python code by using the “PyCall” and “RCall” packages.

How to use Python code in Julia

Here’s how you can use your Python code in Julia.


# Install the "PyCall" package for Python
using Pkg
Pkg.add("PyCall")
using PyCall

## Importing numpy package from Python
np = pyimport("numpy")

## Using numpy package in Julia
np.array([1,2,3])
Output:

3-element Vector{Int64}:
1
2
3

How to use R code in Julia

Let’s look at an example of using R in Julia.

# Install "RCall" package for R
Pkg.add("RCall")
using RCall

# There are several ways to call R code

# Method 1
# Calling the mean() function from R
rcall(:mean, [1,2,3])

# Method 2 for user-defined functions
R"""
power_4 <- function(x)
{
return(x^4)
}"""
    
## Calling the defined function
rcall(:power_4, 5)
Output:

RObject{RealSxp}
[1] 2
RObject{RealSxp}
[1] 625

For further details on using the RCall package, you can refer here.


Python vs Julia – Comparison of syntax for basic operations

FeaturePythonJulia
Column vector[1, 2, 3][1,2,3]
Row vector[[1], [2], [3]][1 2 3]
Sequence of numbersrange(1, 10,2)[1:2:10; ]
Matrix[[1,2] , [3,4]][1 2; 3 4]
Random numbersnp.random.rand(4,5)rand(4,5)
Mutationinplace =True!
Matrix to vectorA.flatten()A[:]
Conjugate of a matrixA.conj()A’
Element wise operationmap().
Powervar**2var^2
IndexingStarts from 0Starts from 1
Maximum valuemax(A)maximum(A)
Minimum valuemin(A)minimum(A)
For loopfor i in range(n):
# command
for i in 1:n
# command
end
While loopWhile i <=n:While i <=n
If-elseIf i <=m:
# command
Else:
#command
if i <=m
# command
else
# command
end
Temporary functionY = lambda x: x**5Y = x -> x^5
Functiondef func(y, x):
return y + 3*x
func(y, x) = y + 3x
OR
function func(y, x)
return y + 3x
end

Visit QuantInsti for additional insight on this article: https://blog.quantinsti.com/julia-syntax/.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.