1

I am creating a piecewise linear approximation for the following equation:

$$W = \frac{\theta \left(k \rho\right)^{k}}{2 k^{2}k! \rho \left( 1- \rho\right)^{2} \left(\frac{\left(k \rho\right)^{k}}{\left(1- \rho\right) k!} + \sum_{n=0}^{k - 1} \frac{\left(k \rho\right)^{n}}{n!}\right)}$$

as $W_{approx}=A+B\rho+C\theta+Dk$. In this equation, $\theta$, $\rho$, and $k$ are the variables, where $\theta,\rho\in \mathbb{R}^+$, $k\in \mathbb{Z}^{>0}$, and $0\leq\rho<1$. To do that multivariate linear regression, I want to minimize the norm: $$F = \int_a^b\int_c^d\int_e^f\left(A+B\rho+C\theta+Dk-W \right)^2$$ where $a\leq \rho\leq b$, $c\leq \theta \leq d$, and $e\leq k \leq f$. After integration, I need to find $\frac{\partial F}{\partial A}$,$\frac{\partial F}{\partial B}$, $\frac{\partial F}{\partial C}$, and $\frac{\partial F}{\partial D}$, set them equal to $0$, and solve for $A$, $B$, $C$, and $D$. I learned all the process in this post thanks to Claude Leibovici.

For a single instance of the piecewise approximation, let $a=0$, $b=0.1$, $c=0$, $d=10$, $e=1$, $f=3$, I used sympy module in Python to code the whole process as following:

from sympy import *
init_printing(True)
A, B, C, D = symbols('A B C D', real=True)
theta, rho = symbols('theta rho', real=True)
k = symbols('k', integer=True)
a = 0; b = 0.1; c = 0; d = 10; e=1; f=3
n = symbols('n', integer= True)
W = (theta*(k*rho)**k / (2*factorial(k)*k**2*rho*(1-rho)**2
            *(Sum((k*rho)**n/factorial(n), (n,0,k-1)) 
              + (k*rho)**k/(factorial(k)*(1-rho))))).doit()
F = (A + B*rho+C*theta+D*k - W)**2
eq1 = diff(integrate(F, (rho, a, b), (theta, c, d), (k, e, f)), A)
eq2 = diff(integrate(F, (rho, a, b), (theta, c, d), (k, e, f)), B)
eq3 = diff(integrate(F, (rho, a, b), (theta, c, d), (k, e, f)), C)
eq4 = diff(integrate(F, (rho, a, b), (theta, c, d), (k, e, f)), D)
sol = solve([simplify(eq1),simplify(eq2),simplify(eq3), simplify(eq4)], [A,B,C,D])

If I could successfully find $A$,$B$,$C$,$D$ values, I was going to use the following to find an approximated value for the given:

rho = 0.05; theta = 2, k = 2
Wapprox = sol[A] + sol[B]*rho + sol[C]*theta + sol[D]*k

I realized that the issue resides in eq1 = diff(integrate(F, (rho, a, b), (theta, c, d), (k, e, f)), A), the multi-integration process. I ran the code more than an hour and could not get any results. I also tried eq1 = diff(integrate(simplify(F), (rho, a, b), (theta, c, d), (k, e, f)), A) as simplifying the feed function worked for a similar problem, but not this time. If you have any comments how to resolve the issue, I would really appreciate it.

tcokyasar
  • 147
  • Is there a particular reason, why you don't want to use numerical methods? (Or would you also be interested in a numerical approximation of the solution.) – Steffen Plunder Jul 26 '18 at 16:41
  • @SteffenPlunder What do you mean by "a numerical approximation". Do you mean, Newton's method? If yes, a solution with respect to a single point is not accurate enough for my optimization problem. The above equation is a only a set of linear equality constraints that will go into my MILP problem. So, I really need a "range-based" approach with a high accuracy of approximation. If you know a better way, I would be happy to learn. – tcokyasar Jul 26 '18 at 16:45
  • 1
    I'm not sure if my numerical idea is good or not (Gaussian quadrature to evaluate F and then solving the least square problem). Anyway: The integral is not trivially well defined for k~∈[e,f], since in order to evaluate W at non-integer values, the finite sum and the factorial need some extra care. In your code you decided to use ⌊k~⌋ as upper limit for the sum. But I'm not sure about the factorial, would you like to replace it by the gamma-function or instead by ⌊k~⌋! ? – Steffen Plunder Jul 26 '18 at 18:19
  • @SteffenPlunder, Gaussian quadrature is new for me. I have no idea how to start with it. If you may write your solution, we can see the deviation of the approximation. If the error rate is small, I can use it. Currently, the only problem does not seem to be $k!$ with $k$ being non-integer. I tried to come up with an approximation based only on $\rho$ and $\theta$ and keeping $k$ as a constant. It worked well for $k=1$ and $k=2$. But, it cannot integrate eq1 when $k=3$, which means I should really consider another way of integrating. Practically, a solution with $k<10$ works for me. – tcokyasar Jul 26 '18 at 18:27

1 Answers1

1

Disclaimer: I'm not sure if you really should use a linear approximation in your application!

  1. Especially the dependency with respect the integer $k$ is not well approximated by a linear function.
  2. If you want to get a piece-wise linear approximation, you may want the pieces to match at their boundaries, but this is more difficult.

Anyway, here is a numerical method to find a solution for one piece. Since the approximation of $w$ by a linear function already introduces a big approximation error, I would argue, that numerical methods are faster and more stable in your case than symbolic tools. The advantage is, that we can compute the difficult integral using quadrature rules (which are simple are good for smooth functions) instead of using quite tricky symbolic algorithms.

Numerical method for fixed integer $k$:

To use the same notation as in my source code, I will denote $W$ by $h(\rho, \theta, k)$.

Using Gaussian quadratue, we can approximate $$F \approx \sum_{i=1}^n \sum_{j=1}^m w_i^{(\rho)} w_j^{(\theta)} ( c_1 \rho_i + c_2 \theta_j + c_3 k - h(\rho_i, \theta_j,k))^2.$$

Here $\rho_i$ denotes the quadratue points and $w_i^{(\rho)}$ denotes the associated weights for the Gauss-Legendre quadrature of a chosen degree, for example $\text{deg}=10$.

Now with some time and a piece of paper, it is possible to figure out matrices $W, A, H$ (see below) such that

$$ \sum_i \sum_j w_i^{(\rho)} w_j^{(\theta)} ( c_1 \rho_i + c_2 \theta_j + c_3 k - h(\rho_i, \theta_j,k))^2 = ||W^{\frac 1 2} A \mathbf c - W^{\frac 1 2} H||_2^2.$$ Hence $\min F$ is equal to the least-square problem $$\min || W^{\frac 1 2} A - W^{\frac 1 2} H ||.$$

The solution of this problem this is known to be $$\mathbf c = (B^T B)^{-1}B^TH, \quad \text{ with } B = W^{\frac 1 2} A.$$

Results

From my point of view, the results look great for each fixed $k$. This plot represents the solution for $k=4$. In my cases the convergence with respect to the chosen quadrature degree was very quick.

enter image description here

Extensions to variable $k$

You now could also apply the same approach with varying $k$, but the solutions do not behave well, therefore I would suggest to find coefficients for each individual $k$.

Source code

Quick-and-Dirty implementation

The matrices

Roughly $W^{\frac 1 2} \in \mathbb R^{n\cdot m \times n \cdot m}$ contains $\sqrt{w_i^{(\rho)} w_j^{(\theta)}}$ on the diagonal, $A \in \mathbb R^{n\cdot m \times 3}$ contains the quadrature points, i.e. all combinations of $(\rho_i, \theta_j)$ and $H \in \mathbb R^{n\cdot m \times 3}$ contains the values of $h$ at these points. Then each entry of $W^{\frac 1 2} A \mathbf c - W^{\frac 1 2} H$ represents one component of the double sum, i.e. $\sqrt{w_i^{(\rho)} w_j^{(\theta)}} ( c_1 \rho_i + c_2 \theta_j + c_3 k - h(\rho_i, \theta_j,k))$.

Steffen Plunder
  • 1,033
  • 9
  • 12
  • Firstly, thank you very much for such a well explained answer. It is going to take time for me to digest them all. Using code, I saw that condensing the $a$-$b$ range yields a better approximation while $\theta$ does not play much role. Again, it will take time for me to understand, but, can you at least explain what "degree" really means in there? Also, after running the code, can I find an approximation within the ranges for specified values of $\rho$ and $\theta$ with a fixed $k$ by h(rho,theta,k) in Python? If yes, the approximations are perfect. – tcokyasar Jul 26 '18 at 20:55
  • Also, I loved the name of your notebook, "strange_function_approx". It is really strange. It is an approximate formulation of queue waiting time in M/G/k queueing theory. And yes, you have contributed to something good, if I can really understand them all and use it in my paper.I really appreciate a lot. – tcokyasar Jul 26 '18 at 20:58
  • I just noticed h(rho,theta,k) refers to the original $W$. You used float(coefs[j][0])*x + float(coefs[j][1])*y + float(coefs[j][2])*k to find the approximate value.Is there an easy way of finding the approximation for specified values of $\rho$, $\theta$, and $k$? I guess, you also considered k=0 by having that condition, right? Sorry for asking a lot of question! – tcokyasar Jul 26 '18 at 21:08
  • 1
    I'm glad that it might help. About the questions: 1) Since $\theta$ enters the function linearly, it will be always the same. 2) The quadrature degree has the following meaning: If you evaluate a integral $\int_a^b p(x) dx$ with a quadrature rule of degree $d$, it will be exact for all polynomials $p(x)$ of polynomial degree $\leq 2d+1$. (That's the big surprise in each course on numerical integration.) Since your function is not a polynomial, you only can reply on error estimates, for example Wikipedia: Error Estimates – Steffen Plunder Jul 26 '18 at 21:22
  • 1
  • I updated the file, in the second and third cell, you now find a simpler implementation, which allows a simple evaluation of $W_{\text{approx}}$, which is called h_approx. It should work well for vectorized inputs $\rho$ and $\theta$, but k must be an element of k_range. You can extend k_range of cause. The case $k=0$ is not well defined, since $W$ is not well defined for $k=0$ (division by zero)
  • – Steffen Plunder Jul 26 '18 at 22:03
  • I have two questions: 1) Why did you use coef = np.linalg.solve( A.T*A, A.T*H) to find the coefficients instead of your definition: $c = (B^T B)^{-1}B^TH, \quad \text{ with } B = W^{\frac 1 2} A$. Do they mean the same? 2) The value of degree does not seem to change the approximation. But, making it larger slightly makes the computation more time-consuming. Is $10$ a safe value? – tcokyasar Jul 27 '18 at 03:32
  • 1
    In general, the Gaussian Quadrature approximations seem to be worse than $F=\int_a^b\int_c^d(A+B\rho+C\theta-(W|k={1,2,...,K}))^2$. Yet, the symbolic calculation does not yield a result for $k>=3$ for a long time. Thus, it becomes useless. If we could find a solution for that approximation, it could be better.I understood how Gaussian works from here and understood your code better thanks to here. Once again, thank you very much. – tcokyasar Jul 27 '18 at 03:45
  • No worries about not using this solution. If you get the symbolic tools to work, it would be great! ad 1) Yes the notations differ, in the code I did A = W A, with W $= W^{\frac 1 2}$. Sorry. ad 2) I'm not sure. Actually I would even guess, that a degree like $1$ or $2$ is sufficient. (The solution is just the projection of $h$ onto the space of linear polynomials w.r.t. the $L^2$-norm. In fact you can find the coefficients also as $c_1 = <p_1(\rho), h >_{L^2}$, etc. where $p_1$ is a linear polynomial, which is zero at $\frac{a+b}{2}$. Which is exactly the same like your derivatives.) – Steffen Plunder Jul 27 '18 at 08:37
  • Sure, feel free to use it in any way. – Steffen Plunder Jul 27 '18 at 19:22
  • Sorry to bother you about this question again. But, I just wondered why you used $i$ and $j$ as indices, instead of using only one index. I understand the sizes of the indices could have been different. But, as we used $deg=10$ for both, couldn't we just use only $i$ and say $i={1,2,...,10}$? – tcokyasar Jul 31 '18 at 00:30
  • The domain of the original integral is two dimensional $\theta, \rho$. If we take $n$ evaluation points in the $\theta$ direction and also $n$ in the $\rho$ direction, we still get $n\cdot n$ evaluation points ${ (\theta_1,\rho_1), (\theta_1, \rho_2), (\theta_1, \rho_3), \dots, (\theta_n, \rho_n) }$. If we only use one index $i$, this would imply that we only evaluate at the diagonal of the domain, but instead we want to evaluate on a grid like set of points. (Two dimensional quadrature: Link ) – Steffen Plunder Jul 31 '18 at 07:59
  • I just misinterpreted the situation. It says: "usually $M = N$ (so $\eta_i = \xi_i, \tilde{w}_i = w_i$)". Then it denotes: $\sum_i^N \sum_j^N w_i w_j g(\xi_i, \xi_j)$ This makes sense. So, we have one set (with the range value) and two indices to matriculate the points. Thanks for the document. It is really helpful. – tcokyasar Jul 31 '18 at 14:01