# Scan the QR code to sign up for the source creation #
original text ：Julia vs R vs Python: simple optimization
author ：ZJ, Data scientist , Full stack engineer , Head of the credit risk model team .
compile ： Open source in China （oschina2013）
If you need to reprint, please indicate the above information in the text
In this article , The author optimizes by a simple likelihood function （Maximum Likelihood Optimization） Let's make a comparison Julia,R and Python. This is a relatively small optimization problem , The performance difference may not be obvious , But the process of solving problems can well reflect the advantages and disadvantages of the three .
At the time of writing this article , The familiarity with these three languages is as follows ：
Julia evangelist ChrisRackauckas Once said ：
If you use Julia Deal with one 10 The question of seconds , Its advantages don't show up . And once the problem gets complicated , It takes a long time , At this time Julia The advantages of the company will gradually manifest itself .
Someone uses it Python and Julia Did a comparative experiment . With 10⁵ Calculate for the boundary point , When the value ratio 10⁵ Less time Python Than Julia fast . But the value is greater than 10⁵ after ,Julia It's faster than Python Much faster .
Watch the sequence Q1,Q2,…,Qn, We need to find the parameters that optimize the likelihood function μ and σ：
Usually we try to optimize log likelihood ：
Statistically , This is the maximum likelihood estimation of the truncated normal distribution （MLE）.
Julia The test situation of
Here's how the author uses Julia The situation of testing . Use Julia Medium Optim.jl, You can use special symbols directly （symbols） As variable name , According to the usage habit , Here the author uses the Greek alphabet μσ.Julia One more JuMP.jl Packages are used to optimize problems . but JuMP.jl More suitable for more advanced optimization problems , It's a bit of a fuss to use here .
Julia First optimization
Julia In performing the first optimization, we used 7 second , Than R and Python All slow . Regarding this ,ChrisRackauckas Pointed out that ：
If you need to solve 100 individual 10 Second optimization problem , The first execution costs 17 second , The next optimization doesn't require compilation , It's about 10 second . therefore , The total running time is 1007 second . therefore , When used Julia Deal with one 10⁵ The second question is , this 7 Seconds can be ignored ; But if use Julia Handle 5 Seconds or less , this 7 The difference in seconds is particularly obvious .
The author hardcoded below in MLE Used in the estimate Q_t Value ：
The output effect is as follows , The layout looks very comfortable , It also supports math public display ：
From that ** Julia The advantages of ：**
Julia Deficiency ：**
R The test situation of
R There is one truncnorm Used to deal with truncated normality
The result will output ：
R The advantages of ：
R Deficiency ：
Python The test situation of
The author makes use of the existing Python Learn from experience and come up with the following plan , Enter the code ：
Output results ：
Python The advantages of ：
Python Deficiency ：
in summary , A comprehensive comparison of the three languages is as follows ：