Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. using Distributions
  2. using Statistics
  3.  
  4. function monotone_approx(N::Int64,n_trials::Int64,n::Int64)
  5.  
  6. """
  7. inputs:
  8. N: the range of U([-N,N])
  9. n_trials: the number of times we generate random vectors
  10. n: the 'dimension' of the random vector
  11.  
  12. outputs:
  13. Q: the quality of the approximation for ten values
  14. monotone relation: a logical verification that the monotone relation holds
  15.  
  16. """
  17.  
  18. X = rand(-1*N:N,n_trials,n);
  19.  
  20. Y = sum(X,dims=(2));
  21.  
  22. ## comparison with binomial:
  23. Q = zeros(10);
  24.  
  25. D, D_hat = zeros(10), zeros(10);
  26.  
  27. for k =1:10
  28.  
  29. ## delta is approximately N/2
  30. delta = N/2
  31.  
  32. ## an approximation of n_hat which is 'n' in the n choose k formula
  33. n_hat = Int(floor((2*N/(2*N+1))*n));
  34.  
  35. ## the fraction of S_n whose value is greater than 2*Delta*k:
  36. D[k] = mean(Y.>2*delta*k);
  37.  
  38. ## k in the n choose k formula:
  39. D_hat[k] = Int(floor((2*k + n_hat)/2));
  40.  
  41. ## a CDF that should have a monotone relationship with W
  42. ## this may be considered an approximation
  43. Z = 1-cdf(Binomial(n_hat),D_hat[k]);
  44.  
  45. ## checking the quality of the approximation
  46. Q[k] = min(D[k],D_hat[k])/max(D[k],D_hat[k])
  47.  
  48. end
  49.  
  50. return Q, minimum((D[2:10].-D[1:9]).*(D_hat[2:10].-D_hat[1:9])) > 0
  51.  
  52. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement