Weights and interpolation -- Replicating Python code with hand calculations

In summary, the code calculates the evolution of a function of a continuous uniform random variable U(-1,1) using an interpolation polynomial with a given order and a set of points. The code also returns the Nodes, Weights, Mean and Variances in each of the points. To replicate this, the book suggests using the Legendre polynomials and the expression Integrate lj(N)*wdx to calculate the weights, where lj(N) is the corresponding Legendre polynomial.
  • #1
confused_engineer
39
2
Hello everyone. I have a Python code which calculates, given a continuos uniform random variable U(-1,1), the order of a interpolation polynomial and a set of points the evolution of a function of this random variable. i.e.
v0 = cp.Uniform(-1,1)
t = np.linspace(0, 10, 10)
order=1
.
.
.
plt.plot (t, v0*t)

and it returns the Nodes, Weights, Mean and Variances in each of the 10 points.

I want to replicate this by hand.

I have no issue with the zeros, since I just have to find the ones of (½)*x*(5*x2-3), but I don't know how to calculate the weights. I am following the book Numerical Methods for Stochastic Computations A Spectral Method Approach by Dongbin Xiu (page 40), where it suggests that I should write wj(N)=Integrate lj(N)wdx.
Does this means that I shoud integrate (½)*x*(5*x2-3) multiplied by the random variable U(-1, 1)? Am I understanding this wrong? I don't know how to integrate a random variable...
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
confused_engineer said:
Hello everyone. I have a Python code which calculates, given a continuos uniform random variable U(-1,1), the order of a interpolation polynomial and a set of points the evolution of a function of this random variable. i.e.
v0 = cp.Uniform(-1,1)
t = np.linspace(0, 10, 10)
The line above creates an array with 10 elements, spaced along the interval [0, 10]. If you want the elements to be 0, 1, 2, ..., 10, do this:
Python:
t = np.linspace(0, 10, 11)
A partition of an interval with 10 subintervals is defined by 11 points.
confused_engineer said:
order=1
.
.
.
plt.plot (t, v0*t)

and it returns the Nodes, Weights, Mean and Variances in each of the 10 points.
I don't see how this is done, based on the code you're showing.
confused_engineer said:
I want to replicate this by hand.

I have no issue with the zeros, since I just have to find the ones of (½)*x*(5*x2-3), but I don't know how to calculate the weights. I am following the book Numerical Methods for Stochastic Computations A Spectral Method Approach by Dongbin Xiu (page 40), where it suggests that I should write wj(N)=Integrate lj(N)wdx.
What is ##l_j^{(N)}##?
Also, is w your weighting function -- ##w(x) = \frac 1 2 x(5x^2 - 3)##?
confused_engineer said:
Does this means that I shoud integrate (½)*x*(5*x2-3) multiplied by the random variable U(-1, 1)?
U(-1, 1) isn't the random variable. This notation means that the random variable X is uniformly distributed in the interval [-1, 1].
confused_engineer said:
Am I understanding this wrong? I don't know how to integrate a random variable...
BTW, I used BBCode code tags for the Python code I showed above, and I used LaTeX scripting for the equations.
We have tutorials on both of these:
Code tags - https://www.physicsforums.com/threads/read-this-before-posting-code-geshi-syntax-highlighter.773407/
LaTeX primer - https://www.physicsforums.com/help/latexhelp/
 
  • Like
Likes Grands
  • #3
I apologize if I haven't expressed myself propplerly.
Here it is the full code:

import chaospy as cp
import numpy as np
import odespy as od
import matplotlib.pyplot as plt
np.set_printoptions(threshold=1000,edgeitems=1000)
def s(t, v0):
return v0*t
v0 = cp.Uniform(-1,1)
#a = cp.Beta(2,2)

#v0 = cp.Normal(0,1)
#a = cp.Normal(0,1)
dist = v0
#t = np.linspace(0, 10, 1000)
t = np.linspace(0, 10, 10)
print(t)
order=1
P, norms = cp.orth_ttr(order, dist, retall=True)
nodes, weights = cp.generate_quadrature(order+1, dist, rule="G")
samples_u = [s(t, *n) for n in nodes.T]
u_hat, coef = cp.fit_quadrature(P, nodes, weights, samples_u, retall=True)
print(nodes.shape)
print(weights.shape)
print(len(samples_u))
sensitivity_Q = cp.Sens_t(u_hat, dist)
#plt.plot(t, cp.E(u_hat, dist))
#plt.plot(t, cp.Var(u_hat, dist))
E = cp.E(u_hat, dist)
Var = cp.Var(u_hat, dist)
Analit_Var = np.array((1./240.)*t**2*(20+3*t**2))
print('Polynomials')
print(P)
print('Samples')
print(samples_u)
print('Norms')
print(norms)
print('Nodes')

print(nodes)
print('Weights')
print(weights)

print('Mean')
print(E)
print('Variance')
print(Var)
print('u_hat')
print(u_hat)
print(coef)
#print(sensitivity_Q)
#plt.plot(t,E)
#plt.plot(t,Var)
#plt.plot (t, [1, 2, 4, 8, 16, 32, 64, 128, 129, 130]) #esto funciona
plt.plot (t, Analit_E, 'bs')
plt.plot(t, Analit_Var, 'g^')
plt.plot(t, Analit_E, t, Analit_Var)
plt.show()

I wanted to replicate what this program returns. The proper base for a uniform variable are the legrendre polynomials. If I need three zeros, the legendre polynomial which has three zeros is (1/2)*x*(5*x3-3), and I find the same zeros in this polynomial as the ones the code returns.

Now, I want to calculate the weights, and for that I want to use the expression found in the book which I mentioned, where lj(N) is the corresponding Legendre polynomial. I will rewrite the expression: Integrate between -1 and 1: lj(N)*wdx.
This weight calculation is what I don't know how to do.

Edit: the Python anwser is
Weights
[ 0.27777778 0.44444444 0.27777778]
 
Last edited:
  • #4
I don't think you have understood the text you refer to correctly. Firstly, ##l_j^N## is the Lagrange interpolation polynomial, and not the Legendre polynomial. Now, what is discussed in the text is the Gauss-quadrature formula of the form
$$\int_{-1}^{1} W(x) f(x) dx =\sum_{k=1}^n w_k f(x_k) $$
where ##W(x)## is the weight function and ##x_k## are the zeroes of the corresponding orthogonal polynomial, and ##w_k##. In your case one has ##W(x)=1## meaning that the polynomial is the Legendre polynomial of order ##n##. The easiest way to compute the weights is to use the formula (see http://mathworld.wolfram.com/Legendre-GaussQuadrature.html)
$$w_k=\frac{2}{nP_{n-1}(x_k)P'_n(x_k)}$$

Your last post also contains a typo. The polynomial should be
$$P_3(x)=(1/2)x(5x^2-3)$$

I hope this helps.
 
  • Like
Likes confused_engineer
  • #5
eys_physics said:
I don't think you have understood the text you refer to correctly. Firstly, ##l_j^N## is the Lagrange
I hope this helps.
Indeed it does. Thank you.
I only have one question left. Since I am working with a random variable, what shoud I use as f(x)? if I used the expected value, I would get a zero, since I am dealing with a uniform random variable.
 
  • #6
I have to admit that I'm very far from an expert on this. But, I believe you can use a probability distribution of the form
$$f(x)=\begin{cases} 1/(b-a) & a\leqslant x \leqslant b \\
0 & x<a \text{ or } x>b\end{cases}$$

Probably, it is also useful for you to read the Wikipedia page https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)
 
  • Like
Likes confused_engineer
  • #7
eys_physics said:
I have to admit that I'm very far from an expert on this. But, I believe you can use a probability distribution of the form
$$f(x)=\begin{cases} 1/(b-a) & a\leqslant x \leqslant b \\
0 & x<a \text{ or } x>b\end{cases}$$

Probably, it is also useful for you to read the Wikipedia page https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)
Don't worry. You have already helped me a lot.
Thanks
 

1. What is the purpose of weights and interpolation in scientific calculations?

Weights and interpolation are used to estimate values between known data points. This is particularly useful in situations where the data is not continuous or evenly spaced, and a smooth curve needs to be created to make accurate predictions or calculations.

2. How do weights and interpolation work together?

Weights are used to assign values to data points based on their proximity to the point being estimated. These weights are then used in the interpolation process, where a mathematical function is used to estimate the value between the data points based on the assigned weights.

3. Can weights and interpolation be applied to any type of data?

Yes, weights and interpolation can be applied to any type of data, including numerical, categorical, or even spatial data. The key is to have enough data points to accurately estimate the values between them.

4. How accurate are the results obtained from weights and interpolation?

The accuracy of the results obtained from weights and interpolation depends on the quality and quantity of the data points, as well as the chosen interpolation method. In general, the results can be quite accurate, but it is always recommended to validate the results with additional data or techniques.

5. Can weights and interpolation be replicated with hand calculations?

Yes, weights and interpolation can be replicated with hand calculations, although it may be more time-consuming and prone to errors compared to using programming languages or specialized software. However, for small datasets, hand calculations can be a useful tool for understanding the underlying principles of weights and interpolation.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
2
Views
900
  • Programming and Computer Science
Replies
3
Views
931
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
21
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top