# Similarity function with given properties

I would like to find a similarity function $f$ between two values (each value is continuous and is bounded by $[0,1]$) that would have the following properties:

$$f(1, 1) = 0.5$$

$$f(0.5, 0.5) =0.25$$

$$f(1, 0) = 1$$

$$f(0, 1) = 1$$

$$f(0, 0) = 0$$

Is there such a function in math? If not how could I design it?

• I took the liberty of editing your question. Please take a look and roll back if I misunderstood. Jun 8 '16 at 9:49
• If you want a similarity function, do you require that $f(a,b)=f(b,a)$ and that $f\geq 0$? Possibly also that $f(a,b)>0$ if $a\neq b$? Jun 8 '16 at 9:50
• I don't strickly require metric properties you mentioned. But this would be desirable to have them. Jun 8 '16 at 9:52

The function

$$f\colon [0,1]\times[0,1]\to[0,1], \quad(x,y)\mapsto \frac{1}{4}x+\frac{1}{4}y+\frac{3}{4}(x-y)^2$$

does what you want. Plus, it's positive, symmetric and definite ($x\neq y$ implies that $f(x,y)>0$).

Neither it nor its root is linearly homogeneous like a norm-derived distance function, though ($f(\lambda x, \lambda y)\neq\lambda f(x,y)$) - but that does not seem to possible anyway given your requirements.

I found it by estimating a linear model based on your input data, with covariates $x$, $y$ and $(x-y)^2$:

foo <- data.frame(a=c(1,.5,1,0,0),b=c(1,.5,0,1,0),y=c(.5,.25,1,1,0))
model <- lm(y~a*b+I((a-b)^2),foo)

xx <- yy <- seq(0,1,.01)
persp(x=xx,y=yy,z=outer(xx,yy,function(xx,yy)xx/4+yy/4+0.75*(xx-yy)^2))

• I guess now I can also try to specify more data points and make the function more precise, right? Jun 8 '16 at 10:05
• Yes indeed. Then it likely won't fit so nicely at the boundaries any more, though. Jun 8 '16 at 10:12
• Very clever solution. How did you hit upon the specific covariates you used, symmetry? Jun 8 '16 at 16:32
• @MatthewDrury: yes. As a matter of fact, after thinking about it, I'd rather use $(x+y)^2$ (instead of $x$ and $y$) and $(x-y)^2$, to enforce symmetry even with different input data. I don't like (dis)similarities that are not symmetric. Jun 8 '16 at 18:06