Generating random quantity from a vector of quantities

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?

What is W in the last line?

#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.

I have made your code a little easier to see.
(please read: PSA: how to quote code with backticks and Style Guide · JuMP )

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]

2 Likes

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.

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

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.


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