Gaussian Density¶

  • Write a function gaussian_density (or gaussianDensity, according to your language's case convention) that takes $\mu$ (mu, representing the mean), $\sigma$ (sigma, representing the standard deviation), and $x$, and returns the value of the Gaussian (normal) probability density function at $x$:

$$ \frac{1}{ \sqrt{2\pi}\,\sigma } \exp\!\left(-\frac{ (x-\mu)^2} {2\sigma^2} \right) $$

  • Use 64-bit floating point numbers for $x$, $\mu$, and $\sigma$.

  • Consider introducing local variables appropriately, to avoid making a single expression too large to read.

  • Boilerplate source files {go,jl,ml,rs}/gaussian_density.{go,jl,ml,rs} containing the test code is generated and shown below.

  • Edit the source files either by opening them in a text editor (e.g., vscode), or editing the cells below and executing them.

1. AI tutor¶

1-1. Prepare¶

  • Your personal AI tutor is provided for questions and feedback
  • Execute the following cell before you use it
In [ ]:
import heytutor

1-2. Examples¶

1-2-1. A general question¶

%%hey
How to write a function in Go?

1-2-2. A hint on this specific problem¶

%%hey
Give me a hint on this problem for Rust

1-2-3. NEW: A few builtin variables¶

  • {file:FILENAME} is the content of FILE
  • {bash[-1]} is the output of the last %%bash_ cell, {bash[-2]} that of the second last %%bash_ cell, etc.
  • {problem} is the content of the file you specified by %%hey problem_file=foo.md
  • {answer} is the content of the file you specified by %%hey answer_file=go/foo.go

1-2-4. Help when you struggle¶

%%hey answer_file=go/foo.go
I get this error when I compile it. What's wrong?"

My program:
{answer}

Error message:
{bash[-1]}

1-2-5. Ask feedback¶

  • You are encouraged to ask a feedback once you think you are done with the problem, to know if there is a better answer. You can do so by something like:
%%hey problem_file=foo.md answer_file=go/foo.md
Give me a feedback to my answer.

Problem:
{problem}

My Answer:
{answer}

2. Go¶

2-1. Baseline code¶

In [ ]:
import heytutor
In [ ]:
%%writefile_ go/gaussian_density.go
package main
import "fmt"
import "math"

/** begin my answer */

func gaussianDensity(mu, sigma, x float64) float64 {
	z := (x - mu) / sigma
	z2 := z * z
	norm := 1.0 / (math.Sqrt(2.0 * math.Pi) * sigma)
	return norm * math.Exp(-0.5 * z2)
}
/** end my answer */
func main() {
	if !(math.Abs(gaussianDensity(0.0, 1.0, 0.0) - 0.398942) < 1.0e-5) { panic("wrong") }
	if !(math.Abs(gaussianDensity(0.0, 2.0, 1.0) - 0.176033) < 1.0e-5) { panic("wrong") }
	if !(math.Abs(gaussianDensity(1.0, 3.0, 5.0) - 0.054670) < 1.0e-5) { panic("wrong") }
	fmt.Println("OK")
}

2-2. Compile¶

In [ ]:
%%bash_
export PATH=${PATH}:~/.local/go/bin:~/go/bin
go build -o go/gaussian_density go/gaussian_density.go
  • Note: when you run go or other Go commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (export PATH=${PATH}:~/go/bin)
  • You may consider adding that line in your ~/.bash_profile

2-3. Run¶

In [ ]:
%%bash_
go/gaussian_density

2-4. Ask Questions or Get Feedback¶

In [ ]:
%%hey problem_file=gaussian_density.md answer_file=go/gaussian_density.go

Problem:
{problem}
My Answer (between /** begin my answer */ and /** end my answer */):
{answer}

Give me a feedback to my answer.

3. Julia¶

3-1. Baseline code¶

In [ ]:
import heytutor
In [ ]:
%%writefile_ jl/gaussian_density.jl
### begin my answer

function gaussian_density(mu, sigma, x)
    z = (x - mu) / sigma
    z2 = z * z
    norm = 1 / (sqrt(2π) * sigma)
    norm * exp(-0.5 * z2)
end
### end my answer

function main()
    @assert abs(gaussian_density(0.0, 1.0, 0.0) - 0.398942) < 1.0e-5
    @assert abs(gaussian_density(0.0, 2.0, 1.0) - 0.176033) < 1.0e-5
    @assert abs(gaussian_density(1.0, 3.0, 5.0) - 0.054670) < 1.0e-5
    println("OK")
end

main()

3-2. Compile¶

  • Julia code is compiled "just in time" (compiled upon executed), so does not need a specific action for compilation before you run

3-3. Run¶

In [ ]:
%%bash_
export PATH=${PATH}:~/.juliaup/bin
julia jl/gaussian_density.jl
  • Note: when you run julia or other Julia commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (export PATH=${PATH}:~/.juliaup/bin)
  • You may consider adding that line in your ~/.bash_profile

3-4. Interactive execution¶

  • julia command also serves is an interactive command for Julia programs

  • You can run a source code and continue interaction

$ julia -i jl/gaussian_density.jl
  • For trial and error, you may also consider creating a Julia notebook

3-5. Ask Questions or Get Feedback¶

In [ ]:
%%hey problem_file=gaussian_density.md answer_file=jl/gaussian_density.jl

Problem:
{problem}

My Answer (between ### begin my answer and ### end my answer):
{answer}

Give me a feedback to my answer.

4. OCaml¶

4-1. Baseline code¶

In [ ]:
import heytutor
In [ ]:
%%writefile_ ml/gaussian_density.ml
(** begin my answer *)

let gaussian_density mu sigma x =
  let z = (x -. mu) /. sigma in
  let z2 = z *. z in
  let norm = 1.0 /. (sqrt (2.0 *. Float.pi) *. sigma) in
  norm *. exp (-0.5 *. z2);;
(** end my answer *)

let main () =
  assert (abs_float (gaussian_density 0.0 1.0 0.0 -. 0.398942) < 1.0e-5);
  assert (abs_float (gaussian_density 0.0 2.0 1.0 -. 0.176033) < 1.0e-5);
  assert (abs_float (gaussian_density 1.0 3.0 5.0 -. 0.054670) < 1.0e-5);
  Printf.printf "OK\n"
;;

main()

4-2. Compile¶

In [ ]:
%%bash_
eval $(opam env)
ocamlc ml/gaussian_density.ml -o ml/gaussian_density
  • Note: when you run ocamlc or other OCaml commands (see below) in a terminal (SSH or Jupyter terminal), you need to execute the first line (eval $(opam env))
  • You may consider adding that line in your ~/.bash_profile

4-3. Run¶

In [ ]:
%%bash_
ml/gaussian_density

4-4. Interactive execution¶

  • ocaml command is an interactive command for OCaml programs

  • In terminal (Jupyter or SSH), you can directly run a source code

$ eval $(opam env)   # once in your session or put it in ~/.bash_profile
$ ocaml ml/gaussian_density.ml
  • You can run a source code and continue interaction
$ eval $(opam env)   # once in your session or put it in ~/.bash_profile
$ ocaml -init ml/gaussian_density.ml
  • For trial and error, you may also consider creating an OCaml notebook

4-5. Ask Questions or Get Feedback¶

In [ ]:
%%hey problem_file=gaussian_density.md answer_file=ml/gaussian_density.ml

Problem:
{problem}

My Answer (between (** begin my answer *) and (** end my answer *)):
{answer}

Give me a feedback to my answer.

5. Rust¶

5-1. Baseline code¶

In [ ]:
import heytutor
In [ ]:
%%writefile_ rs/gaussian_density.rs
/** begin my answer */

fn gaussian_density(mu: f64, sigma: f64, x: f64) -> f64 {
    let z = (x - mu) / sigma;
    let z2 = z * z;
    let norm = 1.0 / ((2.0 * std::f64::consts::PI).sqrt() * sigma);
    norm * (-0.5 * z2).exp()
}
/** end my answer */

fn main() {
    assert!((gaussian_density(0.0, 1.0, 0.0) - 0.398942).abs() < 1.0e-5);
    assert!((gaussian_density(0.0, 2.0, 1.0) - 0.176033).abs() < 1.0e-5);
    assert!((gaussian_density(1.0, 3.0, 5.0) - 0.054670).abs() < 1.0e-5);
    println!("OK")
}

5-2. Compile¶

In [ ]:
%%bash_
. ~/.cargo/env
rustc rs/gaussian_density.rs -o rs/gaussian_density
  • Note: when you run rustc or other Rust commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (. ~/.cargo/env)
  • You may consider adding that line in your ~/.bash_profile

5-3. Run¶

In [ ]:
%%bash_
rs/gaussian_density

5-4. Ask Questions or Get Feedback¶

In [ ]:
%%hey problem_file=gaussian_density.md answer_file=rs/gaussian_density.rs

Problem:
{problem}

My Answer (between /** begin my answer */ and /** end my answer */):
{answer}

Give me a feedback to my answer.