# Generating random quantity from a vector of quantities

**URL:** <https://discourse.julialang.org/t/generating-random-quantity-from-a-vector-of-quantities/23718>\
**Category:** Finance and Economics\
**Tags:** question\
**Created:** [April 30, 2019, 10:21pm UTC](https://discourse.julialang.org/t/generating-random-quantity-from-a-vector-of-quantities/23718 "2019-04-30T22:21:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arega\_Getaneh](https://avatars.discourse-cdn.com/v4/letter/a/7bcc69/32.png) [@Arega\_Getaneh](https://discourse.julialang.org/u/Arega_Getaneh)\
**Post date:** [April 30, 2019, 10:21pm UTC](https://discourse.julialang.org/t/generating-random-quantity-from-a-vector-of-quantities/23718/1 "2019-04-30T22:21:06Z")

</div>

Q\_mean\_vec=[0 3000 3500 4000 4500 5000 5500 6000 6500 7000 7500 8000]  
qmean=0  
for Q\_v=Q\_mean\_vec  
qmean=qmean+1  
Q\_mean=Q\_v  
Q\_std=0.2;  
Q\_sigma=Q\_std.\*Q\_v  
dQ = MvNormal(Q\_v,Q\_sigma)  
Q=rand(dQ,W);

end  
I wanted Q to be generated automaticlay from Q\_mean\_vec. But it doesnt work as I wanted. Any one who can help me?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 1, 2019, 12:29am UTC](https://discourse.julialang.org/t/generating-random-quantity-from-a-vector-of-quantities/23718/2 "2019-05-01T00:29:26Z")

</div>

What is `W` in the last line?

---

<div class="post-metadata">

**Author:** ![Arega\_Getaneh](https://avatars.discourse-cdn.com/v4/letter/a/7bcc69/32.png) [@Arega\_Getaneh](https://discourse.julialang.org/u/Arega_Getaneh)\
**Post date:** [May 1, 2019, 1:04am UTC](https://discourse.julialang.org/t/generating-random-quantity-from-a-vector-of-quantities/23718/3 "2019-05-01T01:04:06Z")

</div>

#Just to give a hint about my data, this are required for this one  
I=3; #conventional units  
J=1; #stochastic units  
W=20; #number of scenarios

##### RAMDOM GENERATION

Q\_mean=[5000]#this this one scalar, I wanted to try a vector of Renewable generations in the last code where I have trouble  
Q\_std=0.2;  
Q\_sigma=Q\_std.\*Q\_mean  
dQ = MvNormal(Q\_mean,Q\_sigma)  
Q=rand(dQ,W); #random renewable generation  
phi=0  
#-------------------  
risk\_vec=[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1]  
#here are long ecpressions  
cont=0  
for risk\_v=risk\_vec  
cont=cont+1  
phi=risk\_v  
#expression  
end  
#This one works properly  
Q\_mean\_vec=[0 3000 3500 4000 4500 5000 5500 6000 6500 7000 7500 10000]  
#long expression  
for Q\_v=Q\_mean\_vec  
qmean=qmean+1  
Q\_mean=Q\_v  
Q\_std=0.2;  
Q\_sigma=Q\_std.\*Q\_v  
dQ = MvNormal(Q\_v,Q\_sigma)  
Q=rand(dQ,W);  
#long expression  
end  
#it doesnt work. So here is what I need help.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 1, 2019, 2:02am UTC](https://discourse.julialang.org/t/generating-random-quantity-from-a-vector-of-quantities/23718/4 "2019-05-01T02:02:06Z")

</div>

I have made your code a little easier to see.  
(please read: [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530) and [Style Guide · JuMP](http://www.juliaopt.org/JuMP.jl/v0.19.0/style/) )

```julia
using Distributions, Random
#Just to give a hint about my data, this are required for this one
I = 3 #conventional units
J = 1 #stochastic units
W = 20 #number of scenarios

# RAMDOM GENERATION
# this one scalar, I wanted to try a vector of Renewable generations in the last code where I have trouble

Q_mean = [5000]
Q_std = 0.2
Q_sigma = Q_std .* Q_mean
dQ = MvNormal(Q_mean,Q_sigma)
Q = rand(dQ,W); #random renewable generation
phi = 0

risk_vec = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1]

cont=0
for risk_v in risk_vec
   global cont
   cont = cont+1
   phi = risk_v
end

# This one works properly (it does not -- see `qmean` comment)

Q_mean_vec = [0 3000 3500 4000 4500 5000 5500 6000 6500 7000 7500 10000]

# qmean = ????
for Q_v in Q_mean_vec
    global qmean
    qmean = qmean + 1
    Q_mean = Q_v
    Q_std = 0.2
    Q_sigma = Q_std .* Q_v
    dQ = MvNormal(Q_v, Q_sigma)
    Q = rand(dQ,W)
end

```

`qmean` is undefined  
you should define functions and call them rather than making everything cascade into itself  
(it will help you work faster and more cleanly and help others see where issues may arise)

Where there are `#long expressions` in your code that are not of import in determining how to help you, just omit them without comment – this is part of what is called making a Minimal Working Example [MWE}, or in this case a Minimal NonWorking Example and that helps.  
[put yourself in our shoes – and make it as simple as can be while keeping the essential]

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 1, 2019, 3:11am UTC](https://discourse.julialang.org/t/generating-random-quantity-from-a-vector-of-quantities/23718/5 "2019-05-01T03:11:27Z")

</div>

Please reduce your question to the simplest possible, giving example input in the form of working code with all quantities defined, and the expected output that you would like.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [May 1, 2019, 3:24am UTC](https://discourse.julialang.org/t/generating-random-quantity-from-a-vector-of-quantities/23718/6 "2019-05-01T03:24:20Z")

</div>

> [@JeffreySarnoff](#):
>
> # qmean = ???

i suppose that qmean = 0, looking at the code style

---

<div class="post-metadata">

**Author:** ![Arega\_Getaneh](https://avatars.discourse-cdn.com/v4/letter/a/7bcc69/32.png) [@Arega\_Getaneh](https://discourse.julialang.org/u/Arega_Getaneh)\
**Post date:** [May 1, 2019, 8:54am UTC](https://discourse.julialang.org/t/generating-random-quantity-from-a-vector-of-quantities/23718/9 "2019-05-01T08:54:04Z")

</div>

**Thank you for your replies and help.**  
_The code looks like as I put below. I have anather section for all the data, I used for the simulation. This is just where I have problem and need help. But in the way you told me could not generate what I want. If it had worked, the result for Q must be `12*20` where12 (size of the vector) times 20 (number of scenarios). However the code gives me `20*10000 or 0*20`. That means the loop takes the first or the last element from the vector and considers it as the row or columon of Q. which was not the intention. That was my problem priorly and came here to seek help. Hope you got me right._

```julia

Q_mean_vec=[0 3000 3500 4000 4500 5000 5500 6000 6500 7000 7500 10000] 
delta_i=comp *ones(I); 
delta_j=comp* ones(J);
 varphi_i=comp *ones(I); 
varphi_j=comp* ones(J);
 chi_i=0 *ones(I);
 chi_j=0* ones(J); 
mm=size(Q_mean_vec,2)

obj_gen=zeros(mm);cvar_i_gen=zeros(mm,I);cvar_j_gen=zeros(mm,J);Pi_i_gen=zeros(mm,I,W);Pi_j_gen=zeros(mm,J,W);EPi_i_gen=zeros(mm,I);EPi_j_gen=zeros(mm,J);qF_i_gen=zeros(mm,I);qF_j_gen=zeros(mm,J);qS_i_gen=zeros(mm,I,W);qS_j_gen=zeros(mm,J,W);PF_gen=zeros(mm);PS_gen=zeros(mm,W)

obj_CFD =zeros(mm);cvar_i_CFD =zeros(mm,I);cvar_j_CFD =zeros(mm,J);Pi_i_CFD =zeros(mm,I,W);Pi_j_CFD =zeros(mm,J,W);EPi_i_CFD =zeros(mm,I);EPi_j_CFD =zeros(mm,J);qF_i_CFD =zeros(mm,I);qF_j_CFD =zeros(mm,J);qS_i_CFD =zeros(mm,I,W);qS_j_CFD =zeros(mm,J,W);PF_CFD =zeros(mm);PS_CFD =zeros(mm,W)

obj_two=zeros(mm);cvar_i_two=zeros(mm,I);cvar_j_two=zeros(mm,J);Pi_i_two=zeros(mm,I,W);Pi_j_two=zeros(mm,J,W);EPi_i_two=zeros(mm,I);EPi_j_two=zeros(mm,J);qF_i_two=zeros(mm,I);qF_j_two=zeros(mm,J);qS_i_two=zeros(mm,I,W);qS_j_two=zeros(mm,J,W);PS_two=zeros(mm,W)

Pi_i_only=zeros(mm,I,W);Pi_j_only=zeros(mm,J,W);EPi_i_only=zeros(mm,I);EPi_j_only=zeros(mm,J);qS_i_only=zeros(mm,I,W);qS_j_only=zeros(mm,J,W);PS_only=zeros(mm,W) qmean = 0 for Q_v in Q_mean_vec global qmean qmean = qmean + 1 Q_mean = Q_v Q_std = 0.2 Q_sigma = Q_std .* Q_v dQ = MvNormal(Q_v, Q_sigma) Q = rand(dQ,W)

qmean = 0
for Q_v in Q_mean_vec
    global qmean
    qmean = qmean + 1
    Q_mean = Q_v
    Q_std = 0.2
    Q_sigma = Q_std .* Q_v
    dQ = MvNormal(Q_v, Q_sigma)
    Q = rand(dQ,W)

(obj_gen[qmean],cvar_i_gen[qmean,:],cvar_j_gen[qmean,:],Pi_i_gen[qmean,:,:],Pi_j_gen[qmean,:,:],EPi_i_gen[qmean,:],EPi_j_gen[qmean,:],qF_i_gen[qmean,:],qF_j_gen[qmean,:],qS_i_gen[qmean,:,:],qS_j_gen[qmean,:,:],PF_gen[qmean],PS_gen[qmean,:],status)=general_equi_NLP(I,J,W,a,b,c,Q,gamma_S,gamma_F,beta_S,beta_F,qmax_F_i,qmin_F_i,qmax_F_j,qmin_F_j,phi,M,M_dual,alpha,sigma_i,sigma_j,delta_i,varphi_i,varphi_j,chi_i,chi_j)
println(qmean,"->",status," OF -> ",obj_gen[qmean])  
(obj_CFD[qmean],cvar_i_CFD[qmean,:],cvar_j_CFD[qmean,:],Pi_i_CFD[qmean,:,:],Pi_j_CFD[qmean,:,:],EPi_i_CFD[qmean,:],EPi_j_CFD[qmean,:],qF_i_CFD[qmean,:],qF_j_CFD[qmean,:],qS_i_CFD[qmean,:,:],qS_j_CFD[qmean,:,:],PF_CFD[qmean],PS_CFD[qmean,:],status)=CFD_equi_NLP(I,J,W,a,b,c,Q,gamma_S,gamma_F,beta_S,beta_F,qmax_F_i,qmin_F_i,qmax_F_j,qmin_F_j,phi,M,M_dual,alpha,sigma_i,sigma_j,delta_i,varphi_i,varphi_j,chi_i,chi_j)
println(status," OF -> ",obj_CFD[qmean])   
(obj_two[qmean],cvar_i_two[qmean,:],cvar_j_two[qmean,:],Pi_i_two[qmean,:,:],Pi_j_two[qmean,:,:],EPi_i_two[qmean,:],EPi_j_two[qmean,:],qF_i_two[qmean,:],qF_j_two[qmean,:],qS_i_two[qmean,:,:],qS_j_two[qmean,:,:],PS_two[qmean,:],status)=Two_part_equi_NLP(I,J,W,a,b,c,Q,gamma_S,gamma_F,beta_S,beta_F,qmax_F_i,qmin_F_i,qmax_F_j,qmin_F_j,phi,M,M_dual,alpha,sigma_i,sigma_j,delta_i,varphi_i,varphi_j,chi_i,chi_j)   
println(status," OF -> ",obj_two[qmean])   
(PS_only[qmean,:], qS_i_only[qmean,:,:],qS_j_only[qmean,:,:], Pi_i_only[qmean,:,:],Pi_j_only[qmean,:,:], EPi_i_only[qmean,:],EPi_j_only[qmean,:])=Only_spot(I,J,W,a,b,c,Q,gamma_S,beta_S,delta_i)
end
if comp==0 name_comp = string(“Cournot_results_RES.jld”) else name_comp = string(“Competitive_results_RES.jld”) end @save name_comp obj_two cvar_i_two cvar_j_two Pi_i_two Pi_j_two EPi_i_two EPi_j_two qF_i_two qF_j_two qS_i_two qS_j_two obj_CFD cvar_i_CFD cvar_j_CFD Pi_i_CFD Pi_j_CFD EPi_i_CFD EPi_j_CFD qF_i_CFD qF_j_CFD qS_i_CFD qS_j_CFD PF_CFD PS_CFD obj_gen cvar_i_gen cvar_j_gen Pi_i_gen Pi_j_gen EPi_i_gen EPi_j_gen qF_i_gen qF_j_gen qS_i_gen qS_j_gen PF_gen PS_gen PS_only qS_i_only qS_j_only Pi_i_only Pi_j_only EPi_i_only EPi_j_only

#=As it could be seen from above, qmean is the first element in the vector and it supposed automated when it is qmean=qmean+1=#
#=Just try the code below and you will see if it genarates what it has to generate. It doesnt give sense what it gives Q is not 
10000×20 Array{Float64,2}:
1519.28 2985.87 476.075 … 2715.85 4134.3 -908.621  
  1371.42 1237.13 2605.16 952.586 2282.29 1354.05   
 -3531.76 1627.48 -54.9649 -2912.9 -1878.2 -2421.25   
   302.855 -3741.64 -83.5187 -789.506 206.86 -861.804  
   475.353 -2665.26 -1811.17 -389.738 2239.35 42.435  
   964.401 -1328.83 -697.692 … -220.086 -865.61 587.97   
 -1136.12 -1045.77 -3353.4 1617.66 -357.055 984.174  
 -1248.5 2433.42 2209.21 -1573.54 660.237 83.8777 
  1847.68 -1186.83 -1134.5 -2994.51 -1236.82 2070.42   
   839.699 -1225.06 -2685.57 -2629.12 -430.576 -3.99456
  -513.981 1998.83 1987.8 … 885.013 885.386 183.42   
 -2261.29 893.061 3334.27 -3.60624 1603.16 -2551.26   
  3378.73 1438.13 -3998.07 194.097 -297.471 2009.85   
     ⋮ ⋱                                     
  4044.37 3291.43 -3031.23 -6046.27 -2567.44 -2112.24   
   600.755 -851.435 -1562.54 -2525.1 -1603.94 2008.06   
 -1765.07 -700.273 3132.21 … -911.996 206.341 -2068.15   
 -4254.92 1426.3 702.223 896.844 -1956.92 -1968.51   
  -343.506 1288.68 -1973.32 3135.77 -1183.38 -4035.13   
  -523.642 -997.373 2418.8 -2207.67 -2146.25 -364.775  
  -453.447 1372.88 2182.59 2392.01 -1552.5 114.411  
  1824.95 -348.986 344.384 … 1328.87 -793.081 2134.66   
 -1483.69 -408.258 870.426 -2124.35 -2255.04 -1059.69   
 -2014.03 -606.51 -896.236 -478.832 -939.712 107.686  
  3412.07 1322.66 -1491.08 302.769 227.449 3991.34   
 -2477.5 -576.577 -118.4 -462.442 -632.303 -4565.68 =#
Q_mean_vec=[0 3000 3500 4000 4500 5000 5500 6000 6500 7000 7500 10000]
qmean = 0
for Q_v in Q_mean_vec
qmean = 0
global qmean
qmean = qmean + 1
Q_mean = Q_v
Q_std = 0.2
Q_sigma = Q_std .* Q_v
dQ = MvNormal(Q_v, Q_sigma)
Q = rand(dQ,W)
    end
Q
#Therefore, the error message in the main code is as follows
BoundsError: attempt to access 0×20 Array{Float64,2} at index [1, 1]

Stacktrace:
 [1] macro expansion at C:\Users\Utente\.julia\v0.6\JuMP\src\parsenlp.jl:249 [inlined]
 [2] macro expansion at C:\Users\Utente\.julia\v0.6\JuMP\src\macros.jl:1352 [inlined]
 [3] general_equi_NLP(::Int64, ::Int64, ::Int64, ::Array{Float64,2}, ::Array{Float64,2}, ::Array{Float64,2}, ::Array{Float64,2}, ::Array{Float64,1}, ::Int64, ::Array{Float64,1}, ::Float64, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Float64, ::Int64, ::Int64, ::Float64, ::Array{Float64,2}, ::Array{Float64,2}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}) at .\In[169]:70
 [4] macro expansion at .\In[348]:32 [inlined]
 [5] anonymous at .\<missing>:?
 [6] include_string(::String, ::String) at .\loading.jl:522
```
