5

The renewal function for Weibull distribution m(t) with t = 10 is given as below.enter image description here

I want to find the value of m(t). I wrote the following r code to compute m(t)

last_term = NULL
gamma_k = NULL
n = 50
for(k in 1:n){
  gamma_k[k] = gamma(2*k + 1)/factorial(k)
}

for(j in 1: (n-1)){
  prev = gamma_k[n-j]
  last_term[j] = gamma(2*j + 1)/factorial(j)*prev
}

final_term = NULL
find_value = function(n){
  for(i in 2:n){
  final_term[i] = gamma_k[i] - sum(last_term[1:(i-1)])
  }
  return(final_term)
}
all_k = find_value(n)

af_sum = NULL
m_t = function(t){
for(k in 1:n){
af_sum[k] = (-1)^(k-1) * all_k[k] * t^(2*k)/gamma(2*k + 1)
}
  return(sum(na.omit(af_sum)))
}
m_t(20)

The output is m(t) = 2.670408e+93. Does my iteratvie procedure correct? Thanks.

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
score324
  • 687
  • 10
  • 18
  • My advice is to decompose the problem into pieces and look at each piece. First try to get A[k] computed correctly. Try to compute a few terms, A[1], A[2], A[3] etc, by hand, so you know what to expect. Then try to just get your program to compute A[k] to match the expected result. Then do something similar with the main summation: figure out the first few terms by hand, using what you know from A[k], and then get the program to match that. By the way, if the terms in the summation do not decrease very quickly, the summation may be very inaccurate for any small number of terms. – Robert Dodier Aug 07 '20 at 16:40
  • @RobertDodier terms would be huge, I believe. I posted an answer but it won't converge quickly – Severin Pappadeux Aug 08 '20 at 02:07
  • @SeverinPappadeux, Thank you for the input! – score324 Aug 08 '20 at 03:00
  • @score324 well, if you like it, you could bump it – Severin Pappadeux Aug 08 '20 at 03:04
  • @score324 Do you just need to calculate the renewal function, so it would be OK to use a library for that? Or is it required that you implement it yourself? Thanks for the information. – Robert Dodier Aug 08 '20 at 16:34
  • @RobertDodier, this is an approximate renewal function for the Weibull distribution. Please see this journal article https://www.jstor.org/stable/1266342?seq=1#metadata_info_tab_contents, on page 394, equation (4), (8), and (9). Thanks – score324 Aug 08 '20 at 17:32
  • Thanks for the reference. Some comments. (1) I see that m(t) as you have specified it is actually for t = 10 and m = 2 where m is the Weibull shape parameter. Probably it would help others to just leave t and m in the equation. (2) The summation gets harder as t and m increase. I don't know what typical values are. You might try to identify typical values and say that those are to help others understand. (3) Something to try at this point is an asymptotic approximation in t^m. (4) Are you sure nobody has implemented the function in software? Maybe ask on a statistics-oriented forum. – Robert Dodier Aug 09 '20 at 19:24
  • By the way, m(t) over the range 0 to 4 increases only a little faster than linearly -- m(4) is approximately 6.13 if I've implemented the summation correctly. – Robert Dodier Aug 09 '20 at 19:49
  • @RobertDodier, thanks, yes exactly. – score324 Aug 09 '20 at 21:35

2 Answers2

2

I don't think it will work. First, lets move Γ(2k+1) from denominator of m(t) into Ak. Thus, Ak will behave roughly as 1/k!.

In the nominator of the m(t) terms there is t2k, so roughly speaking you're computing sum with terms

100k/k!

From Stirling formula

k! ~ kk, making terms

(100/k)k

so yes, they will start to decrease and converge to something but after 100th term

Anyway, here is the code, you could try to improve it, but it breaks at k~70

N <- 20
A <- rep(0, N)

# compute A_k/gamma(2k+1) terms
ps <- 0.0 # previous sum
A[1] = 1.0
for(k in 2:N) {
    ps <- ps + A[k-1]*gamma(2*(k-1) + 1)/factorial(k-1)
    A[k] <- 1.0/factorial(k) - ps/gamma(2*k+1)
}

print(A)

t <- 10.0
t2 <- t*t

r <- 0.0
for(k in 1:N){
    r <- r + (-t2)^k*A[k]
}

print(-r)

UPDATE

Ok, I calculated Ak as in your question, got the same answer. I want to estimate terms Ak/Γ(2k+1) from m(t), I believe it will be pretty much dominated by 1/k! term. To do that I made another array k!*Ak/Γ(2k+1), and it should be close to one.

Code

N <- 20
A <- rep(0.0, N)

psum <- function( pA, k ) {
    ps <- 0.0
    if (k >= 2) {
        jmax <- k - 1
        for(j in 1:jmax) {
            ps <- ps + (gamma(2*j+1)/factorial(j))*pA[k-j]
        }
    }
    ps
}

# compute A_k/gamma(2k+1) terms
A[1] = gamma(3)
for(k in 2:N) {
    A[k] <- gamma(2*k+1)/factorial(k) - psum(A, k)
}

print(A)

B <- rep(0.0, N)
for(k in 1:N) {
    B[k] <- (A[k]/gamma(2*k+1))*factorial(k)
}

print(B)

shows that

  1. I got the same Ak values as you did.
  2. Bk is indeed very close to 1

It means that term Ak/Γ(2k+1) could be replaced by 1/k! to get quick estimate of what we might get (with replacement)

m(t) ~= - Sum(k=1, k=Infinity) (-1)k (t2)k / k! = 1 - Sum(k=0, k=Infinity) (-t2)k / k!

This is actually well-known sum and it is equal to exp() with negative argument (well, you have to add term for k=0)

m(t) ~= 1 - exp(-t2)

Conclusions

  1. Approximate value is positive. Probably will stay positive after all, Ak/Γ(2k+1) is a bit different from 1/k!.

  2. We're talking about 1 - exp(-100), which is 1-3.72*10-44! And we're trying to compute it precisely summing and subtracting values on the order of 10100 or even higher. Even with MPFR I don't think this is possible.

Another approach is needed

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
  • The ``m(t)`` is always positive. The renewal function cannot be negative. – score324 Aug 08 '20 at 13:10
  • Is there an easy way to specify that arithmetic should be arbitrary precision? I guess there are at least two kinds that might be relevant -- one would be arbitrary-precision integers and the other would be arbitrary-precision floats. I don't know what's possible in R. – Robert Dodier Aug 08 '20 at 16:56
  • @RobertDodier that would be my proposal - there is [Rmpfr](https://cran.r-project.org/web/packages/Rmpfr/index.html) package, with gamma etc. Might worth a try – Severin Pappadeux Aug 08 '20 at 17:07
  • @score324 Sure, I know that. But answer is oscillating between positive and negative, so if for N<-10 it is huge negative, for N<-11 it is huge positive and os on and so forth. I see two ways to go forward - one as proposed by Robert, brute force using arbitrary precision math (MPFR based), another one to use some clever math to accelerate convergence, f.e. something like [this](https://www.johndcook.com/blog/2020/08/06/cohen-acceleration/) – Severin Pappadeux Aug 08 '20 at 17:11
  • I'm trying an adaptation of your program in Maxima which allows arbitrary-precision integers (I've made all the values integers or rationals instead of floats). For varying N from 20 to 200, I get values for the summation which are enormous and negative. Although A[k] appears to go to zero pretty quickly, the term 10^(2 k) inflates everything. It's not clear to me whether direct summation can work for any number of terms. Maybe OP should look at asymptotic approximations or something. – Robert Dodier Aug 08 '20 at 17:14
  • Yeah, I think the next thing to try is to rearrange the summation in some way. Also, exact or arbitrary-precision arithmetic wouldn't hurt, under the circumstances. – Robert Dodier Aug 08 '20 at 17:15
  • @SeverinPappadeux, thank you very much. I computed a few values by hand. Under the same setting the first four `A` values are `all_k = c(A1, A2, A3, A4) = c( 2, 8, 80, 1184)`. – score324 Aug 08 '20 at 17:21
  • @RobertDodier I updated the answer, would appreciate your comments – Severin Pappadeux Aug 08 '20 at 19:52
  • #@score324 Put up an update, would appreciate if you could check it – Severin Pappadeux Aug 08 '20 at 19:52
  • I don't think m(t) ~= 1 - exp(-t^2) could be right. That suggests m(t) is bounded above by 1, but I don't think m(t) as stated by OP is like that, instead, it looks like it is unbounded as t increases. For what it's worth, applying the summation for values of t <= 4 seems to show the slope of m(t) is increasing over that range. Note that the formula shown by OP is actually for the special case m = 2 where m is the Weibull shape parameter (the paper linked by OP shows this). At this point I think OP needs to look for an asymptotic approximation in t^m, if indeed t = 10 and m = 2 is relevant. – Robert Dodier Aug 09 '20 at 19:42
  • @SeverinPappadeux, thank you so much. You made my day! – score324 Aug 09 '20 at 22:25
  • @RobertDodier, this approximation convergence is rapidly for all t, if 0 < \beta ≤ 1. If \beta >1 Constantine and Robinson proposed an infinite series approximation. Here is the link to that paper. https://www.sciencedirect.com/science/article/abs/pii/S0167947396000527 – score324 Aug 09 '20 at 22:53
  • 1
    @RobertDodier You're probably right, it might be off by quite a lot. But still not possible to compute by running formulas from OP question, I think. `At this point I think OP needs to look for an asymptotic approximation in t^m, if indeed t = 10 and m = 2 is relevant` Yes, I agree, say, series over (1/t)^mk would be nice to have for very large argument. – Severin Pappadeux Aug 10 '20 at 05:15
  • @score324 That sounds like an interesting paper, but it's not generally accessible. It would help others help you to say what the relevant formula is. My advice is to give it a try, and open a new question for it. – Robert Dodier Aug 10 '20 at 05:54
  • 1
    @SeverinPappadeux Another approach which seems plausible is to look at the integral equation satisfied by the renewal function, m(t) = F(t) + integral(m(t - s) f(s), s from 0 to t), where f and F are the probability density and cumulative density, respectively. I think it could be approximated in the conventional way, by discretizing it into a system of linear equations. – Robert Dodier Aug 10 '20 at 06:13
  • @RobertDodier. Exactly `m(t) = F(t) + integrate (m(t-s))f(s)ds` `s` from `0 to 1`. In this case I considered as `F(t) = 1 - exp(-t^beta)`. – score324 Aug 10 '20 at 13:42
  • @SeverinPappadeux I've appended an answer which explores a discretization of the integral equation. If you have any comment I would be interested to hear it. – Robert Dodier Aug 14 '20 at 06:37
2

OK, so I ended up going down a pretty different road on this. I have implemented a simple discretization of the integral equation which defines the renewal function:

m(t) = F(t) + integrate (m(t - s)*f(s), s, 0, t)

The integral is approximated with the rectangle rule. Approximating the integral for different values of t gives a system of linear equations. I wrote a function to generate the equations and extract a matrix of coefficients from it. After looking at some examples, I guessed a rule to define the coefficients directly and used that to generate solutions for some examples. In particular I tried shape = 2, t = 10, as in OP's example, with step = 0.1 (so 101 equations).

I found that the result agrees pretty well with an approximate result which I found in a paper (Baxter et al., cited in the code). Since the renewal function is the expected number of events, for large t it is approximately equal to t/mu where mu is the mean time between events; this is a handy way to know if we're anywhere in the neighborhood.

I was working with Maxima (http://maxima.sourceforge.net), which is not efficient for numerical stuff, but which makes it very easy to experiment with different aspects. At this point it would be straightforward to port the final, numerical stuff to another language such as Python.

Thanks to OP for suggesting the problem, and S. Pappadeux for insightful discussions. Here is the plot I got comparing the discretized approximation (red) with the approximation for large t (blue). Trying some examples with different step sizes, I saw that the values tend to increase a little as step size gets smaller, so I think the red line is probably a little low, and the blue line might be more nearly correct.

Compare approximations for renewal function

Here is my Maxima code:

/* discretize weibull renewal function and formulate system of linear equations
 * copyright 2020 by Robert Dodier
 * I release this work under terms of the GNU General Public License
 *
 * This is a program for Maxima, a computer algebra system.
 * http://maxima.sourceforge.net/
 */

"Definition of the renewal function m(t):" $

renewal_eq: m(t) = F(t) + 'integrate (m(t - s)*f(s), s, 0, t);

"Approximate integral equation with rectangle rule:" $

discretize_renewal (delta_t, k) :=
  if equal(k, 0)
    then m(0) = F(0)
    else m(k*delta_t) =   F(k*delta_t)
                        + m(k*delta_t)*f(0)*(delta_t / 2)
                        + sum (m((k - j)*delta_t)*f(j*delta_t)*delta_t, j, 1, k - 1)
                        + m(0)*f(k*delta_t)*(delta_t / 2);

make_eqs (n, delta_t) :=
  makelist (discretize_renewal (delta_t, k), k, 0, n);

make_vars (n, delta_t) :=
  makelist (m(k*delta_t), k, 0, n);

"Discretized integral equation and variables for n = 4, delta_t = 1/2:" $

make_eqs (4, 1/2);
make_vars (4, 1/2);

make_eqs_vars (n, delta_t) :=
  [make_eqs (n, delta_t), make_vars (n, delta_t)];

load (distrib);
subst_pdf_cdf (shape, scale, e) :=
  subst ([f = lambda ([x], pdf_weibull (x, shape, scale)), F = lambda ([x], cdf_weibull (x, shape, scale))], e);

matrix_from (eqs, vars) :=
 (augcoefmatrix (eqs, vars),
  [submatrix (%%, length(%%) + 1), - col (%%, length(%%) + 1)]);

"Subsitute Weibull pdf and cdf for shape = 2 into discretized equation:" $

apply (matrix_from, make_eqs_vars (4, 1/2));
subst_pdf_cdf (2, 1, %);

"Just the  right-hand side matrix:" $

rhs_matrix_from (eqs, vars) :=
 (map (rhs, eqs),
  augcoefmatrix (%%, vars),
  [submatrix (%%, length(%%) + 1), col (%%, length(%%) + 1)]);

"Generate the right-hand side matrix, instead of extracting it from equations:" $

generate_rhs_matrix (n, delta_t) :=
  [delta_t * genmatrix (lambda ([i, j], if i = 1 and j = 1 then 0
                                        elseif j > i then 0
                                        elseif j = i then f(0)/2
                                        elseif j = 1 then f(delta_t*(i - 1))/2
                                        else f(delta_t*(i - j))), n + 1, n + 1),
   transpose (makelist (F(k*delta_t), k, 0, n))];

"Generate numerical right-hand side matrix, skipping over formulas:" $

generate_rhs_matrix_numerical (shape, scale, n, delta_t) :=
  block ([f, F, numer: true], local (f, F),
         f: lambda ([x], pdf_weibull (x, shape, scale)),
         F: lambda ([x], cdf_weibull (x, shape, scale)),
         [genmatrix (lambda ([i, j], delta_t * if i = 1 and j = 1 then 0
                                               elseif j > i then 0
                                               elseif j = i then f(0)/2
                                               elseif j = 1 then f(delta_t*(i - 1))/2
                                               else f(delta_t*(i - j))), n + 1, n + 1),
          transpose (makelist (F(k*delta_t), k, 0, n))]);

"Solve approximate integral equation (shape = 3, t = 1) via LU decomposition:" $

fpprintprec: 4 $
n: 20 $
t: 1;
[AA, bb]: generate_rhs_matrix_numerical (3, 1, n, t/n);
xx_by_lu: linsolve_by_lu (ident(n + 1) - AA, bb, floatfield);

"Iterative solution of approximate integral equation (shape = 3, t = 1):" $

xx: bb;
for i thru 10 do xx: AA . xx + bb;
xx - (AA.xx + bb);
xx_iterative: xx;

"Should find iterative and LU give same result:" $

xx_diff: xx_iterative - xx_by_lu[1];
sqrt (transpose(xx_diff) . xx_diff);

"Try shape = 2, t = 10:" $

n: 100 $
t: 10 $
[AA, bb]: generate_rhs_matrix_numerical (2, 1, n, t/n);
xx_by_lu: linsolve_by_lu (ident(n + 1) - AA, bb, floatfield);

"Baxter, et al., Eq. 3 (for large values of t) compared to discretization:" $
/* L.A. Baxter, E.M. Scheuer, D.J. McConalogue, W.R. Blischke.
 * "On the Tabulation of the Renewal Function,"
 * Econometrics, vol. 24, no. 2 (May 1982).
 * H(t) is their notation for the renewal function.
 */
H(t) := t/mu + sigma^2/(2*mu^2) - 1/2;

tx_points: makelist ([float (k/n*t), xx_by_lu[1][k, 1]], k, 1, n);

plot2d ([H(u), [discrete, tx_points]], [u, 0, t]), mu = mean_weibull(2, 1), sigma = std_weibull(2, 1);
Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
  • thank you so much. I am unable to install maxima 5.44.0 software. I am getting an error saying `C:\maxima-5.44.0\clisp-2.49\ANNOUNCE`. – score324 Aug 15 '20 at 04:02
  • It's hard to tell what's the problem. My advice is to post a message to the Maxima mailing list. Please see: https://sourceforge.net/projects/maxima/lists/maxima-discuss – Robert Dodier Aug 15 '20 at 20:22