# Introduction to Probability for Data Science: collaborative effort to translate code to Julia

**URL:** https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614
**Category:** Teaching & Outreach
**Tags:** book, probability
**Created:** [October 12, 2021, 11:09am UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614 "2021-10-12T11:09:41Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [October 12, 2021, 11:09am UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/1 "2021-10-12T11:09:41Z")

</div>

There is a collaborative effort underway to translate to Julia the Python and Matlab code that accompanies Stanley H. Chan’s textbook _Introduction to Probability for Data Science_. This effort is authorized by Dr. Chan. The textbook is free to download as a pdf. Please see the page [Introduction to Probability for Data Science](https://probability4datascience.com/index.html) for more information about the book.

My opinion is that this is a very nice probability and statistics textbook for undergraduate students of data science, and that it has the potential to be very widely used.

The code to be translated is quite basic (for the most part). This is an opportunity to participate in helping to make Julia visible to many people, even if your experience with Julia programming is not that great.

The code to be translated is in the **Code and Data** section of the web page mentioned above, at the bottom left.

If you are interested in translating the code for a chapter, please say so here, and I will edit this top message to keep track of who is working on which chapter. Please sign up only if you can get the work done in a couple of weeks, at the most.

Please make pull requests for completed work to [https://github.com/mcreel/IntProbDS.jl](https://github.com/mcreel/IntProbDS.jl)

Regarding style, I suggest to follow the style used in the Julia code for Ch. 1, and the style of the Python and Matlab code: self contained blocks that can be pasted into the REPL, and which will run independently of other blocks.

Ch. 1: completed - mcreel  
Ch. 2: no code for Ch. 2  
Ch. 3: completed - vinicius\_de\_lima  
Ch. 4: completed - rafaelchp  
Ch. 5: completed - Elias Carvalho  
Ch. 6: completed - j\_verzani  
Ch. 7: completed . Paul\_Soderlind  
Ch. 8: completed - mcreel  
Ch. 9: completed - mcreel  
Ch. 10: congUoM

---

<div class="post-metadata">

### Author: ![vinicius\_de\_lima](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinicius_de_lima/32/28717_2.png) [@vinicius\_de\_lima](https://discourse.julialang.org/u/vinicius_de_lima)
#### Post date: [October 12, 2021, 8:22pm UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/2 "2021-10-12T20:22:14Z")

</div>

That’s a wonderful initiative! I would like to contribute with the translation of the Ch. 3.

---

<div class="post-metadata">

### Author: ![eliascarv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eliascarv/32/22152_2.png) [@eliascarv](https://discourse.julialang.org/u/eliascarv)
#### Post date: [October 13, 2021, 1:23am UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/3 "2021-10-13T01:23:09Z")

</div>

Excellent initiative!  
Hi, my name is Elias Carvalho and I would like to contribute with the translation of chapter 5.

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [October 13, 2021, 5:07am UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/4 "2021-10-13T05:07:28Z")

</div>

Excellent! Thanks to both of you. Please feel free to open issues, etc., if you have suggestions about organization, style, etc.

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [October 13, 2021, 6:12pm UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/5 "2021-10-13T18:12:15Z")

</div>

Code for Chapter 8 (estimation) is done.

---

<div class="post-metadata">

### Author: ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)
#### Post date: [October 13, 2021, 6:43pm UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/6 "2021-10-13T18:43:58Z")

</div>

Before I sign up, could I ask whether you expect the code to be (mostly) bottom up or to introduce various Julia packages? (I was thinking of Ch 7 in particular.) /Paul S

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [October 13, 2021, 7:22pm UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/7 "2021-10-13T19:22:41Z")

</div>

I’ve done Ch1 and Ch8 trying to use a style similar to the code on the book’s web page, so that instructors who decide to use Julia will be able to follow the text without difficulties. I do take advantage of things like broadcasting, and I have made use some use of specialized packages like ImageView.jl, and considerable use of Distributions.jl, but it has been pretty easy to stay close to the Matlab and Python code. However, please work the way you wish, if you decide to go ahead. Getting an initial version done is the main thing. We can try for some uniformity later, if it seem necessary.

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [October 14, 2021, 6:33am UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/8 "2021-10-14T06:33:52Z")

</div>

Paul: Ch7 uses \ for OLS fit in the Python code, and that seems natural to use for Julia as well, instead of a package. However, for linear programming (almost certainly), Lasso (probably), and perhaps Legendre polynomials, which I don’t know about, a package could be the way to go.

One doubt I have is about leaving the code at global score, or wrapping in a function. Given that the target audience is people learning about probability for doing data science, there’s the desire to communicate the theory, but also to become a data scientist, where performance of code is a consideration. Wrapping in a function is a departure from the Matlab and Python code, but probably it should be done to teach good Julia habits. Any opinions on that?

---

<div class="post-metadata">

### Author: ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)
#### Post date: [October 14, 2021, 7:10am UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/9 "2021-10-14T07:10:02Z")

</div>

> However, for linear programming (almost certainly), Lasso (probably), and perhaps Legendre polynomials, which I don’t know about, a package could be the way to go.

OK, I’ll give ch. 7 a try then.

Yes, some functions should perhaps be sneaked in here and there.

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [October 14, 2021, 7:22am UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/10 "2021-10-14T07:22:34Z")

</div>

Great, thanks!

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [October 14, 2021, 3:47pm UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/11 "2021-10-14T15:47:55Z")

</div>

Chapters 4 and 6 still available at the moment. These ones are reasonably small projects…

---

<div class="post-metadata">

### Author: ![r2cp](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@r2cp](https://discourse.julialang.org/u/r2cp)
#### Post date: [October 14, 2021, 3:50pm UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/12 "2021-10-14T15:50:19Z")

</div>

Hello, I would like to contribute with chapter 4.

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [October 14, 2021, 3:57pm UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/13 "2021-10-14T15:57:45Z")

</div>

Great! Thanks!

---

<div class="post-metadata">

### Author: ![r2cp](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@r2cp](https://discourse.julialang.org/u/r2cp)
#### Post date: [October 14, 2021, 4:09pm UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/14 "2021-10-14T16:09:21Z")

</div>

I’m sorry, I didn’t noticed that StatsPlots was removed from the project dependencies. I think it would be particularly useful in Chapter 4. Should I try to reproduce the plots in the same way that the ones from MATLAB? For instance, defining the support vector and evaluating the pdf at those points.

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [October 14, 2021, 4:17pm UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/15 "2021-10-14T16:17:49Z")

</div>

Please go ahead and use StatsPlots if it adds things that are needed that aren’t in Plots. Apparently, the most recent StatsPlots will use an older version of Plots, but I don’t think that the difference will impact the code we’re working with.

---

<div class="post-metadata">

### Author: ![congUoM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/conguom/32/10413_2.png) [@congUoM](https://discourse.julialang.org/u/congUoM)
#### Post date: [October 14, 2021, 4:34pm UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/16 "2021-10-14T16:34:13Z")

</div>

Hi, I can contribute to chapter 10.

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [October 14, 2021, 4:51pm UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/17 "2021-10-14T16:51:42Z")

</div>

Excellent! Thanks!

---

<div class="post-metadata">

### Author: ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)
#### Post date: [October 14, 2021, 7:02pm UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/18 "2021-10-14T19:02:00Z")

</div>

I had been looking over Ch 7 to make sure it was doable, but see that was assigned. I can do Ch 6 if still open.

As for Ch 7, I was using `SpecialPolynomials` for the Legendre polynomials, but if dependencies are an issue, this function might be helpful. It evaluates Legendre polynomials via Clenshaw reduction:

```julia
function legendre(cs, x)
    N = length(cs)
    R = typeof(one(x)*cs[1]/1)
    p₀ = one(R)
    N == 0 && return zero(R)
    N == 1 && return cs[1] * p₀

    Δ0::R = cs[end - 1]
    Δ1::R = cs[end]

    @inbounds for i in (N - 1):-1:2
        An,Bn,Cn = (2i-1)/i, zero(R), (i-1)/i
        Δ0, Δ1 = cs[i - 1] - Δ1 * Cn, Δ0 + Δ1 * muladd(x, An, Bn)
    end
    p₁ = muladd(x, one(R), zero(R))
    return Δ0 * p₀ + Δ1 * p₁
end
# for a single basis
legendre(i::Int, x) = legendre(ntuple(j -> (j == i+1) ? 1 : 0, i+1), x)

```

---

<div class="post-metadata">

### Author: ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)
#### Post date: [October 14, 2021, 7:26pm UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/19 "2021-10-14T19:26:37Z")

</div>

For ch 07, I have created a rough draft. You find it at [https://github.com/mcreel/IntProbDS.jl/pull/2#issuecomment-943635817](https://github.com/mcreel/IntProbDS.jl/pull/2#issuecomment-943635817)

There is a lot of `LegendrePolynomials.jl` in that code. Please have a look and let me know what you think.

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [October 14, 2021, 8:45pm UTC](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614/20 "2021-10-14T20:45:20Z")

</div>

Yes, Ch6 is still open, so I will put your name there. Thanks!

[Next page](https://discourse.julialang.org/t/introduction-to-probability-for-data-science-collaborative-effort-to-translate-code-to-julia/69614.md?page=2)
