Using Vectors in NetLogo - netlogo

How can you define add or subtract vectors in NetLogo. It doesn't seem to have any datatype pertaining to it.
By vectors I here am talking in terms specifically velocity of a turtle.
Are there any extensions in netlogo that support this, I can't find any.

I don't know of an extension that provides vectors. But the math involved to code it in NetLogo itself is generally not that complicated. So for example suppose you choose to represent a two-dimensional vector as a list of two numbers. Then:
to-report vector-add [v1 v2]
report (list (first v1 + first v2) (last v1 + last v2))
end
observer> show vector-add [0.1 0.2] [0.5 0.3]
observer: [0.6 0.5]
vector-add can also be written as:
to-report vector-add [v1 v2]
report (map + v1 v2)
end
which works on vectors of any dimension.

Related

Implementing Objective function with intlinprog

I have a Matlab program that outputs some binary variables based on some constraints.
As an example with three n=3 bits, {x_1 x_2 x_3, x_4 x_5 x_6, x_7 x_8 x_9} my program will output all these bits based on the constraints.
At this point, I have no objective function.
However, the goal of the objective function is to minimize the total Hamming distance (HD) between some of the n bits pairs.
Say I want to minimize HD (x_1 x_2 x_3 vs x_4 x_5 x_6) + (x_1 x_2 x_3 vs x_7 x_8 x_9)
Needless to say, n can vary as can the number of pairs compared for HD.
How do I perform this with intlinprog? I am not finding this helpful. A little bit of direction would do. Do I need to change my A,b,Aeq,etc?
There may be better implementations for this type of objective, but I like to go back to basics to understand what is going on. A simple approach is to think about what the Hamming distance means for a pair of binary variables. The Hamming distance will be zero if the variables have the same value (0 and 0, or 1 and 1), and the Hamming distance will be 1 if the variables don't have the same value.
Assume you have two binary variables v1 and v2. Create another variable Z and add constraints:
Z >= V1 - V2
Z >= V2 - V1
Now Z must be greater than or equal to the Hamming distance between V1 and V2. So if we minimise Z we will minimise the Hamming distance. The generalisation to multiple pairs of variables is obvious - create a variable like Z for all pairs of variables like V1 and V2, and then minimise the sum of those Z variables.

Is there a way to code a multivariate normal distribution in NetLogo

I want to sample from a multivariate normal distribution for a model in netlogo. I know there's a random-normal function but I'd like to extend this to two dimensions.
Or am I better off coding it up in R and feeding it to the netlogo model that way?
Okay, it's been a long time since I had to do something like this. But you want something like (with rho as your correlation that you control):
set dim1 random-normal 1 0.2
set dim2 rho * dim1 + sqrt(1 - rho ^ 2) * random-normal 5 1
You might want to check on crossvalidated (the statistics equivalent to stackoverflow) to get the correct formula. But it's definitely straightforward to do in NetLogo once you know how to adjust a normal random number to introduce the dependency. Using the R extension is always fiddly and it's not worth the effort if this is all you are doing with it.

Need alternative to randsample for probability vectors with 0s in MATLAB

I have a question similar to this one:
Weighted random numbers in MATLAB
At this point, I use randsample in my program as follows:
T = [0 .5 .5; .5 0 .5; .5 .5 0]
choices = 1:3
for i = 1:3
t(i) = transpose(randsample(choices,1,true,T(i,:)));
end
Thus t(i) describes for each person which neighbor they will select.
My T matrix, when read rowwise describes the probability of a person to select their neighbor. For example, the first row says the 1st person will select node 2 or 3 with 50% probability each. They cannot select his own node 1. As I scale up my model, they will always have equal probability of selecting a neighbor, 1/number of neighbors. I have hardcoded the T matrix here for brevity's sake.
I tried to use histc as suggested in the linked topic, but since there is always a 0 in each row of my T matrix, I don't think the cumulative sum accurately sets up bins for rows with a 0 in the middle (here the second row of T).
I also tried to use bsxfun, and I was able to get sensical output when looking at each row of my T matrix individually, but I failed to put it in a loop. I feel the solution may be here, but I'm not sure I fully understand how this function is operating in my context.
So, does anybody have any ideas of how I can speed up my randsample function? At the moment, I iterate it 10000x, and so it is a major bottleneck in my program. It works as I need it to, it's just far too slow.
So you want, for each person, to pick a neighbour among all other persons, with uniform probability.
I would do it this way. It should be pretty fast.
n = 7; %// number of persons
t = ceil((n-1)*rand(1,n)); %// step 1
t = t + (t>=(1:n)); %// step 2
For each k, the generated t(k) is randomly picked from the set {1, 2, ..., k-1, k+1, ..., n} with uniform distribution. To achieve this, two steps are used:
A random value is picked from {1, ..., n-1};
If this value is greater than or equal to k it is incremented by 1.

Merging multiple probability arrays in a Cartesian type of way in Matlab

I have a couple vectors where each entity denotes a probability value. For example, consider the following to vectors:
a=[0.7 0.3]
b=[0.1 0.9]
Consider a and b as vectors showing probability. For example, a denotes that a random variable will be 0 with probability 0.7, and it will be 1 with probability 0.3. Similarly, b represents another random variable that will be 0 with probability 0.1, and it will be 1 with probability 0.9
I want to compute a vector c where c captures the probability mass function of the sum of a and b by considering a and b are independent. In this example, c should be
c=[0.07 0.66 0.27]
In other words, c=0 when both a=0 and b=0 this happens with probability 0.7*0.1=0.07. c=1 when either a=0 and b=1 or a=1 and b=0. The first occurs with probability 0.7*0.9=0.63 and the second occurs with probability 0.3*0.1=0.03, so the sum is 0.63+0.03=0.66. Finally, the third entry of c corresponds to the case in which both a and b is equal to 1 with probability 0.3*0.9=0.27.
I want to write a code to compute c. In my application, there will be 30 of these a vectors with a length of 100 each. So, scalability definitely matters.
Many thanks!
For your simple example, you can use conv
c=conv(a,b)
however for your actual case it will be more complicated. You could repeatedly conv vectors like so (if your a vectors are rows of A)
A=a(1,:);
for i=2:30
A=conv(A,a(i,:));
end
(Note: This code works but I am not sure whether it will give you the correct results---this is not a topic I know much about, so be careful!)

Creating a new probabilistic matrix from two existing ones according to prespecified rules in MATLAB

I have a problem in my MATLAB code. Let me first give you some explanation about the issue. I have two matrices which represent probabilities of specific outcomes of events. The first one is called DemandProbabilityMatrix or in short DemandP. Entry (i,j) shows the probability that item i is demanded j many times. Similarly, we have a ReturnProbabilityMatrix, i.e. ReturnP. An element of type (i,j) stores the probability that item i is returned j many times.
We want to compute the net demand probability out of these two matrices. For an example:
DemandP=[ .4 .5 .1]
ReturnP=[ .2 .3 .5]
In this case we have 1 item and it can be demanded or returned either 1,2 or 3 times with the given probabilities. To be more specific That item will be demanded just for once with probability .4 .
Then we need to compute the net demand. In this case, net demand can be -2,-1,0,1 or 2. For instance in order to get a net demand of -1 we can either have a demand of 1 and return of 2 or demand of 2 and return of 3. Thus we have
NetDemandP(1,2)= DemandP(1,1)*ReturnP(1,2)+DemandP(1,2)*ReturnP(1,3).
Thus the NetDemandP should look as:
NetDemandP=[.20 .37 .28 .13 .02]
I can do this with nested for loops but I'm trying to come up with a faster way. In case it helps I have the following for loops solutions where I denotes the number of rows in ReturnP and DemandP, J+1 denotes the number of columns in those matrices.
NetDemandP=zeros(I,2*J+1);
for i=1:I
for j=1:J+1
for k=1:J+1
NetDemandP(i,j-k+J+1)=NetDemandP(i,j-k+J+1)+DemandP(i,j)*ReturnP(i,k);
end
end
end
Thanks in advance
What you want is the convolution of your probability density functions. Or, more specifically, you want the convolution of the demand density with the reverse of the return density. This is easily achieved in Matlab. For example:
DemandP = [.4 .5 .1];
ReturnP = [.2 .3 .5];
NetDemandP = conv(DemandP,fliplr(ReturnP))
If you have matrices instead of vectors, then just iterate through the rows:
for i = 1:size(DemandP,1)
NetDemandP(i,:) = conv(DemandP(i,:),fliplr(ReturnP(i,:)))
end