Skip to content

Commit

Permalink
Add and update notebooks for using the package
Browse files Browse the repository at this point in the history
  • Loading branch information
davide-f committed Oct 17, 2022
1 parent c5afc4f commit 81aeb07
Show file tree
Hide file tree
Showing 2 changed files with 324 additions and 19 deletions.
315 changes: 315 additions & 0 deletions examples/general_distribution.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Basic usage of PointEstimateMethod for a generic distribution provided as a vector of samples\n",
"\n",
"In this notebook, we describe the use of PointEstimateMethod for a generic experimental distribution provided as vectors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic imports"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"using Distributions\n",
"using PointEstimateMethod\n",
"using Random"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup parameters"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Example of general distribution using Distributions.jl: truncated Normal distribution\n",
"distribution = truncated(Normal(1.0, 0.4), 0.0, +Inf);\n",
"\n",
"# number of point estimate models to use\n",
"N_pem = 3;\n",
"\n",
"# number of monte carlo elements used for estimating the moments\n",
"# note: this is an optional parameter for the execution but here specified as an example\n",
"monte_carlo_points = 100000;\n",
"\n",
"# for reproducibility\n",
"SEED = 0;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Estimation PEM"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Using the default function\n",
"In the following cell, the PEM method is applied using distribution described as an UnivariateDistribution of the Distributions.jl package"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"┌ Info: Function moment does not have a direct implementation for Distribution Truncated(Normal{Float64}(μ=1.0, σ=0.4); lower=0.0). Perform Monte Carlo sempling over the distribution with 100000 points\n",
"└ @ PointEstimateMethod c:\\Users\\Davide\\git\\gitdf\\PointEstimateMethod.jl\\src\\pem.jl:55\n"
]
},
{
"data": {
"text/plain": [
"(x = [0.41047975377132706, 1.0537235611993516, 1.7307915848536406], p = [0.21868855722632294, 0.6422623275641921, 0.1390491152094849])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"Random.seed!(SEED) # for reproducibility\n",
"\n",
"pem_output = pem(distribution, N_pem)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Locations of the points"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3-element Vector{Float64}:\n",
" 0.41047975377132706\n",
" 1.0537235611993516\n",
" 1.7307915848536406"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pem_output.x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Probabilities of the points"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3-element Vector{Float64}:\n",
" 0.21868855722632294\n",
" 0.6422623275641921\n",
" 0.1390491152094849"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pem_output.p"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Using a sampled distribution\n",
"\n",
"The PEM package is also able to calculate the desired PEM points using a vector as representative of a distribution.\n",
"In the example that follows, the proposed distribution described above is sampled to show that results are equivalent."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sample the distribution"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5-element Vector{Float64}:\n",
" 1.3771882133784477\n",
" 1.0535691030612737\n",
" 1.610027563404992\n",
" 1.0495604924822388\n",
" 0.5176910862960256"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"Random.seed!(SEED) # for reproducibility\n",
"\n",
"sampled_distribution = rand(distribution, monte_carlo_points);\n",
"\n",
"first(sampled_distribution, 5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Execute the PEM method"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"┌ Info: Function moment does not have a direct implementation for Distribution Truncated(Normal{Float64}(μ=1.0, σ=0.4); lower=0.0). Perform Monte Carlo sempling over the distribution with 100000 points\n",
"└ @ PointEstimateMethod c:\\Users\\Davide\\git\\gitdf\\PointEstimateMethod.jl\\src\\pem.jl:55\n"
]
},
{
"data": {
"text/plain": [
"(x = [0.41047975377132706, 1.0537235611993516, 1.7307915848536406], p = [0.21868855722632294, 0.6422623275641921, 0.1390491152094849])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"Random.seed!(SEED) # for reproducibility\n",
"\n",
"pem_output_sampled = pem(distribution, N_pem)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Locations of the points"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3-element Vector{Float64}:\n",
" 0.41047975377132706\n",
" 1.0537235611993516\n",
" 1.7307915848536406"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pem_output_sampled.x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Probability of the points"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3-element Vector{Float64}:\n",
" 0.21868855722632294\n",
" 0.6422623275641921\n",
" 0.1390491152094849"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pem_output_sampled.p"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.7.3",
"language": "julia",
"name": "julia-1.7"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.7.3"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
28 changes: 9 additions & 19 deletions examples/simple_normal.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,10 @@
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"distribution = Normal() # distribution to consider\n",
"N_pem = 3 # number of point estimate models to use"
"distribution = Normal(); # distribution to consider\n",
"N_pem = 3; # number of point estimate models to use"
]
},
{
Expand All @@ -68,7 +58,7 @@
{
"data": {
"text/plain": [
"(x = [-1.7320508075688772, 1.7320508075688776, 0.0], p = [0.16666666666666674, 0.16666666666666655, 0.6666666666666667])"
"(x = BigFloat[-1.73205080756887763726581397349946200847625732421875, 1.732050807568877193176604123436845839023590087890625, 0.0], p = [0.1666666666666666, 0.16666666666666663, 0.6666666666666667])"
]
},
"metadata": {},
Expand All @@ -94,9 +84,9 @@
{
"data": {
"text/plain": [
"3-element Vector{Float64}:\n",
" -1.7320508075688772\n",
" 1.7320508075688776\n",
"3-element Vector{BigFloat}:\n",
" -1.73205080756887763726581397349946200847625732421875\n",
" 1.732050807568877193176604123436845839023590087890625\n",
" 0.0"
]
},
Expand Down Expand Up @@ -124,8 +114,8 @@
"data": {
"text/plain": [
"3-element Vector{Float64}:\n",
" 0.16666666666666674\n",
" 0.16666666666666655\n",
" 0.1666666666666666\n",
" 0.16666666666666663\n",
" 0.6666666666666667"
]
},
Expand Down

0 comments on commit 81aeb07

Please sign in to comment.