Understand normalized squared euclidean distance? - statistics

I'm trying to understand the normalized squared euclidean distance formula from the Wolfram documentation:
1/2*Norm[(u-Mean[u])-(v-Mean[v])]^2/(Norm[u-Mean[u]]^2+Norm[v-Mean[v]]^2)
I searched around for this formula on the web but couldn't find it. Can someone explain how this formula is derived?

Meaning of this formula is the following:
Distance between two vectors where there lengths have been scaled to
have unit norm. This is helpful when the direction of the vector is
meaningful but the magnitude is not.
https://stats.stackexchange.com/questions/136232/definition-of-normalized-euclidean-distance

Further to Luca's comment, here is an example showing the "distance between two vectors where their lengths have been scaled to have unit norm". It doesn't equal the normalised square Euclidean distance. The former is coloured blue in the graphic below. The standard Euclidean distance is coloured red.
(* Leave this unevaluated to see symbolic expressions *)
{{a, b, c}, {d, e, f}} = {{1, 2, 3}, {3, 5, 10}};
N[EuclideanDistance[{a, b, c}, {d, e, f}]]
7.87401
Norm[{a, b, c} - {d, e, f}]
SquaredEuclideanDistance[{a, b, c}, {d, e, f}]
Norm[{a, b, c} - {d, e, f}]^2
N[NormalizedSquaredEuclideanDistance[{a, b, c}, {d, e, f}]]
0.25
(1/2 Norm[({a, b, c} - Mean[{a, b, c}]) - ({d, e, f} - Mean[{d, e, f}])]^2)/
(Norm[{a, b, c} - Mean[{a, b, c}]]^2 + Norm[{d, e, f} - Mean[{d, e, f}]]^2)
1/2 Variance[{a, b, c} - {d, e, f}]/(Variance[{a, b, c}] + Variance[{d, e, f}])
{a2, b2, c2} = Normalize[{a, b, c}];
{d2, e2, f2} = Normalize[{d, e, f}];
N[EuclideanDistance[{a2, b2, c2}, {d2, e2, f2}]]
0.120185
Graphics3D[{Line[{{0, 0, 0}, {1, 2, 3}}],
Line[{{0, 0, 0}, {3, 5, 10}}],
Red, Thick, Line[{{1, 2, 3}, {3, 5, 10}}],
Blue, Line[{{a2, b2, c2}, {d2, e2, f2}}]},
Axes -> True, AspectRatio -> 1,
PlotRange -> {{0, 10}, {0, 10}, {0, 10}},
AxesLabel -> Map[Style[#, Bold, 16] &, {"x", "y", "z"}],
AxesEdge -> {{-1, -1}, {-1, -1}, {-1, -1}},
ViewPoint -> {1.275, -2.433, -1.975},
ViewVertical -> {0.551, -0.778, 0.302}]

Related

How to merge list with common elements in it and form a dictionary with the merged list?

As a beginner in python, I want to know simplest way without using any complicated built in modules or very complex program to form a merged list with common elements in it and with that merged list form a dictionary with smallest element as a key and other than the key everything is value.
For example,
list = [[a, o], [c, r], [d, m], [l, p], [g, t], [e, r], [e, s], [e, o]]
and my merged list should be,
merged list = [[a, e, r, c, s, o], [l, p], [d, m], [g, t]]
and my dictionary output should be,
pair dictionary = {'a': [c, e, r, s ,o], 'l': 'p', 'd': 'm', 'g': 't'}

How to solve a cubic function without knowing the coefficients a b and c?

Apology for this silly question. In the cubie function below (as from a school project), I am given the value for x and f(x) while coefficients a, b ,c and constant of d are unknown.
f(x)=ax^3+bx^2+cx+d
In such case, is there a way to find out a, b and c by using any python package? I found good amount of python tutorial for solving cubic function but they seem to mainly focus on solving x while a, b and c value are given.
Here is an approach via sympy, Python's symbolic math library.
As an example, we are trying to find the formula for the sum of the first n triangular numbers. The triangular numbers (formula n*(n+1)/2) are 0, 1, 3, 6, 10, 15, 21, ..... The sums of the first n triangular numbers are thus 0, 1, 4, 10, 20, 35, 56, ....
from sympy import Eq, solve
from sympy.abc import a,b,c,d, x
formula = a*x**3 + b*x**2 + c*x + d # general cubic formula
xs = [0, 1, 2, 3] # some x values
fxs = [0, 1, 4, 10] # the corresponding function values
sol = solve([Eq(formula.subs(x, xi), fx) for xi, fx in zip(xs, fxs)])
print(sol) # {a: 1/6, b: 1/2, c: 1/3, d: 0}
You can use more x, fx pairs to check that a cubic formula suffices (this won't work with float values, as sympy needs exact symbolic equations).
Also sympy's interpolate can be interesting. This calculates a polynomial through some given points. Such code could look like:
from sympy import interpolate
from sympy.abc import x
xs = [0, 1, 2, 3]
fxs = [0, 1, 4, 10]
fx_dict = dict(zip(xs, fxs))
sol = interpolate(fx_dict, x)
print(sol) # x**3/6 + x**2/2 + x/3
print(sol.factor()) # x*(x + 1)*(x + 2)/6

Converting a python dictionary into modified dictionary

I have a dictionary like this :
[{'Custodian': a, 'BatchId': b, 'Key': c, 'BamSymbol': d, 'SecurityType': e, 'PMCode': f, 'ComplianceGroup': g, 'SubStrategy': h, 'AggregationUnit': i, 'Strategy': j, 'Size': k, 'Side': l}]
But i want to convert this dictionary as a modified one :
[{'Custodian': a, 'BatchId': b, 'Security': {'Key': c, 'BamSymbol': d, 'SecurityType': e}, 'Portfolio': {'PMCode': f, 'ComplianceGroup': g, 'SubStrategy': h, 'AggregationUnit': i, 'Strategy': j}, 'Size': k, 'Side': l}]
I am not sure how to proceed with this.

ColorFunction with InterpolationFunction in RegionPlot3D

I am writing to ask a question regarding the implementation of a field-dependent color in a 3d region plot in Mathematica.
Specifically, I have created the following plot, where f[x,y,z] is an interpolating function of a three-dimensional array (this is done to have lower resolution plots with ease, since the amount of data in the array is significant).
The problem I am encountering is that if I run the following instruction:
RegionPlot3D[f[x, y, z] >= 0.5 && f[x, y, z] <= 0.6, {x, 0, 1}, {y, 0,0.416}, {z, 0, 0.666},
ColorFunction -> Function[{x, y, z}, Hue[Rescale[f[x, y, z], {0, 1}]]]]
The color is not imposed correctly (i get a region of uniform color). If I utilize a function g instead (can be any function, e.g. the norm of the point position) inside the Hue, so that
Hue[Rescale[g[x, y, z], {0, 1}]]
the color information is passed correctly. I assume that I am making a mistake with the handling of InterpolatingFunction objects. How should this problem be handled?
Any help is appreciated.
RegionPlot3D passes 4 arguments to ColorFunction :
Try this (just adding a dummy arg to Function )
RegionPlot3D[f[x, y, z] >= 0.5 && f[x, y, z] <= 0.6, {x, 0, 1}, {y, 0,0.416}, {z, 0, 0.666},
ColorFunction -> Function[{x, y, z, p}, Hue[Rescale[f[x, y, z], {0, 1}]]]]
or like this:
RegionPlot3D[f[x, y, z] >= 0.5 && f[x, y, z] <= 0.6, {x, 0, 1}, {y, 0,0.416}, {z, 0, 0.666},
ColorFunction -> ( Hue[Rescale[f[#1, #2, #3 ], {0, 1}]] &)]

Uniformly distribute Points within an object using Graphics in Mathematica

Considering :
preferred ={{1, 1, 63}, {2, 1, 44}, {3, 1, 27}, {4, 1, 33}, {5, 1, 33}}
frmWidth = 20.9067;
frmHeight = 15.68;
I am displaying 5 types of stimuli 2 by 2. Subjects must choose the one they prefer. Each type of stimuli is displayed 80 times so :
{1,1,63} indicates that the stimuli Cond 1 was preferred 63 times out of the 80 times it was displayed.
{3, 1, 27} indicates that the stimuli Cond 3 was preferred 27 times out of the 80 times it was displayed.
Cond1 refers to center of the screen
Cond2 refers to Top-Left Quadrant
Cond3 refers to Top-Right Quadrant
Cond4 refers to Bottom-Left Quadrant
Cond5 refers to Bottom-Right Quadrant
I would like to express this showing results.
This is what I have done :
Graphics[{
Black, EdgeForm[{Thin, LightGray}],
Rectangle[{-1, -1}, {frmWidth + 1, frmHeight + 1}],
PointSize[0.03],
Yellow,
Point#Tuples[{Range[0, frmWidth/2, frmWidth/19],
Range[0, frmHeight/2, frmHeight/14]}][[;; preferred[[5, 3]]]],
Red,
Point#Tuples[{Range[frmWidth/2, frmWidth, frmWidth/19],
Range[0, frmHeight/2, frmHeight/14]}][[;; preferred[[4, 3]]]],
Green,
Point#Tuples[{Range[frmWidth/2, frmWidth, frmWidth/19],
Range[frmHeight/2, frmHeight, frmHeight/14]}][[;; preferred[[3, 3]]]],
Orange,
Point#Tuples[{Range[0, frmWidth/2, frmWidth/19],
Range[frmHeight/2, frmHeight, frmHeight/14]}][[;;
preferred[[2, 3]]]],
Blue,
Point#Tuples[{Range[frmWidth/4, 3/4 frmWidth, frmWidth/19],
Range[frmHeight/4, 3/4 frmHeight, frmHeight/14]}][[;;
preferred[[1, 3]]]]
}]
Problem is the rectangles are gradually filled with points from left to right, instead of the points being uniformly located.
Consider the following :
Graphics[{
White, EdgeForm[Thick],
Rectangle[{0, 0}, {frmWidth, frmHeight}],
Orange, Opacity[.5],
Rectangle[{0, frmHeight/2}, {frmWidth/2, frmHeight}, RoundingRadius -> 3],
Green,
Rectangle[{frmWidth/2, frmHeight/2}, {frmWidth, frmHeight},RoundingRadius -> 3],
Red,
Rectangle[{frmWidth/2, 0}, {frmWidth, frmHeight/2}, RoundingRadius -> 3],
Yellow,
Rectangle[{0, 0}, {frmWidth/2, frmHeight/2}, RoundingRadius -> 3],
Blue,
Rectangle[{frmWidth/4, frmHeight/4}, {3/4 frmWidth, 3/4 frmHeight}, RoundingRadius -> 3]
}]
Now I would like to fill those edge rounded rectangles with the points but have the density changing rather than the part of the rectangles that are filled.
Below is something very ugly I draw in PPT :
Ideally, the shapes filled with Points could be of any kind.
Points would not overlap.
Please let me know alternative ideas.
OK, try this:
Manipulate[ld = Floor[Sqrt[n]];
Graphics[
{{EdgeForm[Dashed], White,
Polygon[{{0, 0}, {0, h}, {w, h}, {w, 0}}]},
Point[Flatten[#, 1] &#
Table[{x, y}, {x, 0, w, w/ld}, {y, 0, h, h/ld}]] },
PlotRange \[Rule] {{-1, 20}, {-1, 20}}],
{{n, 25}, 10, 100, 1},
{{h, 10}, 5, 20},
{{w, 10}, 5, 20}]
typical configuration:
(the code I gave lets you control the total number and size of the box via sliders)
Given that your rectangles are rather small, the easiest solution is to use
RandomSample[ allPointsInAnObject ]
Kind of like so:
Graphics[{Circle[{0, 0}, 11], PointSize[0.02],
Point[RandomSample[
Cases[Outer[List, Range[-11, 11], Range[-11, 11]], {x_, y_} /;
x^2 + y^2 <= 11^2, {2}], 50]]}]

Resources