Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am new to Rcpp so I apologize in advance if this question is simple to answer. I searched on the web but couldn't find much help and I am hoping the savviness in this forum can help me!
I have an existing code in R using Rcpp, and I need to add to this code the following. I have a quadratic function in two variables, f(x, y), and I need to find the zeros of it:
f(x, y) = (x + by + c)' W (x + by + c)
where the unknowns are x and y. That is, I am interested in finding the set of pairs (x, y) that satisfy f(x , y)=0.
Note: This is a simulation exercise where I need to find the zeros of this function for different values of a, b, c and W. Therefore, I need to code this in a mechanical way (cannot just find the solution, for instance, by graphical inspection). Both variables are continue, and I don't want to work with a grid for (x,y) to see when f(x,y)=0. I need a more general/optimization solution. I don't really know what values (x,y) can take.
Before diving into the numerical part I think you should define this question in a better way. Here I assume that x, y, and c are vectors, and b is a scalar.
A quick observation is that, if W is positive definite, then f(x, y) = 0 implies that x + by + c = 0. If both x and y are free variables, then the solution is not unique. For example, if (x, y) is a solution, then (x - b, y + 1) (element-wise operations) is also a solution.
If W is indefinite, then the equation also has multiple solutions. I just give a very simple example here. Imagine that W is a 2x2 diagonal matrix with 1 and -1 on the diagonal. Then as long as x + by + c = (t, t)' for any t, the function value is exactly zero.
In short, under my assumption on the notations, the equation has infinite number of solutions. I believe you need additional restrictions to make it unique.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
I want to make a model using TensorFlow which will return the 2 characteristics of a Weibull distribution. In order to make it I need to create a loss function which fits the Weibull Distribution.
I found online how to make Negative log likelihood for a binomial
distribution (k is the shape parameter, l is the scale parameter and y_true is the current value that the loss function gets):
nll = (
tf.math.lgamma(k)
+ tf.math.lgamma(y_true + 1)
- tf.math.lgamma(k + y_true)
- k * tf.math.log(l)
- y_true * tf.math.log(1 - l)
)
but I don't know how to calculate the negative log likelihood for a Weibull distribution.
you need to take the sum of the log probabilities of this PDF:
Now, ignoring the part of x<0 (which i hope that your dataset fulfill, otherwise you have chosen the wrong distribution), the transformation is done as follows:
you have the product of the probabilities: prod p(x)
you take the log, so you have log(prod p(x))
by log properties, becomes sum log(p(x))
you just substitute p(x) with the above formula, and apply the properties of log
Now, your NN should predict both k and lambda (will call it l), supposing that the output layer respect the domain of the parameters, the loss of a single element is the following:
def loss(x,k,l,epsilon=1e-7):
return tf.math.log(k)-tf.math.log(l) + (tf.math.log(x) - tf.math.log(k)) * (k-1) - tf.pow(x/(k+epsilon), k)
And I'm pretty sure you can just add a reduce at the beginning to make it work for multiple prediction:
def loss(targets,k_predictions,lambda_predictions,epsilon=1e-7):
return tf.reduce_mean(
tf.math.log(k_predictions) - tf.math.log(lambda_predictions) + (tf.math.log(targets) -
tf.math.log(k_predictions)) * (k_predictions-1) - tf.pow(targets/(k_predictions+epsilon), k_predictions)
)
Well, I have a quadratic programming optimization problem, well structured in Matlab. For instance,
With x a n*1 vector, we have
x = argmin (1/2*x'*H*x+f'*x)
s.t.
A*x <= b
x_lb<=x<=x_ub
Of course, A is a matrix and b, x_lb, x_ub are vectors.
However, my question is, how to construct such a standard in Julia
I tried with Jump.
Actually,
A*x .<= b
can make the last constiant work. But how to construct the second constraint x_lb<=x<=x_ub in JuMP? I tried with
[x_lb[i]<=x[i]<=x_ub[i] for i =1:X_dim]
but it didn't work. It seems like only constant can be assigned as upper/lower bounds?
Also, for the quadratic objective, "aff" and "quad" can be used but we need to treat x, H and f element-wisely, which makes such a job time-consuming.
Any help is appreciated!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a line with start point as P1(x1, y1) & end Point as P2(x2, y2). This line is from the center of circle. The circle radius is r. Need a simple equation to identify the circle line intersect point?
Assuming P1 is the center of the circle, first get the slope of the line, then follow it from P1 to distance r along that direction.
phi = atan2(y2-y1, x2-x1)
x = x1 + r * cos(phi)
y = y1 + r * sin(phi)
The equation for a circle is (x-h)^2 - (y-k)^2 = r^2, where the center is (h, k) (which will end up being (0, 0) relative to your line)
Given two points, you can find the slope of the line, now you can plug it into the formula y = m*x + b.
You now have a system of two equations, solve for x or y in one equation, then plug that expression into the other equation and you will find the numeric value of the variable you did not solve for. You can then plug that back into the equation for a line and find the second variable.
Here is the general formula: http://mathworld.wolfram.com/Circle-LineIntersection.html
And some other answers: Circle line-segment collision detection algorithm?
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'll formulate a simple problem that I'd like to solve with machine learning (in R or similar platforms): my algorithm takes 3 parameters (a,b,c), and returns a score s in range [0,1]. The parameters are all categorical: a has 3 options, b has 4, and c has 10.
Therefore my dataset has 3 * 4 * 10 = 120 cases.
High scores are desirable (close to 1), low scores are not (close to 0).
Let's treat the algorihm as a black box, taking a,b,c and returning a s.
The dataset looks like this:
a, b, c, s
------------------
a1, b1, c1, 0.223
a1, b1, c2, 0.454
...
If I plot the density of the s for each parameter, I get very wide distributions, in which some cases perform very well (s > .8 ), others badly (s < .2 ).
If I look at the cases where s is very high, I can't see any clear pattern.
Parameter values that overall perform badly can perform very well in combination with specific parameters, and vice versa.
To measure how well a specific value performs (e.g. a1), I compute the median:
median( mydataset[ a == a1]$s )
For example, median(a1)=.5, median(b3)=.9, but when I combine them, I get a lower result s(a_1,b_3)= .3.
On the other hand, median(a2)=.3, median(b1)=.4, but s(a2,b1)= .7.
Given that there aren't parameter values that perform always well, I guess I should look for combinations (of 2 parameters) that seem to perform well together, in a statistically significant way (i.e. excluding outliers that happen to have very high scores).
In other words, I want to obtain a policy to make the optimal parameter choice, e.g. the best performing combinations are (a1,b3), (a2,b1), etc.
Now, I guess that this is an optimisation problem that can be solved using machine learning.
What standard techniques would you recommend in this context?
EDIT: somebody suggested a linear programming solution with glpk, but I don't understand how to apply linear programming to this problem.
The most standard technique for this question is Linear Regression. You may predict the value for specific parameters; in more general - to get the function that on your 3 parameters gives you maximum value
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I'm trying to produce a boolean method that will return true if a point (lonc,latc) lies on a great circle arc starting at (lona,lata) and ending at (lonb,latb)
the point of the method returning true is so if you are in a location where you should be able to see this great circel the section you can see will be shown.
the jist is that you are at (lond,latd) with a small circle at 10degrees radius and I want to work out if the great circle and small circle will intersect. There will be multiple great circles but only one small circle.
I feel the simplest approach is to check any of the longitude and latitudes on the circumference of the small circle lie on a great circle line
any help will be most appreciated
You have three latitude-longitude pairs (θa, φa), (θb, φb) and (θc, φc) and you want to determine whether point (θc, φc) lies on the great circle determined by (θa, φa) and (θb, φb). You can accomplish this for example using the following calculations:
Convert all latitude-longitude pairs to (x, y, z) triples using the following formulae: x = sin(θ)*cos(φ), y = sin(θ)*sin(φ), z = cos(θ). This will give you three triples (xa, ya, za), (xb, yb, zb), (xc, yc, zc).
Determine the formula in cartesian coordinates of the plane going through points (xa, ya, za), (xb, yb, zb) and the origin (0, 0, 0). The formula is x+b*y+c*z=0 and we seek b and c which can be determined from two simultaneous equations obtained by substituting coordinates of points A and B for x, y and z in the plane formula x+b*y+c*z=0.
Calculate the distance between point (xc, yc, zc) and the plane determined in point 2 using the following formula: d=abs(xc+b*yc+c*zc)/sqrt(1+b*b+c*c).
From the distance in cartesian coordinates you can find the angular distance between the point (xc, yc, zc) and the great circle determined by (θa, φa) and (θb, φb) using the following formula: α = asin(d).
Since you should not compare floating point numbers exactly you should have an angular threshold which determines how far a point can be from the great circle for you to still consider the point to lie on the circle. You then compare α determined in point 4 with the threshold to come up with the boolean value you seek.
assuming you're working on a sphere wich has points a and b marking a cirlce arc. and the you have point c on this sphree and you want to check if c is on the circlearc between a and b...
for this you can check the following:
generate a plane wich is formed from the vectors center-to-a and center-to-b
check if point c intersects with this plane (assuming the point is actually on the sphere, and not above or below it's surface)