The Brass Tacks of Julia Programming – Part III

QuantInsti

Contributor:
QuantInsti
Visit: 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 Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight 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 permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR 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 sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. 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.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.