I'm struggling to use the functions MultinormalDistribution and InverseCDF in MultivariateStatistics package. Essentially
<< MultivariateStatistics`
sig = .5; u = .5;
dist = MultinormalDistribution[{0, 0}, sig*IdentityMatrix[2]];
delta=InverseCDF[dist, 1 - u]
The output is
InverseCDF[
MultinormalDistribution[{0, 0}, {{0.5, 0}, {0, 0.5}}], {0.5}]
can someone correct the above code? If I've understood this correctly, delta should be a single number.
1) MultinormalDistribution is now built in, so don't load MultivariateStatistics it unless you are running version 7 or older. If you do you'll see MultinormalDistribution colored red indicating a conflict.
2) this works:
sig = .5; u = .5;
dist = MultinormalDistribution[{0, 0}, sig IdentityMatrix[2]];
delta = CDF[dist, {xx, yy}]
(*1/4 Erfc[-1. xx] Erfc[-1. yy]*)
note it is a 2d distribution so that CDF needs two variables in its second argument. The "inverse" of this is a curve in {xx,yy} space. I don't think InverseCDF works for such multivariate distributions however.
You can visualize your inverse like this:
ContourPlot[delta == 1/2 , {xx, -2, 4}, {yy, -2, 4}]
Related
I have a glb from Blender (Sapling Tree) that has a trunk and leaves which import as separate meshes. I am trying to create a multi mesh with this tree and am currently having some strange results that I can't figure out. I have a script attached to one MultiMeshInstance3D that creates the multi mesh for the trunk and another script attached to another MultiMeshInstance3D which creates the Multimesh for the leaves. Since I want the leaves to be Transformed exactly like the trunk I thought I'd position and rotate the trunks first then grab that transform data and just assign it to each instance for the leaves Multimesh. Unfortunately once I apply the transform using multimesh.set_instance_transform() on the leaves the outcome seems to invert the Vertex in the transform.
In my tree trunk script I create the positions using
for i in len(positions):
var position = positions[i]
var basis = Basis(Vector3.UP, 0.0).rotated(Vector3.UP, randi_range(0, 180))
multimesh.set_instance_transform(i, Transform3D(basis, position))
And in my leaves script I take the trunk transform and then apply each leaves instance to each trunk
var transform1 = tree_trunk.multimesh.transform_array
var j = 0
for i in range(0, tree_trunk.count):
var t = transform1.slice(j, j+4)
multimesh.set_instance_transform(i, Transform3D(t[0], t[1], t[2], t[3]))
j += 4
However the result for the trunk and leaves are different in the fact that the leaves don't have the same transform
Here's an example of what happens:
The trunks transform_array index 0 as an example
[(-0.994367, 0, 0.105988), (0, 1, 0), (-0.105988, 0, -0.994367), (-48.50394, 35.99831, 29.6063),...
The leaves transform_array index 0
[(-0.994367, 0, -0.105988), (0, 1, 0), (0.105988, 0, -0.994367), (-48.50394, 35.99831, 29.6063),...
As you can see it inverts the x-axis z value and the z-axis x value but I don't know why. My current fix is to multiply them to fix it and get my leaves on the same rotation as the trunks.
multimesh.set_instance_transform(i, Transform3D(t[0] * Vector3(1, 1, -1), t[1], t[2] * Vector3(-1, 1, 1), t[3]))
The origin seems to be fine it's just the rotation of the leaves that is off.
That fixes the problem but why do they invert in the first place?
I have 2 data sets x1 and x2. I want to be able to get a total sum of all the products of x1 and x2 only in the rows where the From column has Auckland in it.
see here
The final answer should be (5*1) + (2*1) + (3*1) + (4*1) or 14. The PuLP code that I wrote to do this is given below
# Import PuLP modeller functions
from pulp import *
varFinal = sum([x1[a] * x2[a] for a in Arcs if a == Nodes[0]])
print Nodes[0]
print Arcs[0]
Final = varFinal
The output that gets printed to the console is
Auckland
('Auckland', 'Albany')
I realise that my final value is zero because Arcs[some number] does not equal Nodes[some number]. Is there anyway to change the code so my final value is 14?
Any help is appreciated.
Welcome to stack overflow! Cause you've only posted part of your code, I have to guess at what data-types you're using. From the output, I'm guessing your Nodes are strings, and your Arcs are tuples of strings.
Your attempt is very close, you want the from column to have Auckland in it. You can index into a tuple the same way you would into an array, so you want to do: a[0] == Nodes[0].
Below is a self-contained example with the first bit of your data in which outputs the following (note that I've changed to python 3.x print statements (with parentheses)):
Output:
Auckland
('Auckland', 'Albany')
14
Code:
# Import PuLP modeller functions
from pulp import *
# Data
Nodes = ['Auckland',
'Wellington',
'Hamilton',
'Kansas City',
'Christchuch',
'Albany',
'Whangarei',
'Rotorua',
'New Plymouth']
Arcs = [('Auckland','Albany'),
('Auckland','Hamilton'),
('Auckland','Kansas City'),
('Auckland','Christchuch'),
('Wellington','Hamilton'),
('Hamilton','Albany'),
('Kansas City','Whangarei'),
('Christchuch','Rotorua')]
x1_vals = [1, 2, 3, 4, 5, 9, 11, 13]
x2_vals = [5, 1, 1, 1, 1, 1, 1, 1]
x1 = dict((Arcs[i], x1_vals[i]) for i in range(len(Arcs)))
x2 = dict((Arcs[i], x2_vals[i]) for i in range(len(Arcs)))
varFinal = sum([x1[a] * x2[a] for a in Arcs if a[0] == Nodes[0]])
print(Nodes[0])
print(Arcs[0])
print(varFinal)
For future reference, answers are most likely to be forthcoming if you include code which others can try to run (without external data dependencies), that way people can try to run it, fix it, and re-post it.
I've been using imgaug to augment data for my project. Naturally, I'd use affine transformations, so I understand that we use the order parameter to choose the interpolation method. The way to choose interpolation looks quite obscure, though, at least to me.
Let's say this is my augmenter (it's a part of Sequential() augmenter):
iaa.Affine(scale = {"x": (+0.8, +1.0), "y": (+0.8, +1.0)},
translate_percent = {"x": (-0.2, +0.2), "y": (-0.2, +0.2)},
rotate = (-5, +5),
shear = ( -5, +5),
order = [1, 2], #interpolation
cval = 255,
)
As far as I know, order = [1,2] stands for bi-quadratic interpolation, and order = [0,1] stands for linear interpolation. What does it mean? How do I get other interpolations, such as bicubic or Lanczos?
"Use the Source, Luke". Either directly, or from docstring with help function.
order : int or iterable of int or ia.ALL or StochasticParameter, optional(default=1)
Interpolation order to use. Same meaning as in
skimage:
* 0: Nearest-neighbor
* 1: Bi-linear (default)
* 2: Bi-quadratic (not recommended by skimage)
* 3: Bi-cubic
* 4: Bi-quartic
* 5: Bi-quintic
Method 0 and 1 are fast, 3 is a bit slower, 4 and 5 are very
slow.
* If a single int, then that order will be used for all images.
* If an iterable, then for each image a random value will be sampled
from that iterable (i.e. list of allowed order values).
* If ia.ALL, then equivalant to list [0, 1, 3, 4, 5].
* If StochasticParameter, then that parameter is queried per image
to sample the order value to use.
Here is a problem that I don't know if can be solved in Mathematica.
(* Courtesy to Lunchtime Playground Blog *)
to3d[plot_, height_, opacity_] :=
Module[{newplot}, newplot = First#Graphics[plot];
newplot = N#newplot /. {x_?AtomQ, y_?AtomQ} -> {x, y, height} /.
Arrowheads[List[List[x_, y_, notz_]]] ->
Arrowheads[List[List[x, y]]];newplot /.GraphicsComplex[xx__] -> {Opacity[opacity], GraphicsComplex[xx]}];
(* A function to combine 2D Graphics object in Mathematica *)
test[list_]:=VectorQ[list,SameQ[Head[#],Graphics]&];
My3DPlot[list_?(test[#]&),height_?(VectorQ[#,NumberQ]&),opacity_?(VectorQ[#,NumberQ]&),opts:OptionsPattern[]]:=Block[{a},a=MapThread[Graphics3D[to3d[#1,#2,#3]]&,{list,height,opacity}];
Show[a,opts]
]
(* List of 2D graphics *)
list=Table[ContourPlot[y+Sin[x^i+i y],{x,-3,3},{y,-3,3},Contours->15,ContourLines->False,ColorFunction->RandomChoice[ColorData["Gradients"]]],{i,{1,2,3,4}}];
(* List of heights where you want to place the images *)
height={-.5,0,.5,1};
(* List of opacities you want to apply to your 2D layers *)
opacity={1,.8,.7,.5};
(* The function inherits all the options of standard Graphics3D as they are passed through the Show command *)
My3DPlot[Reverse#list,height,opacity,Lighting->"Neutral",BoxRatios->{1,1,.9},Axes->True]
Now this returns a cool picture like this one.
Here my question is if it is possible to create a filling for this 2D layers using the same color functions as are used with in the contour plots for example? Target is to fill the hollow between these 2D layers with some light or color that continuously changes according to the neighboring layer color-function.
I hope this can be done in Mathematica but my limited knowledge in Mathematica graphics is making it a difficult hurdle for me.
It should be possible. Texture can be used to generate a 3D texture. The example given in the documentation:
data = Table[{r, g, b}, {r, 0, 1, 1/20}, {g, 0, 1, 1/20}, {b, 0, 1, 1/20}];
Graphics3D[
{
Opacity[1/3],
Texture[data],
EdgeForm[],
Polygon[Table[{{0, 0, z}, {1, 0, z}, {1, 1, z}, {0, 1, z}}, {z, 0, 1, 1/20}],
VertexTextureCoordinates ->
Table[{{0, 0, s}, {1, 0, s}, {1, 1, s}, {0, 1, s}}, {s, 0, 1, 1/20}]]
},
Lighting -> "Neutral"
]
This simulates a volume by using a large set of planes. You can do the same. All you have to do is describe the 3D texture, which should interpolate between the planes you already have.Blend would be the function to be used here. For each pixel column in your cube the color varies as Blend[{col1,col2,col3,...},x] with x going from 0 to 1 and coli the color of the pixel in the ith plane given by the contour plots.
The main problem will be that a 3D semi-transparant object with fuzzy color gradients is not something that visualizes very well.
I need to input a variable, say var, into Mathematica function Series[ ] like this: Series[A^2+B^2+C^2, var]. Series[ ] has the following syntax:
Series[f, {x, x_0, n}] generates a power series expansion for f about the point x=x_0 to order n.
Series[f, {x, x_0, n}, {y, y_0, m}, ...] successively finds series expansions with respect to x, then y, etc.
Because I am not always computing Series[ ] in one dimension (i.e., B and C are not always variables at each iteration), var must be properly formatted to fit the dimension demands. The caveat is that Mathematica likes lists, so any table degenerated will have a set of outer {}.
Suppose my previous code generates the following two sets of sets:
table[1]= {{A, 0, n}};
table[2]= {{A, 0, n}, {B, 0, m}}; .
My best idea is to use string manipulation (for i= 2):
string = ToString[table[i]]; .
str = StringReplacePart[string, {" ", " "}, {{1}, {StringLength[string], StringLength[string]}}]
The next step is to convert str to an expression like var and do Series[A^2 + B^2 + C^2, var] by doing var= ToExpression[str], but this returns the following error:
ToExpression::sntx: Invalid syntax in or before "{A, 0, n}, {B, 0, m}".
$Failed
Help convert str to expression propertly or suggest another way to handle this problem.
If I understood correctly, you have
table[2] = {{A, 0, n}, {B, 0, m}};
and are trying to obtain from that
Series[f[A,B],{A,0,n},{B,0,m}]
This may be done using Sequence, like so (I will use series instead of Series to keep it unevaluated so you can see what is happening):
series[f[A, B], Sequence ## table[2]]
(*
-> series[f[A,B],{A,0,n},{B,0,m}]
*)
So for instance
table[3] = {{A, 0, 2}, {B, 0, 2}};
Series[f[A, B], Sequence ## table[3]]
gives the right series expansion.
You can use First or Last or more generally, Part to get the List you want. For e.g.,
var = {{x, 0, 3}, {x, 0, 5}};
Series[1/(1 + x), var[[1]]]
Out[1]= 1 - x + x^2 - x^3 + O[x]^4
Series[1/(1 + x), var[[2]]]
Out[2]= 1 - x + x^2 - x^3 + x^4 - x^5 + O[x]^6
EDIT:
For multiple variables, you can use a SlotSequence (##) along with Apply (##) like so:
Series[Sin[u + w], ##] & ## {{u, 0, 3}, {w, 0, 3}}