I have a rankingMat which contains the rankings of equity tickers, where every column represents one ticker and every row represents a point in time. The ranking has been performed in a descending manner, i.e. a 1 in rankingMat identifies the highest rank for that time period (read: row). Ticker/column 4 represents a cash security. This will be important further down the road.
Now, I would like to identify in which equity tickers I am supposed to invest. There exists two conditions:
I only invest in tickers that have a rank less or equal to 3.
In addition, I only invest in tickers that are ranked higher or equal compared to cash (ticker/column 4).
I'm able to get a result that fulfills the first condition:
rankingMat = ...
[NaN, NaN, NaN, NaN, NaN, NaN; ...
1, 5, 2, 3, 6, 4; ...
4, 5, 2, 3, 6, 1; ...
4, 1, 2, 5, 6, 3; ...
6, 4, 5, 2, 1, 3; ...
2, 3, 4, 6, 1, 5; ...
3, 6, 4, 1, 2, 5; ...
2, 5, 6, 1, 4, 3];
portfolio = rankingMat <= 3;
The result looks like this:
portfolio = ...
[0, 0, 0, 0, 0, 0; ...
1, 0, 1, 1, 0, 0; ...
0, 0, 1, 1, 0, 1; ...
0, 1, 1, 0, 0, 1; ...
0, 0, 0, 1, 1, 1; ...
1, 1, 0, 0, 1, 0; ...
1, 0, 0, 1, 1, 0; ...
1, 0, 0, 1, 0, 1]
My problem is condition 2. In every row, I need to compare not only if the integer is less or equal to 3, I also need to make sure that it is less than the integer in column 4 in that particular row. I am looking for a solution that avoids a for-loop. I guess it is possible with indexing. So, any hint is highly appreciated.
The final result should look like this:
portfolio = ...
[0, 0, 0, 0, 0, 0; ...
1, 0, 1, 1, 0, 0; ...
0, 0, 1, 1, 0, 1; ...
0, 1, 1, 0, 0, 1; ...
0, 0, 0, 1, 1, 0; ...
1, 1, 0, 0, 1, 0; ...
0, 0, 0, 1, 0, 0; ...
0, 0, 0, 1, 0, 0]
% Prior to R2016b
portfolio = rankingMat <= 3 & ...
bsxfun(#lt, rankingMat, rankingMat(:,4));
% On or after R2016b
portfolio = rankingMat <= 3 & ...
rankingMat < rankingMat(:,4);
Related
I have a set of samples. These samples are partitioned based on their values into several clusters (3 clusters).
df = pd.DataFrame({'samples': ['A', 'B', 'C', 'D', 'E'],
'values': [[5, 0, 2, 2],[1, 6, 0, 2],[7, 2, 0, 0],[3, 6, 0, 0],[7, 0, 0, 2]],
'cluster': [1, 0, 2, 0, 1]})
df
output:
samples values cluster
0 A [5, 0, 2, 2] 1
1 B [1, 6, 0, 2] 0
2 C [7, 2, 0, 0] 2
3 D [3, 6, 0, 0] 0
4 E [7, 0, 0, 2] 1
Assuming after a round, the value of sample (B) has changed from [1, 6, 0, 2] to [7, 1, 0, 2]
How could I measure whether sample (B) still belongs to its own cluster (0) or should move to other clusters?
samples values cluster
0 A [5, 0, 2, 2] 1
1 B [7, 1, 0, 2] ?
2 C [7, 2, 0, 0] 2
3 D [3, 6, 0, 0] 0
4 E [7, 0, 0, 2] 1
I could use silhouette index, but I looking for different methods such as math formulas, or other techniques.
I've got this segment of code in a discriminator network for MNIST:
nn.Conv2d(1, 64, 4, 2, 1),
From my understanding, there is 1 input channel (the MNIST image), then we apply a 4x4 kernel to the image in strides of 2 to produce 64 feature maps. Does this mean that we actually have 64 kernels at this layer? Because in order to get 64 different feature maps, we would need 64 separate kernels to convolve over the image?
Then after some ReLu, we have another convolution:
nn.Conv2d(64, 128, 4, 2, 1),
How do we get from 64 to 128? From my understanding of the first example, we have 64 seperate kernels that can produce 64 seperate feature maps. But here we go from 64 feature maps to 128 feature maps? Does that mean that we only have two kernels?
I hope someone can shine some light on whether my understanding is correct!
Your understanding in the first example is correct, you have 64 different kernels to produce 64 different feature maps.
In case of the second example, so the number of input channels not beeing one, you still have as "many" kernels as the number of output feature maps (so 128), which each are trained on a linear combination of the input feature maps. So in your case each of these kernels would have 4x4x64 trainable weights.
All the input channels are connected to each output channel (if group = 1, as by default) by convolution with filters (kernels) -- one for each output channel. Each kernel though has sub-kernels for each input channel.
So in the first layer you have in_channels = 1 and out_channels = 64 meaning that there are 64 kernels (and sub-kernels). In the second layer you have in_channels = 64 and out_channels = 128 meaning that there are 128 kernels each having 64 * 128 sub-kernels.
Here's a simple example of one conv layer taken from cs231n for clarification:
And my implementation in Pytorch:
import torch
from torch import nn
cnn = nn.Conv2d(in_channels=3, out_channels=2, kernel_size=3,
stride=2, padding=1, bias=True, groups=1)
w0 = torch.FloatTensor([[[-1, -1, 0],
[ 1, 1, 1],
[ 1, 1, 0]],
[[ 1, 1, -1],
[ 0, 0, 0],
[ 1, 1, -1]],
[[ 0, -1, 0],
[-1, 0, -1],
[ 1, 0, 1]]])
b0 = torch.FloatTensor([1])
w1 = torch.FloatTensor([[[-1, 0, 0],
[ 1, 1, 1],
[-1, -1, 0]],
[[ 1, -1, -1],
[-1, 1, -1],
[ 1, -1, 0]],
[[ 1, -1, 0],
[ 0, 1, 1],
[ 1, 0, 1]]])
b1 = torch.FloatTensor([0])
cnn.weight = torch.nn.Parameter(torch.stack((w0, w1), 0))
cnn.bias = torch.nn.Parameter(torch.cat((b0, b1), 0))
inpt = torch.FloatTensor([[[ 1, 2, 0, 1, 2],
[ 1, 0, 2, 2, 0],
[ 2, 0, 0, 2, 2],
[ 0, 0, 2, 2, 0],
[ 2, 2, 2, 1, 2]],
[[ 2, 0, 0, 1, 1],
[ 1, 0, 2, 1, 2],
[ 2, 0, 2, 2, 1],
[ 0, 2, 0, 0, 1],
[ 1, 2, 1, 2, 0]],
[[ 0, 0, 2, 1, 2],
[ 0, 1, 0, 2, 0],
[ 1, 1, 0, 0, 2],
[ 0, 0, 0, 1, 1],
[ 0, 1, 2, 0, 2]]])
cnn(inpt.unsqueeze(0))
Output:
tensor([[[[ 7., 9., 10.],
[ 0., 6., 10.],
[ 2., 5., 2.]],
[[ 4., 4., 4.],
[ 5., 1., 2.],
[ 2., 6., 0.]]]])
So the formula that needs to be implemented is:
P = ((1 - delta)/n) + ((delta)*(A)ij / (Sigma(k=1 to n)(A)ik))
where delta = 0.85
n = 8
and A = the adjacency matrix for web pages being surfed
The mathematica code for it is:
A = {{1, 1, 1, 0, 0, 1, 0, 1}, {0, 0, 1, 0, 0, 1, 0, 1}, {1, 1, 0, 1,
0, 1, 1, 0}, {0, 1, 1, 0, 1, 0, 1, 0}, {1, 1, 1, 1, 0, 1, 1,
1}, {1, 1, 1, 0, 0, 1, 1, 0}, {1, 0, 1, 0, 1, 0, 1, 0}, {0, 0, 0,
0, 1, 0, 0, 1}};
n = 8;
\[Delta] = 0.85;
P = Transpose[Table[(1 - \[Delta])/n + (\[Delta]*A[[i, j]])/(Sum[A[[i, k]], {k, 1, n}]), {i, 1, n}, {j, 1, n}]];
Everything else is just plugging in numbers.
Now the main problem I seem to have is getting the A[[i,j]]/Sum[A[i,k]] to work on matlab.
On matlab: when I input A[[i,j]] as A, and sum[A[i,k]] as either (sum(A,2))' or sum(A,1), the P that gets output on matlab becomes a column vector rather than an 8 x 8 matrix.
What am I missing?
There are many ways of doing it. I'll show you one way of using native vectorized MATLAB code, so no need for for-loops or arrayfun or anything like that.
A = [1, 1, 1, 0, 0, 1, 0, 1; 0, 0, 1, 0, 0, 1, 0, 1; 1, 1, 0, 1, 0, 1, 1, 0; 0, 1, 1, 0, 1, 0, 1, 0; 1, 1, 1, 1, 0, 1, 1, 1; 1, 1, 1, 0, 0, 1, 1, 0; 1, 0, 1, 0, 1, 0, 1, 0; 0, 0, 0, 0, 1, 0, 0, 1];
n = size(A, 1);
delta = 0.85;
% First, sum up all columns in each row
rowSums = sum(A,2);
% Then replicate them because the entries in each row of the vector are valid for every column in that specific row. So replicate them so that the output-matrix matches the size of A so we can work element-wise division later
rowSums2 = repmat(rowSums, 1, n);
% Use vectorized code, ./ will yield element-wise division as A and rowSums2 are of the same size now
P = (1 - delta)/n + delta*A ./ rowSums2
I hope I got the desired output right.
I was trying to solve a matrix with parameters in matlab. So i defined a and b with syms a b and then my matrix D = [-1 -2 2 1 -3; 1 1 a 1 2; 2 1 -1 b 3; 1 1 -1 1 2; -1 1 a -1-b 0]
D =
[ -1, -2, 2, 1, -3]
[ 1, 1, a, 1, 2]
[ 2, 1, -1, b, 3]
[ 1, 1, -1, 1, 2]
[ -1, 1, a, - b - 1, 0]
When i do rref(D) it gives me the following:
>> rref(D)
ans =
[ 1, 0, 0, 0, 1]
[ 0, 1, 0, 0, 1]
[ 0, 0, 1, 0, 0]
[ 0, 0, 0, 1, 0]
[ 0, 0, 0, 0, 0]
This actually useless for me, because i need the system solved with the resulting parameters in it.
Anyone an idea? Thanks in advance!
well, it is pretty obvious that both a and b can be eliminated using rref, since a appears twice only in the 2nd col, and b appears twice only in the 4th col. For example, (row2+row3)/(row5+row1) and bingo, no a and b ...
1) I'm wondering why the Facebook Graph gives empty results for the following objects:
(Please keep in mind I have a token that has the correct permissions, moreover, extended permissions to access these objects).
tagged - https://graph.facebook.com/me/tagged or /{userid}/tagged
likes
activities
accounts
which produces:
{"data": []}
2) Did Facebook strip our permissions from accessing our own data?
3) Which objects cannot be accessed?
4) Here are my extended /permissions
<pre>
{
"data": [
{
"installed": 1,
"status_update": 1,
"publish_checkins": 1,
"photo_upload": 1,
"video_upload": 1,
"sms": 1,
"email": 1,
"create_event": 1,
"create_note": 1,
"export_stream": 1,
"share_item": 1,
"rsvp_event": 1,
"read_stream": 1,
"publish_stream": 1,
"read_mailbox": 1,
"ads_management": 1,
"read_friendlists": 1,
"manage_friendlists": 1,
"xmpp_login": 1,
"read_insights": 1,
"read_requests": 1,
"manage_notifications": 1,
"manage_pages": 1,
"publish_actions": 1,
"user_birthday": 1,
"user_religion_politics": 1,
"user_relationships": 1,
"user_relationship_details": 1,
"user_hometown": 1,
"user_location": 1,
"user_likes": 1,
"user_activities": 1,
"user_interests": 1,
"user_education_history": 1,
"user_work_history": 1,
"user_online_presence": 1,
"user_website": 1,
"user_groups": 1,
"user_events": 1,
"user_photos": 1,
"user_videos": 1,
"user_photo_video_tags": 1,
"user_notes": 1,
"user_checkins": 1,
"user_questions": 1,
"user_about_me": 1,
"user_status": 1,
"user_games_activity": 1,
"user_subscriptions": 1,
"friends_birthday": 1,
"friends_religion_politics": 1,
"friends_relationships": 1,
"friends_relationship_details": 1,
"friends_hometown": 1,
"friends_location": 1,
"friends_likes": 1,
"friends_activities": 1,
"friends_interests": 1,
"friends_education_history": 1,
"friends_work_history": 1,
"friends_online_presence": 1,
"friends_website": 1,
"friends_groups": 1,
"friends_events": 1,
"friends_photos": 1,
"friends_videos": 1,
"friends_photo_video_tags": 1,
"friends_notes": 1,
"friends_checkins": 1,
"friends_questions": 1,
"friends_about_me": 1,
"friends_status": 1,
"friends_games_activity": 1,
"friends_subscriptions": 1
}
]
}
</pre>
I've checked them, I don't see any tags (but not sure if I have been tagged in the last few days).
The other ones work perfectly for me when tested in the https://developers.facebook.com/tools/explorer. So are you sure your account has likes/activities and more then one account? Since I only see my 'pages' of which I am also owner.