Why does creating a relationship in pyDatalog seem to give me the wrong answer? - logic-programming

I'm new to the concepts of Datalog, and I am exploring it through pyDatalog. I am experimenting with a unit measurement converter. So, given facts about how many inches there are in a meter, for example, it should be able to tell me how many meters there are in an inch. Here is my code so far:
from pyDatalog import pyDatalog
from pyDatalog.pyDatalog import create_terms as cterms
pyDatalog.create_terms('scale') # the long way of doing it
cterms('A, B, V')
scale['meter', 'inch'] = 39.3707787
scale[A, B] = 1/ scale[B, A]
print(scale['inch', 'meter'] == V)
Wny doesn't the program convert inches to meters correctly? It should see that there are 39.37 inches in a meter, and apply the reciprocal rule to convert the other way. So it should print out 0.0234, but it actually prints out
V
----------
39.3707787

It's a bug. Correction is now done in changeset b1a5df9, and will be available in the next release. In the meantime, you may want to apply the changeset on your system.

Related

How to represent these non-standard pdes with FiPy?

I am trying to solve a set of PDEs with FiPy, but not sure how to represent the equations: see the PDEs here.
The terms give me trouble are those in red rectangle and blue rectangle. The one in red rectangle is a convection term times a function depending on x, T1 and T2; The one in blue rectangle is a transient term times another function depending on x, T1 and T2.
I couldn't find any example including this kind of terms. Could you please give me some suggestions, or share me an example?
I appreciate any help. Thanks in advance.
This question is similar to cellvariable*Diffusion in fipy, but for convection instead of diffusion. The solution is the same though. Use,
to convert the red terms into a convection term with a coefficient of f and a source term.
For the blue terms do exactly the same to get a transient term with a coefficient and a source term.
Edit: if we assume that then we can still use
and approximate explicitly (with f as a variable in FiPy and use grad). However, we can take it further and use,
and approximate the final explicitly.
Again, we can go even further with,
Again the last term can be solved explicitly. Depending on the form of f the last them should become more insignificant and thus, the explicitness less of an issue.

Can we change the default Cut-off(0.5) taken by Logistic Regression and not while calculating the classification error

We know that the work flow of logistic regression is it first gets the probability based on some equations and uses default cut-off for classification.
So, I want to know if it is possible to change the default cutoff value(0.5) to 0.75 as per my requirement. If Yes, can someone help me with the code either in R or Python or SAS. If No, can someone provide if with relevant proofs.
In my process of finding the answer for this query, i found that :-
1.) We can find the optimal cutoff value that can give best possible accuracy and build the confusion matrix accordingly :-
R code to find optimul cutoff and build confusion matrix :-
library(InformationValue)
optCutOff <- optimalCutoff(testData$ABOVE50K, predicted)[1]
confusionMatrix(testData$ABOVE50K, predicted, threshold = optCutOff)
Misclassification Error :-
misClassError(testData$ABOVE50K, predicted, threshold = optCutOff)
Note :- We see that the cutoff value is changed while calculating the confusion matrix, but not while building the model. Can someone help me with this.
Reference link :- http://r-statistics.co/Logistic-Regression-With-R.html
from sklearn.linear_model import LogisticRegression
lr=LogisticRegression()
lr.fit(x_train, y_train)
we find first use
lr.predict_proba(x_test)
to get the probability in each class, for example, first column is probability of y=0 and second column is probability of y=1.
# the probability of being y=1
prob1=lr.predict_proba(X_test)[:,1]
If we use 0.25 as the cutoff value, then we predict like below
predicted=[1 if i > 0.25 else 0 for i in prob1]

D portion of PID calculation

I am implementing PID control for motor speed control. In the specification I have been told to implement filtering technique using the following equation for D part. I am done with Implementing the PI control and it works perfectly fine for me.
Now what I understood so far is that s represents "dx/dt" generally which corresponds to the rate of change of error but here i can relate it with rate of change of feedback. Td/N is for limiting the all over gain output(hope i got this right). Now to represent this in terms of C code I tried with the following way.
s = (CurrentFeedback()-Old_Feedback)*100/(MaxFeedback()); //to calculate the % change in feedback
s = s*1000/sampleTime; //1000 is multiplied because sampleTime is in milliseconds
D = (Td*s)/(1+(s*Td/N));
D = D*KP; //Kp is multiplied as per the standard pid equation.
Old_Feedback = CurrentFeedback();
PID = P+I-D;
Well the results by adding D is not what I have predicted. I just want to know that ..did I implement the D portion equation correctly? Am I making in mistake in the understanding the basic maths of differentiation?
NOTE: I am not in liberty to change the recalculate the kp,ti,td as it comes directly from the VFD. Thanks a lot in advance. Sorry for the bad english.
Whenever the sample time is high enough and the sensibility of the measure is high the D will only add noise, because any small change will be inmediately attacked by the D control, creating an awful ripple at the end.
In most systems you don't need a D if you can live with a slower control. If you still want to use a D then be sure that it's far smaller than P and I.
And maybe you can change your last symbol.

Raise matrix to complex power

I'm implementing a library which makes use of the GSL for matrix operations. I am now at a point where I need to raise things to any power, including imaginary ones. I already have the code in place to handle negative powers of a matrix, but now I need to handle imaginary powers, since all numbers in my library are complex.
Does the GSL have facilities for doing this already, or am I about to enter loop hell trying to create an algorithm for this? I need to be able to raise not only to imaginary but also complex numbers, such as 3+2i. Having limited experience with matrices as a whole, I'm not even certain on the process for doing this by hand, much less with a computer.
Hmm I never thought the electrical engineering classes I went through would help me on here, but what do you know. So the process for raising something to a complex power is not that complex and I believe you could write something fairly easily (I am not too familiar with the library your using, but this should still work with any library that has some basic complex number functions).
First your going to need to change the number to polar coordinates (i.e 3 + 3i would become (3^2 + 3^2) ^(1/2) angle 45 degrees. Pardon the awful notation. If you are confused on the process of changing the numbers over just do some quick googling on converting from cartesian to polar.
So now that you have changed it to polar coordinates you will have some radius r at an angle a. Lets raise it to the nth power now. You will then get r^n * e^(jan).
If you need more examples on this, research the "general power rule for complex numbers." Best of luck. I hope this helps!
Just reread the question and I see you need to raise to complex as well as imaginary. Well complex and imaginary are going to be the same just with one extra step using the exponent rule. This link will quickly explain how to raise something to a complex http://boards.straightdope.com/sdmb/showthread.php?t=261399
One approach would be to compute (if possible) the logarithm of your matrix, multiply that by your (complex) exponent, and then exponentiate.
That is you could have
mat_pow( M, z) = mat_exp( z * mat_log( M));
However mat_log and even mat_exp are tricky.
In case it is still relevant to you, I have extended the capabilities of my package so that now you can raise any diagonalizable matrix to any power (including, in particular, complex powers). The name of the function is 'Matpow', and can be found in package 'powerplus'. Also, this package is for the R language, not C, but perhaps you could do your calculations in R if needed.
Edit: Version 3.0 extends capabilities to (some) non-diagonalizable matrices too.
I hope it helps!

Find cosine similarity between two arrays

I'm wondering if there is a built in function in R that can find the cosine similarity (or cosine distance) between two arrays?
Currently, I implemented my own function, but I can't help but think that R should already come with one.
These sort of questions come up all the time (for me--and as evidenced by the r-tagged SO question list--others as well):
is there a function, either in R core or in any R Package, that does x? and if so,
where can i find it among the +2000 R Packages in CRAN?
short answer: give the sos package a try when these sort of questions come up
One of the earlier answers gave cosine along with a link to its help page. This is probably exactly what the OP wants. When you look at the linked-to page you see that this function is in the lsa package.
But how would you find this function if you didn't already know which Package to look for it in?
you can always try the standard R help functions (">" below just means the R command line):
> ?<some_name>
> ??<some_name>
> *apropos*<some_name>
if these fail, then install & load the sos package, then
***findFn***
findFn is also aliased to "???", though i don't often use that because i don't think you can pass in arguments other than the function name
for the question here, try this:
> library(sos)
> findFn("cosine", maxPages=2, sortby="MaxScore")
The additional arguments passed in ("maxPages=2" and "sortby="MaxScore") just limits the number of results returned, and specifies how the results are ranked, respectively--ie, "find a function named 'cosine' or that has the term 'cosine' in the function description, only return two pages of results, and order them by descending relevance score"
The findFn call above returns a data frame with nine columns and the results as rows--rendered as HTML.
Scanning the last column, Description and Link, item (row) 21 you find:
Cosine Measures (Matrices)
this text is also a link; clicking on it takes you to the help page for that function in the Package which contains that function--in other words
using findFn, you can pretty quickly find the function you want even though you have no idea which Package it's in
It looks like a few options are already available, but I just stumbled across an idiomatic solution I like so I thought I'd add it to the list.
install.packages('proxy') # Let's be honest, you've never heard of this before.
library('proxy') # Library of similarity/dissimilarity measures for 'dist()'
dist(m, method="cosine")
Taking the comment from Jonathan Chang I wrote this function to mimic dist. No extra packages to load.
cosineDist <- function(x){
as.dist(1 - x%*%t(x)/(sqrt(rowSums(x^2) %*% t(rowSums(x^2)))))
}
Check these functions lsa::cosine(), clv::dot_product() and arules::dissimilarity()
You can also check the vegan package: http://cran.r-project.org/web/packages/vegan//index.html
The function vegdist in this package has a variety of dissimilarity (distance) functions, such as manhattan, euclidean, canberra, bray, kulczynski, jaccard, gower, altGower, morisita, horn,mountford, raup , binomial, chao or cao. Please check the .pdf in the package for a definition or consult references https://stats.stackexchange.com/a/33001/12733.
If you have a dot product matrix, you can use this function to compute the cosine similarity matrix:
get_cos = function(S){
doc_norm = apply(as.matrix(dt),1,function(x) norm(as.matrix(x),"f"))
divide_one_norm = S/doc_norm
cosine = t(divide_one_norm)/doc_norm
return (cosine)
}
Input S is the matrix of dot product. Simply, S = dt %*% t(dt), where dt is your dataset.
This function is basically to divide the dot product by the norms of vectors.
The cosine similarity is not invariant to shift. The correlation similarity maybe a better choice because fixes this problem and it is also connected to squared Euclidean distances (if data are standardized)
If you have two objects described by p-dimensional vectors of features,
x1 and x2 both of dimension p, you can compute the correlation similarity by cor(x1, x2).
Note that in statistics correlation is used as a scaled moment notion, so it is naturally thought as correlation between random variables. The cor(dataset) function will compute correlations between columns of the data matrix.
In a typical situation with a (n x p) data matrix X, with units (or objects) on its rows, and variables (or features) on its columns you can compute the correlation similarity matrix simply by computing cor on the transpose of X, and giving the result object a dist class
as.distance(cor(t(X)))
By the way you can compute correlation dissimilarity matrix the same way. The following make a distinction about the size of the angle and the orientation between objects' vectors
1 - cor(t(X))
This one doesn't care about the orientation, only size of the angle
1 - abs(cor(t(X)))

Resources