How do I set initial value of a scan to be a list? - kdb

I'm trying to do:
(.5 .5) (+\) (.9 .2;.4 .1)
Expected result:
1.4 0.7
1.8 0.8
But instead I get 'type.
I cannot seem to use a list as the leftmost argument. What am I doing wrong?

q)(+\)[0.5 0.5;(.9 .2;.4 .1)]
1.4 0.7
1.8 0.8

Your expression works fine without the parens:
q).5 .5 +\ (.9 .2;.4 .1)
1.4 0.7
1.8 0.8
As you see, you can use a list as left argument. But scalar extension means you need not.
q).5 +\ (.9 .2;.4 .1)
1.4 0.7
1.8 0.8
Explanation
You want cumulative sums from your list:
q)sums (.9 .2;.4 .1)
0.9 0.2
1.3 0.3
And then some:
q).5 + sums (.9 .2;.4 .1)
1.4 0.7
1.8 0.8
But you have seen a little deeper into this. sums is syntactic sugar for the derived function Add Scan +\. The derived function is variadic – can be applied as either a unary or a binary – and sums is only for unary application. When Add Scan is applied as a binary its left argument is an initial value.
q).5 +\ (.9 .2;.4 .1)
1.4 0.7
1.8 0.8
When the right argument is a long list, it is more efficient to specify an initial value than to add it afterwards to each of the cumulative sums.
q)show L:1000000?1.
0.3927524 0.5170911 0.5159796 0.4066642 0.1780839 0.3017723 0.785033 0.5347096 0.711171..
q)\ts:1000 .5+sums L
2360 16777472
q)\ts:1000 .5+\ L
1477 8388880
Those parens
As mentioned, the derived function +\ is variadic.
q)5+\1 2 3 / +\ applied as a binary
6 8 11
To apply it as a unary, use either bracket notation +\[1 2 3] or ‘capture’ it in parens. The immediate effect is to prevent its application. You now have a data item. It has ‘noun syntax’ and can be passed as an argument.
q)type(+\)
108h
But q syntax allows you to apply a noun by juxtaposition.
For noun N, N x is equivalent to N#x or N[x].
q)"abc" 2 0
"ca"
q)til 3
0 1 2
q)(+\)1 2 3 / +\ applied as a unary
1 3 6

Related

What's the easiest way of making a histogram in KDB?

If I've got a list of values x, what's the easiest way to make a histogram with bin-size b?
It seems like I could hack a complicated solution, but I feel like there must be a built-in function for this somewhere that I don't know about.
I haven't heard about built-in histogram so far. But I would approach this task like below.
For fixed bucket size:
a: 0.39 0.51 0.51 0.4 0.17 0.3 0.78 0.53 0.71 0.41;
b: 0.1;
{count each group x xbar y}[b;a]
// returns 0.3 0.5 0.4 0.1 0.7!2 3 2 1 2j
For "floating" buckets:
a: 0.39 0.51 0.51 0.4 0.17 0.3 0.78 0.53 0.71 0.41;
b: -1 0.5 0.7 1;
{count each group x#x bin y}[b;a]
// returns -1 0.5 0.7!5 3 2j
Above functions return dictionary with bucket starts as keys and number of bucket occurrences as values.
Assuming you have a list of x values (let's assume x = 1000):
v:1000?1.0;
You can achieve what you need as follows:
b:0.1;
hist:(count') group xbar[b;v];
There are two points:
the keys in hist are not sorted
For the bucket, do you prefer to output the left or the right delimiter?
To solve for 1), you simply do:
hist:(asc key hist)#hist;
To solve for 2) - I mean, if you want to have the right delimiter:
hist:(+[b;key hist])!value hist;

Error: math operation produced a non-number

I am getting the following error when I run my code:
"math operation produced a non-number"
I know that it may happen when in the formula a ^ b a is negative and b is fractional.
Mine are
(1 + (1 / a)*(1 - b)^(1 / a))
(1 + a)*(1 - b)^(a))
where a can vary from 0.1 to 1 and b may be defined as
1) a random-normal 0.1 0.1 (a random value between 0 and 0.2), or 2) random-normal 0.8 0.2 (a random value between 0.6 and 1).
However, I cannot understand what I am doing wrong. The formula that I use is correct. Can it be the random value that I am computing?

Neural network - exercise

I am currently learning for myself the concept of neural networks and I am working with the very good pdf from
http://neuralnetworksanddeeplearning.com/chap1.html
There are also few exercises I did, but there is one exercise I really dont understand, at least one step
Task:
There is a way of determining the bitwise representation of a digit by adding an extra layer to the three-layer network above. The extra layer converts the output from the previous layer into a binary representation, as illustrated in the figure below. Find a set of weights and biases for the new output layer. Assume that the first 3 layers of neurons are such that the correct output in the third layer (i.e., the old output layer) has activation at least 0.99, and incorrect outputs have activation less than 0.01.
I found also the solution, as can be seen on the second image
I understand why the matrix has to have this shape, but I really struggle to understand the step, where the user calculates
0.99 + 3*0.01
4*0.01
I really don't understand these two steps. I would be very happy if someone can help me understand this calculation
Thank you very much for help
Output of previous layer is 10x1(x). Weight matrix is 4x10. New output layer will be 4x1. There are two assumption first:
x is 1 only at one row. xT= [1 0 0 0 0 0 0 0 0 0]. If you multiple this vector with matrix W your output will be yT=[0 0 0 0], because there is only 1 in x. After multiplication by W will be this only 1 multiple by 0th column of W which are zeroes.
Second assumption is, what if x is not 1 anymore, instead of one x can be xT=[0.99 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01]. And if you perform multiplication of x with first row of W result is 0.05(I believe here is typo). When xT=[0.01 0.99 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01] after multiplication with first row of W result is 1.03. Because:
0.01*0 + 0.99*1 + 0.01*0 + 0.01*1 + 0.01*0 + 0.01*1 + 0.01*0 + 0.01*1 + 0.01*0 + 0.01*1 = 1.03
So I believe there is a typo, because author probably assume 4 ones at first row of W, which is not true, because there is 5 ones. Because if there was 4 ones at first first row, than really results will be 0.04 for 0.99 at first row of x and 1.02 for 0.99 at second row of x.

How to select whole numbers in a row and its adjacent numbers in Matlab

Good day!
I would like to select the whole numbers in my random data, at the same time it will also choose the adjacent numbers.
For example, I have this raw data
A = [0.1 0.2
0.2 0.1
1 0.3
0.3 0.2
0.4 0.4
2 0.5]
so would like to select the (1, 0.3) and (2, 0.5). then my final ouptut will be,
B= [1 0.3
2 0.4]
Thanks in advance!
You can use modulo:
B=A(sum(mod(A,1),2)==0,:)
========== EDIT ====================
Editing w.r.t. comments, if you are only checking for integers in the first column then you do not need to sum results:
B=A(mod(A(:,1),1)==0,:)
Alternative ways would use logicals instead of numericals:
B=A(all(A==round(A),2),:)
or if only the 1st column is checked:
B=A(A==round(A(:,1)),:)

Greater than function in MATLAB not working as expected

i have a matrix B{1} =[1.1 1.1 1.0 ; 0.8 0.9 1.2 ; 0.9 0.9 1.5]
I have found that the overall median of the matrix is 1.0.
Next i want to go through every element in the matrix and compare it with the median. If the element exceeds the error threshold value of 0.1, the element will be replaced by zero. If the element is equal or lesser than 0.1, the element value will remain.
After going through the coding below, i am expecting my end result of B{1} to be
[1.1 1.1 1.0 ; 0.0 0.9 0.0 ; 0.9 0.9 0.0].
However the output of the coding below gives B{1}=[ 0.0 0.0 1.0 ; 0.0 0.9 0.0 ; 0.9 0.9 0.0]
for x=1:9
matrix=B{1};
excess = abs(minus(matrix(x),1.0))
if excess > 0.1
matrix(x)=0;
B{1}=matrix;
end
end
Any idea where is the mistake(s) in the coding?
You are running into precision issue, which can be avoided by adding a little tolerance into it. With that change, you can have a vectorized solution to this -
B{1} =[1.1 1.1 1.0 ; 0.8 0.9 1.2 ; 0.9 0.9 1.5]
matrix=B{1};
TOL = 0.001;%%// Tolerance to account for precision issue
matrix(abs(bsxfun(#minus,matrix,median(matrix(:))))>0.1+TOL)=0;
B{1} = matrix;
In your code, you could have done the same with this -
TOL = 0.001;%%// Tolerance to account for precision issue
excess = abs(minus(matrix(x),1.0+TOL))
Edit 1: You can add a matrix dependent tolerance to it, by using this (thanks to #bdecaf on this) -
TOL = max(eps(matrix(:)))