File: plot_estimate_conditional_quantile.py

package info (click to toggle)
openturns 1.24-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 66,204 kB
  • sloc: cpp: 256,662; python: 63,381; ansic: 4,414; javascript: 406; sh: 180; xml: 164; yacc: 123; makefile: 98; lex: 55
file content (202 lines) | stat: -rw-r--r-- 5,359 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""
Estimate a conditional quantile
===============================
"""

# sphinx_gallery_thumbnail_number = 8
# %%
# From a multivariate data sample, we estimate a distribution with kernel smoothing.
# Here we present a bivariate distribution :math:`X= (X_1, X_2)`.
# We use the `computeConditionalQuantile` method to estimate the 90% quantile :math:`Q_1` of the conditional variable :math:`X_2|X_1` :
#
# .. math::
#    Q_2 : x_1 \mapsto q_{0.9}(X_2|X_1=x_1)
#
# We then draw the curve :math:`Q_2 : x_1 \mapsto Q_2(x_1)`.
# We first start with independent Normals then we consider dependent marginals with a Clayton copula.
#

# %%
import openturns as ot
import openturns.viewer as viewer
import numpy as np

ot.Log.Show(ot.Log.NONE)

# %%
# Set the random generator seed
ot.RandomGenerator.SetSeed(0)

# %%
# Defining the marginals
# ----------------------
#
# We consider two independent Normal marginals :
X1 = ot.Normal(0.0, 1.0)
X2 = ot.Normal(0.0, 3.0)


# %%
# Independent marginals
# ---------------------

distX = ot.JointDistribution([X1, X2])
sample = distX.getSample(1000)


# %%
# Let's see the data

# %%
graph = ot.Graph("2D-Normal sample", "x1", "x2", True, "")
cloud = ot.Cloud(sample, "blue", "fsquare", "My Cloud")
graph.add(cloud)
graph.setXTitle("$X_1$")
graph.setYTitle("$X_2$")
graph.setTitle("A sample from $X=(X_1, X_2)$")
view = viewer.View(graph)

# %%
# We draw the isolines of the PDF of :math:`X` :
graph = distX.drawPDF()
graph.setXTitle("$X_1$")
graph.setYTitle("$X_2$")
graph.setTitle("iso-PDF of $X=(X_1, X_2)$")
view = viewer.View(graph)

# %%
# We estimate the density with kernel smoothing :
kernel = ot.KernelSmoothing()
estimated = kernel.build(sample)


# %%
# We draw the isolines of the estimated PDF of :math:`X` :
graph = estimated.drawPDF()
graph.setXTitle("$X_1$")
graph.setYTitle("$X_2$")
graph.setTitle("iso-PDF of $X=(X_1, X_2)$ estimated by kernel smoothing")
view = viewer.View(graph)


# %%
# We can compute the conditional quantile of :math:`X_2 | X_1` with the `computeConditionalQuantile` method and draw it after.
#

# %%
# We first create :math:`\sampleSize` observation points in :math:`[-3.0, 3.0]` :

# %%
N = 301
xobs = np.linspace(-3.0, 3.0, N)
sampleObs = ot.Sample([[xi] for xi in xobs])

# %%
# We create curves of the exact and approximated quantile :math:`Q_1`

# %%
x = [xi for xi in xobs]
yapp = [estimated.computeConditionalQuantile(0.9, sampleObs[i]) for i in range(N)]
yex = [distX.computeConditionalQuantile(0.9, sampleObs[i]) for i in range(N)]

# %%
cxy_app = ot.Curve(x, yapp)
cxy_ex = ot.Curve(x, yex)
graph = ot.Graph("90% quantile of $X_2 | X_1=x_1$", "$x_1$", "$Q_2(x_1)$", True, "")
graph.add(cxy_app)
graph.add(cxy_ex)
graph.setLegends(["$Q_2$ kernel smoothing", "$Q_2$ exact"])
graph.setLegendPosition("lower right")
view = viewer.View(graph)

# %%
# In this case the :math:`Q_2` quantile is constant because of the independence of the marginals.
#

# %%
# Dependence through a Clayton copula
# -----------------------------------
#
# We now define a Clayton copula to model the dependence between our marginals.
# The Clayton copula is a bivariate asymmmetric Archimedean copula, exhibiting greater dependence
# in the negative tail than in the positive.
#

# %%
copula = ot.ClaytonCopula(2.5)
distX = ot.JointDistribution([X1, X2], copula)

# %%
# We generate a sample from the distribution :
sample = distX.getSample(1000)

# %%
# Let's see the data

# %%
graph = ot.Graph("2D-Normal sample", "x1", "x2", True, "")
cloud = ot.Cloud(sample, "blue", "fsquare", "My Cloud")
graph.add(cloud)
graph.setXTitle("$X_1$")
graph.setYTitle("$X_2$")
graph.setTitle("A sample from $X=(X_1, X_2)$")
view = viewer.View(graph)

# %%
# We draw the isolines of the PDF of :math:`X` :
graph = distX.drawPDF()
graph.setXTitle("$X_1$")
graph.setYTitle("$X_2$")
graph.setTitle("iso-PDF of $X=(X_1, X_2)$")
view = viewer.View(graph)

# %%
# We estimate the density with kernel smoothing :
kernel = ot.KernelSmoothing()
estimated = kernel.build(sample)

# %%
# We draw the isolines of the estimated PDF of :math:`X` :
graph = estimated.drawPDF()
graph.setXTitle("$X_1$")
graph.setYTitle("$X_2$")
graph.setTitle("iso-PDF of $X=(X_1, X_2)$ estimated by kernel smoothing")
view = viewer.View(graph)


# %%
# We can compute the conditional quantile of :math:`X_2 | X_1=x1` with the `computeConditionalQuantile` method and draw it after.
#

# %%
# We first create :math:`\sampleSize` observation points in :math:`[-3.0, 3.0]` :

# %%
N = 301
xobs = np.linspace(-3.0, 3.0, N)
sampleObs = ot.Sample([[xi] for xi in xobs])

# %%
# We create curves of the exact and approximated quantile :math:`Q_1`

# %%
x = [xi for xi in xobs]
yapp = [estimated.computeConditionalQuantile(0.9, sampleObs[i]) for i in range(N)]
yex = [distX.computeConditionalQuantile(0.9, sampleObs[i]) for i in range(N)]

# %%
cxy_app = ot.Curve(x, yapp)
cxy_ex = ot.Curve(x, yex)
graph = ot.Graph("90% quantile of $X_2 | X_1=x_1$", "$x_1$", "$Q_2(x_1)$", True, "")
graph.add(cxy_app)
graph.add(cxy_ex)
graph.setLegends(["$Q_2$ kernel smoothing", "$Q_2$ exact"])
graph.setLegendPosition("lower right")
view = viewer.View(graph)

# %%
# Our estimated conditional quantile is a good approximate and should be better the more data we have. We can observe it by increasing the number of samples.

# %%
# Display the graphs
view.ShowAll()