Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/deprecated/scripts/ars_envelope.py
1192 views
1
# -*- coding: utf-8 -*-
2
# based on https://github.com/probml/pmtk3/blob/master/demos/arsEnvelope.m
3
# author : Ang Ming Liang
4
5
import superimport
6
7
import numpy as np
8
import matplotlib.pyplot as plt
9
from scipy.stats import norm
10
11
xs = np.linspace(-1.5, 1.5, 50)
12
ps = norm.logpdf(xs)
13
14
dy_dx = lambda x : -x
15
16
fig, ax = plt.subplots()
17
18
# Hide the right and top spines
19
ax.spines['right'].set_visible(False)
20
ax.spines['top'].set_visible(False)
21
22
# Fix the plot size
23
ax.set_xlim(-1.6, 1.6)
24
ax.set_ylim(-2.2,-0.8)
25
26
# Plotting the log-concave distribution
27
ax.plot(xs, ps, 'b-',linewidth=3)
28
29
# At x=-0.7
30
x1 = -0.7
31
ratio1 = dy_dx(x1)
32
ax.plot([x1 - 0.5, x1 + 0.5], [norm.logpdf(x1)-0.5*ratio1, norm.logpdf(x1)+0.5*ratio1], '-r', 'LineWidth', 3);
33
ax.plot([x1, x1], [norm.logpdf(x1),-2.2], '--r', 'LineWidth', 3);
34
35
# At x=0
36
x2 = 0
37
ratio2 = dy_dx(x2)
38
ax.plot([x2 - 0.5, x2 + 0.5], [norm.logpdf(x2)-0.5*ratio2, norm.logpdf(x2)+0.5*ratio2], '-r', 'LineWidth', 3);
39
ax.plot([x2, x2], [norm.logpdf(x2),-2.2], '--r', 'LineWidth', 3);
40
41
# At x=0.7
42
x3 = 0.7;
43
ratio3 = dy_dx(x3)
44
ax.plot([x3 - 0.5, x3 + 0.5], [norm.logpdf(x3)-0.5*ratio3, norm.logpdf(x3)+0.5*ratio3], '-r', 'LineWidth', 3);
45
ax.plot([x3, x3], [norm.logpdf(x3),-2.2], '--r', 'LineWidth', 3);
46
47
# Remove the x-axis and y-axis ticks
48
plt.xticks([])
49
plt.yticks([])
50
51
plt.show()
52
53
54