Fuzzy Logic . How to get the complement - complement

i came across the following fuzzy logic example about fuzzy logic.
Representing Age
Problem 2-1. Fuzzy sets can be used to represent fuzzy concepts. Let U be a reasonable age interval of human beings.
U = {0, 1, 2, 3, ... , 100}
Solution 2-1. This interval can be interpreted with fuzzy sets by setting the universal space for age to range from 0 to 100.
Problem 2-2. Assume that the concept of "young" is represented by a fuzzy set Young, whose membership function is given by the following fuzzy set.
Young= FuzzyTrapeZoid [0 ,0 ,25 ,40]
All i want to understand is how i can get the Complement[Young]

The quick answer is that Complement[Young] = FuzzyTrapeZoid[25,40,100,100]. Here is an image to show (in red) Young, and the complement in green.
Were you looking for an algorithm to solve this?
edit: adding more:
A generic fuzzy trapezoid is: FuzzyTrapeZoid[A,B,C,D]
The membership value is 0 up to A, then ramps from 0 to 1 between A and B, stays at 1 from B to C, then ramps from 1 to 0 between C and D. see page 3 of this intro (warning! pdf)
Since the complement of a fuzzy set = 1 - the membership function, then you can pretty much see the values by inspection. For the original problem (which comes from Mathematica), the complement is a single function. For the generic one FuzzyTrapeZoid[A,B,C,D] you will need 2 trapeziods to make the complement: FuzzyTrapeZoid[0,0,A,B] + FuzzyTrapeZoid[C,D,100,100]
For the Young membership function, it is 1 up to 25, so the complement will be 0 up to 25 (this yields [25,x,x,x] where x is yet to be determined). Since the Young membership function ramps to 0 between 25 and 40, it is clear that the complement will ramp from 0 to 1 in the same range (this yields the [25,40,x,x] where x is yet to be determined). Finally, since the Young membership function is 0 from 40 to 100, the complement will be 1 in the same range, this gives [x,40,100,100] (we knew from before that x = 25).
If you were looking for some more formal proof, I'm sorry, I do proofs poorly as I come from the Captain Kirk school of math: I can see it, and I can jump to the right answer, but I can't tell you exactly how I did it.

Related

Finding if there relationship between numbers

I have a challenge. This may be little tricky or even not possible but wanted to check if anyone has any thoughts on this?
PS : This question is in general and not related to only to R. May be I can say its general mathematics
I have a data
df
ColA ColB ColC
6 9 27
1 4 32
4 8 40
If you observe closely, there is some relationship between these columns.
Example, (ColC/ColB)+ColA will give you number 9.
df
ColA ColB ColC ColD
6 9 27 9
1 4 32 9
4 8 40 9
However this data is manipulated and I made sure there is some relation.
But in general, lets us take any numbers, is there a way to find if there is any relationship between these numbers. Need not be (ColC/ColB)+ColA . It could be anything.
Say we have 5 columns of numeric data. I need to find mathematical operation between these so that common number exists.
This is more into mathematics(algebra).
Can anyone let me know is this even possible ?
For some types of relationships this is doable. But when such a method fails to find a relationship, it typically just means there could be a relationship of a kind not covered by your approach.
One common tool for finding relationships is linear algebra, and linear dependencies in particular. Write your data in a matrix like you did. Consider that a linear equation
a*ColA + b*ColB + c*ColC = 0
Use standard techniques such as Gaussian elimination to find coefficients a, b, c which satisfy this equation but are not all zero themselves. You probably can find a library to compute the kernel of a matrix which you can use for that. Now you know whether one of the columns can be expressed as a linear combination of the other two.
This is a very limited class of relationships, and doesn't cover your example yet. But you can improve it by including more columns. Include a column with ones everywhere to allow for a constant term in your formula. Include all pair wise products.
x + a*ColA + b*ColB + c*ColC + ab*ColA*ColB + ac*ColA*ColC + bc*ColB*ColC + aa*ColA^2 + bb*ColB^2 + cc*ColC^2 = 0
Now for your data this could tell you that there is a solution of the form
b=-9 c=1 ab=1 x=a=ac=bc=aa=bb=cc=0
-9*ColB + ColC + ColA*ColB = 0
which is equivalent to the relationship you described in your question.
But also observed that you are now using 3 data points to determine 10 variables. So this one relationship is by far not the only one.
In general you want at least as many data points as you have variables in your equation. You want at least as many rows as you have columns in your extended matrix. Only then can you say that a relationship between them us indeed a property of the underlying data and not merely an artifact of having too much flexibility and too little data.
In R you might want to look into using linear models for determining coefficients in the presence of imprecise data. You can also use powers of formulas to include all interactions between columns, i.e. those higher degree terms which I included above as well.

How to calculate the expected cost?

I am not good at probability and I know it's not a coding problem directly. But I wish you would help me with this. While I was solving a computation problem I found this difficulty:
Problem definition:
The Little Elephant from the Zoo of Lviv is going to the Birthday
Party of the Big Hippo tomorrow. Now he wants to prepare a gift for
the Big Hippo. He has N balloons, numbered from 1 to N. The i-th
balloon has the color Ci and it costs Pi dollars. The gift for the Big
Hippo will be any subset (chosen randomly, possibly empty) of the
balloons such that the number of different colors in that subset is at
least M. Help Little Elephant to find the expected cost of the gift.
Input
The first line of the input contains a single integer T - the number
of test cases. T test cases follow. The first line of each test case
contains a pair of integers N and M. The next N lines contain N pairs
of integers Ci and Pi, one pair per line.
Output
In T lines print T real numbers - the answers for the corresponding test cases. Your answer will considered correct if it has at most 10^-6 absolute or relative error.
Example
Input:
2
2 2
1 4
2 7
2 1
1 4
2 7
Output:
11.000000000
7.333333333
So, Here I don't understand why the expected cost of the gift for the second case is 7.333333333, because the expected cost equals Summation[xP(x)] and according to this formula it should be 33/2?
Yes, it is a codechef question. But, I am not asking for the solution or the algorithm( because if I take the algo from other than it would not increase my coding potentiality). I just don't understand their example. And hence, I am not being able to start thinking about the algo.
Please help. Thanks in advance!
There are three possible choices, 1, 2, 1+2, with costs 4, 7 and 11. Each is equally likely, so the expected cost is (4 + 7 + 11) / 3 = 22 / 3 = 7.33333.

Determine how different are some vectors

I want to differentiate data vectors to find those that are similar. For example:
A=[4,5,6,7,8];
B=[4,5,6,6,8];
C=[4,5,6,7,7];
D=[1,2,3,9,9];
E=[1,2,3,9,8];
In the previous example I want to distinguish that A,B,C vectors are similar (not the same) to each other and D,E are similiar to each other. The result should be something like: A,B,C are similar and D,E are similar, but the group A,B,C is not similar to the group of D,E. Matlab can do this?
I was thinking using some classification algorithm or Kmeans,ROC,etc.. but I'm not sure which one will be the best one.
Any suggestion? Thanks in advance
One of my new favourite methods for this sort of thing is agglomerate clustering.
First, concatenate all your vectors into a matrix, where each row is a separate vector. This makes such methods much easier to use:
F = [A; B; C; D; E];
Then the linkages can be found:
Z = linkage(F, 'ward', 'euclidean');
This can be plotted using:
dendrogram(Z);
This shows a tree, where each leaf at the bottom is one of the original vectors. Lengths of the branches show similarities and dissimilarities.
As you can see, 1, 2 and 3 are shown to be very close, as are 4 and 5. This even gives a measure of closeness, and shows that vectors 1 and 3 are deemed to be closer than vectors 2 and 3 (in the sense that, percentagewise, 7 is closer to 8 than 6 is to 7).
If all the vectors you are comparing are of the same length, a suitable norm on pairwise differences may well be enough. The norm to choose will depend on your particular criteria of closeness, of course, but with the examples you show, simply summing the absolute values of the components of the pairwise differences gives:
A B C D E
A 0 1 1 12 11
B 0 2 13 12
C 0 13 12
D 0 1
E 0
which doesn't need a particularly well-tuned threshold to work.
You can use pdist(), this function gives you the pairwise distances.
Various distance (opposite of similarity) metrics are already implemented, 'euclidean' seems appropriate for your situation, although you may want to try out the effect of different metrics.
Here it goes the solution I propose based on your results:
Z = [A;B;C;D;E];
Y = pdist(Z);
matrix = SQUAREFORM(Y);
matrix_round = round(matrix);
Now that we have the vector we can set the threshold based on the maximun value and decide with which theshold is the most appropriate.
It would be nice to create some cluster plot showing the differences between them.
Best regards

Why is 0 divided by 0 an error?

I have come across this problem in a calculation I do in my code, where the divisor is 0 if the divident is 0 too. In my code I return 0 for that case. I am wondering, while division by zero is generally undefined, why not make an exception for this case? My understanding why division by zero is undefined is basically that it cannot be reversed. However, I do not see this problem in the case 0/0.
EDIT OK, so this question spawned a lot of discussion. I made the mistake of over-eagerly accepting an answer based on the fact that it received a lot of votes. I now accepted AakashM's answer, because it provides an idea on how to analyze the problem.
Let's say:
0/0 = x
Now, rearranging the equation (multiplying both sides by 0) gives:
x * 0 = 0
Now do you see the problem? There are an infinite number of values for x as anything multiplied by 0 is 0.
This is maths rather than programming, but briefly:
It's in some sense justifiable to assign a 'value' of positive-infinity to some-strictly-positive-quantity / 0, because the limit is well-defined
However, the limit of x / y as x and y both tend to zero depends on the path they take. For example, lim (x -> 0) 2x / x is clearly 2, whereas lim (x -> 0) x / 5x is clearly 1/5. The mathematical definition of a limit requires that it is the same whatever path is followed to the limit.
(Was inspired by Tony Breyal's rather good answer to post one of my own)
Zero is a tricky and subtle beast - it does not conform to the usual laws of algebra as we know them.
Zero divided by any number (except zero itself) is zero. Put more mathematically:
0/n = 0 for all non-zero numbers n.
You get into the tricky realms when you try to divide by zero itself. It's not true that a number divided by 0 is always undefined. It depends on the problem. I'm going to give you an example from calculus where the number 0/0 is defined.
Say we have two functions, f(x) and g(x). If you take their quotient, f(x)/g(x), you get another function. Let's call this h(x).
You can also take limits of functions. For example, the limit of a function f(x) as x goes to 2 is the value that the function gets closest to as it takes on x's that approach 2. We would write this limit as:
lim{x->2} f(x)
This is a pretty intuitive notion. Just draw a graph of your function, and move your pencil along it. As the x values approach 2, see where the function goes.
Now for our example. Let:
f(x) = 2x - 2
g(x) = x - 1
and consider their quotient:
h(x) = f(x)/g(x)
What if we want the lim{x->1} h(x)? There are theorems that say that
lim{x->1} h(x) = lim{x->1} f(x) / g(x)
= (lim{x->1} f(x)) / (lim{x->1} g(x))
= (lim{x->1} 2x-2) / (lim{x->1} x-1)
=~ [2*(1) - 2] / [(1) - 1] # informally speaking...
= 0 / 0
(!!!)
So we now have:
lim{x->1} h(x) = 0/0
But I can employ another theorem, called l'Hopital's rule, that tells me that this limit is also equal to 2. So in this case, 0/0 = 2 (didn't I tell you it was a strange beast?)
Here's another bit of weirdness with 0. Let's say that 0/0 followed that old algebraic rule that anything divided by itself is 1. Then you can do the following proof:
We're given that:
0/0 = 1
Now multiply both sides by any number n.
n * (0/0) = n * 1
Simplify both sides:
(n*0)/0 = n
(0/0) = n
Again, use the assumption that 0/0 = 1:
1 = n
So we just proved that all other numbers n are equal to 1! So 0/0 can't be equal to 1.
walks on back to her home over at mathoverflow.com
Here's a full explanation:
http://en.wikipedia.org/wiki/Division_by_zero
( Including the proof that 1 = 2 :-) )
You normally deal with this in programming by using an if statement to get the desired behaviour for your application.
The problem is with the denominator. The numerator is effectively irrelevant.
10 / n
10 / 1 = 10
10 / 0.1 = 100
10 / 0.001 = 1,000
10 / 0.0001 = 10,000
Therefore: 10 / 0 = infinity (in the limit as n reaches 0)
The Pattern is that as n gets smaller, the results gets bigger. At n = 0, the result is infinity, which is a unstable or non-fixed point. You can't write infinity down as a number, because it isn't, it's a concept of an ever increasing number.
Otherwise, you could think of it mathematically using the laws on logarithms and thus take division out of the equation altogther:
log(0/0) = log(0) - log(0)
BUT
log(0) = -infinity
Again, the problem is the the result is undefined because it's a concept and not a numerical number you can input.
Having said all this, if you're interested in how to turn an indeterminate form into a determinate form, look up l'Hopital's rule, which effectively says:
f(x) / g(x) = f'(x) / g'(x)
assuming the limit exists, and therefore you can get a result which is a fixed point instead of a unstable point.
Hope that helps a little,
Tony Breyal
P.S. using the rules of logs is often a good computational way to get around the problems of performing operations which result in numbers which are so infinitesimal small that given the precision of a machine’s floating point values, is indistinguishable from zero. Practical programming example is 'maximum likelihood' which generally has to make use of logs in order to keep solutions stable
Look at division in reverse: if a/b = c then c*b = a. Now, if you substitute a=b=0, you end up with c*0 = 0. But ANYTHING multiplied by zero equals zero, so the result can be anything at all. You would like 0/0 to be 0, someone else might like it to be 1 (for example, the limiting value of sin(x)/x is 1 when x approaches 0). So the best solution is to leave it undefined and report an error.
You may want to look at Dr. James Anderson's work on Transarithmetic. It isn't widely accepted.
Transarithmetic introduces the term/number 'Nullity' to take the value of 0/0, which James likens to the introduction 'i' and 'j'.
The structure of modern math is set by mathematicians who think in terms of axioms.
Having additional axioms that aren't productive and don't really allow one to do more stuff is against the ideal of having clear math.
How many times does 0 go into 0? 5. Yes - 5 * 0 = 0, 11. Yes - 11 * 0 = 0, 43. Yes - 43 * 0 = 0. Perhaps you can see why it's undefined now? :)
Since x/y=z should be equivalent to x=yz, and any z would satisfy 0=0z, how useful would such an 'exception' be?
Another explanation of why 0/0 is undefined is that you could write:
0/0 = (4 - 4)/0 = 4/0 - 4/0
And 4/0 is undefined.
If a/b = c, then a = b * c.
In the case of a=0 and b=0, c can be anything because 0 * c = 0 will be true for all possible values of c. Therefore, 0/0 is undefined.
This is only a Logical answer not a mathamatical one,
imagine Zero as empty how can you Divide an empty by an empty this is the case in division by zero also how can you divide by something which is empty.
0 means nothing, so if you have nothing, it does not imply towards anything to distribute to anything. Some Transit Facilities when they list out the number of trips of a particular line, trip number 0 is usually the special route that is routed in a different way. Typically, a good example would be in the Torrance Transit Systems where Line 2 has a trip before the first trip known as trip number 0 that operates on weekdays only, that trip in particular is trip number 0 because it is a specialized route that is routed differently from all the other routes.
See the following web pages for details:
http://transit.torrnet.com/PDF/Line-2_MAP.pdf
http://transit.torrnet.com/PDF/Line-2_Time_PDF.pdf
On the map, trip number 0 is the trip that is mapped in dotted line, the solid line maps the regular routing.
Sometimes 0 can be used on numbering the trips a route takes where it is considered the "Express Service" route.
why not make an exception for this
case?
Because:
as others said, it's not that easy;)
there's no application for defining 0/0 - adding exception would complicate mathematics for no gains.
This is what I'd do:
function div(a, b) {
if(b === 0 && a !== 0) {
return undefined;
}
if(b === 0 && a === 0) {
return Math.random;
}
return a/b;
}
When you type in zero divided by zero, there's an error because whatever you multiply zero from will be zero so it could be any number.
As Andrzej Doyle said:
Anything dived by zero is infinity. 0/0 is also infinity. You can't get 0/0 = 1. That's the basic principle of maths. That's how the whole world goes round. But you can sure edit a program to say "0/0 is not possible" or "Cannot divide by zero" as they say in cell phones.

Can anyone recommend some Transformation Matrix tutorials for dummies? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
Can anyone recommend some good starting points for understanding Transformation Matrices for dummies like me with poor math skills.
I'm willing to learn the math, and I'm not a complete idiot (I hope) but the examples I'm finding seem to require a huge leap from what I know, to what I need to know.
I wrote a web program that can be used to play around with transformation matrices. It allows preset types and custom ones.
Play with it online or grab the source.
It should be easy to play with the numbers and instantly see how it affects the house drawing. Look at the code available online to determine what it's doing, and you should be able to understand what's going on.
If you're having trouble, realise that the 3×3 matrix is simply being multiplied by each vertex (X & Y coordinate) in the house shape. Matrix multiplication with the vertex (we will now refer to it as a vector) and a transformation matrix looks like so...
1 0 0 1
0 1 0 * 2
0 0 1 0
On the left is an identity matrix (an idempotent matrix that doesn't affect the vector) and a vector of 1, 2, 0 (assume this maps to position X1 and Y2 in the program mentioned above's graph and ignore the final 0).
Matrix multiplication can be visualised like so...
a b c x a * x + b * y + c * z
d e f + y = d * x + e * y + f * z
g h i z g * x + h * y + i * z
So, in our example, that would be...
1 0 0 1 1 * 1 + 0 * 2 + 0 * 0
0 1 0 * 2 = 0 * 1 + 1 * 2 + 0 * 0
0 0 1 0 0 * 1 + 0 * 2 + 1 * 0
Do that math and we get the final vector...
1
2
0
Since we said our identity matrix shouldn't modify the values, we can see above that that is the case as the resulting vector matched the original.
To explain further, consider when you need to translate the vector. Let's say we want to translate the house by 5 pixels along the X axis. We want to start with the identity matrix, but change the top right number to 5 and make the extra dimension in the vector 1 (you will see why briefly).
1 0 5 1 1 * 1 + 0 * 2 + 5 * 1
0 1 0 * 2 = 0 * 1 + 1 * 2 + 0 * 1
0 0 1 1 0 * 1 + 0 * 2 + 1 * 1
We do the math again...
6
2
1
We can see that the first number (X in our coordinates) has been translated along the X axis by 5. Try it in the program linked above.
The reason we made the third value 1 is so when the math was performed, the translation was considered. Had it been 0, it will be ignored, as any number multiplied by 0 results in 0.
If you're still having trouble, check out videos online (this one, for example) which can help explain it in a more visual fashion.
Remember: pretty much anyone can drive a car, and pretty much anyone can learn this, despite any self-evaluated poor understanding of math. Just keep at it: persistence is key. Good luck.
Like duffymo has pointed out, Matrix Transformations is nothing more but (pre)multiplying a vector (like a 3d point) by a matrix. However, that is pure mathematics, and hard for some people to visualise.
The best way to understand transformation matrices (at least for me) is to get an example code, get it running, and play around with the numbers. Try to see if you can place an object farther away, or rotated by 45 degrees. Try putting the transformations in different order and see what the results are.
All working? Good.
Once you get a feel of that, and if you're brave enough to tackle the maths, you could take these steps:
First, understand how matrix multiplications work. Some links:
http://en.wikipedia.org/wiki/Matrix_multiplication
http://www.gamedev.net/reference/list.asp?categoryid=28#259
Also borrow highschool math textbooks from any of your friends.
Google is your friend.
Once you are comfortable with multiplying a matrix by hand, you will get a feel of why transformations are written that way. As you use them, the understanding of matrices will eventually come to you.
Secondly, I always recommend spending an afternoon trying to implement your own Matrix class and define a few common operations like mul(Vector v), transpose() or even createTranslationMatrix(float x, float y, float z). Put in a few tests and see if the results are the same with what you did by hand.
If you've come that far, try implementing your own Perspective Transformation! It's the most amazing thing we never come to appreciate. A useful explanation here:
Deriving Projection Matrices
You will be very proud of yourself once you have accomplished one of the most labourous, yet under-appreciated tasks of implementing a matrix object. Good luck!
A transformation is nothing more than a matrix multiplying a vector to produce the transformed vector, so if you don't understand matrix multiplication and addition you can't get very far.
Start with matricies and linear algebra. There are lots of books out there, but realize that based on the statement that I made above you don't need to read that whole book. You won't need eigenvalues or Gaussian elimination or vector spaces or any of the other stuff that will be advanced and difficult.
You just need to know how to extend what you know about multiplying and adding numbers to matricies.
Getting the entries in that transformation matrix is another matter altogether. You'll need a good book on mathematics and computer graphics. You won't find that in a linear algebra textbook.

Resources