É possível criar uma function que receba como parâmetro um JSON e percorra este JSON salvando os dados em uma tabela, porém filtrando

Question: É possível criar uma function que receba como parâmetro um JSON e percorra este JSON salvando os dados em uma tabela, porém filtrando o Json.
Hoje está inserindo json direto sem filtro.
Exemplo:
Estou recebendo esse json como entrada na proc.:
    {
    "Documento": {
        "Apolice": [
            {
                "cdEmpresa": "01",
                "cdSucursal": "010",
                "cdCarteira": "431",
                "dsTipoDocumento": "A",
                "idApolice": "501741",
                "idEndosso": "000000",
                "ItemProp": [
                    {
                        "idLocal": "000001",
                        "dsEndereco": "RUA RUA RUA",
                        "dsComplemento": "CASA",
                        "dsCidade": "BELO HORIZONTE",
                        "dsUF": "MG"
                    }
                ],
                "ItemAuto": [
                    {
                        "nrItem": "000001",
                        "cdModelo": "0017664",
                        "nmMarca": "VOLKSWAGEN",
                        "nmModelo": "TRENDLINE 1.0 FLEX 12V 5P",
                        "nmTipo": "FOX",
                        "aaFabricacao": "2016",
                        "aaModelo": "2017"
                    },
                    {
                        "nrItem": "000001",
                        "cdModelo": "0017664",
                        "nmMarca": "VOLKSWAGEN",
                        "nmModelo": "TRENDLINE 1.0 FLEX 12V 5P",
                        "nmTipo": "FOX",
                        "aaFabricacao": "2016",
                        "aaModelo": "2017"
                    },
                    {
                        "nrItem": "000001",
                        "cdModelo": "0017664",
                        "nmMarca": "VOLKSWAGEN",
                        "nmModelo": "TRENDLINE 1.0 FLEX 12V 5P",
                        "nmTipo": "FOX",
                        "aaFabricacao": "2016",
                        "aaModelo": "2017"
                    },
                    {
                        "nrItem": "000001",
                        "cdModelo": "0017664",
                        "nmMarca": "VOLKSWAGEN",
                        "nmModelo": "TRENDLINE 1.0 FLEX 12V 5P",
                        "nmTipo": "FOX",
                        "aaFabricacao": "2016",
                        "aaModelo": "2017"
                    }
                ]
            }
        ],
        "Cliente": [
            {
                "cdCliente": "1",
                "dsTipoDocto": "P",
                "nrCpfCnpj": "919191919292",
                "nmCliente": "JOSE JOSE JOSE"
            }
        ],
        "EnderecoCobranca": [
            {
                "dsEndereco": "RUA RUA RUA",
                "nrEndereco": "280",
                "dsComplemento": "",
                "dsBairro": "INTEGRAÇÃO",
                "dsCidade": "PAROBE",
                "dsUF": "RS"
            }
        ],
        "Mensagem": []
    }
}

E gostaria de filtrar o Array de Apolice, no caso os campos: cdEmpresa, cdSucursal, etc..., e inserir dentro dos seus respectivos campos.
Minha Proc é :
    CREATE OR REPLACE FUNCTION schema.tabela(entrada json)
  RETURNS void as

        'INSERT INTO schema."tabela"
        SELECT * FROM json_populate_recordset
        (null::schema."tabela", $1);'

  LANGUAGE sql;

Answer: Consegui realizar um procedimento onde eu leio o arquivo Json de entrada;
Insiro em uma tabela temporaria;
Depois faço insert novamente em outra tabela inserindo somentes os campos que eu tiver no
result da query utilizando funções json Postgre.
Exemplo:
-- resultTeste onde grava o resultado do select realizado no json:
  resultTeste := (select dados ->'Documento'->'Apolice' from _teste as result);

--Insere os registro do resultTeste nos campos correspondentes do Json da table temp:
INSERT INTO _testeDados select * from json_populate_recordset(null::_testeDados,resultTeste);


API call has been blocked by CORS policy | Angular - Python

Question: I am getting this error in UI (AngularJs) side.

Access to XMLHttpRequest at https://MyLinuxServer/MyAPI/ from origin https://MyWindowsServer/ has been
  blocked by CORS policy: Response to preflight request doesn't pass
  access control check: It does not have HTTP ok status.

Deployed my AngularJS project in Windows server (MyWindowsServer) using IIS. Internally it calls APIs (Python) deployed in a Linux Box (MyLinuxServer) based on UI events (like button click).
I have tested the APIs in POSTMAN they are working fine.
The portal is launched successfully and from my local machine I am able to browse my application's dashboard (No API calls during this time) but getting this above error when ever APIs are called (UI event) from Angular side to Linux side.
Can anyone give me some clue to investigate
Comment: You have to set header Access-Control-Allow-Origin: *
Comment: Yes I have added it in the API call header @PrabhjotSinghKainth
Answer: First, a bit about CORS:

Cross-origin resource sharing (CORS) is a mechanism that allows
  restricted resources on a web page to be requested from another domain
  outside the domain from which the first resource was served.1 A web
  page may freely embed cross-origin images, stylesheets, scripts,
  iframes, and videos.[2] Certain "cross-domain" requests, notably Ajax
  requests, are forbidden by default by the same-origin security policy.

This is a security measure implemented within all browsers to prevent unauthorized websites to render the data using your API.
Although your call works on postman, the way postman communicates with your server is different from the actual browser, which implements additional security to improve the user's experience.
Now, to make it work, you can:
1) Disable CORS entirely. This is not safe and should be limited to development environments, but on the other hand, it allows you to get started with your angular app fast.
2) If you are using a domain name, you can:
a) set your current Angular app's address as a valid/permitted CORS address.
b) manually implement the OPTIONS method and set it to return an Access-Control-Allow-Origin header with the response.
My suggestion would be to go with 2a.
As for the specific implementation, please check the api docs for your python(backend) platform of choice.
Comment: Can you pls give me a hint.. how to proceed with the option 2 (a). My backend is not a Django project rather its a simple Python webservice placed in CGI-BIN folder in a linux box
Comment: This is entirely related to backend settings and you don't need to take the OS  into consideration. As for Django settings, please check the following link for the detailed answer: https://stackoverflow.com/questions/35760943/how-can-i-enable-cors-on-django-rest-framework. One last warning i want to leave here is that the browser ** tends to cache the cors settings between the requests** so you should use `Incognito` mode when configuring CORS to force browser to refetch the CORS settings with each call until you get the Django settigns right (it will take a few tries at least~).
Comment: Thanks for your suggestion, but mine is not a Django project.. its simple Python backend app placed in CGI-BIN folder.
Comment: oh sorry. I read it as `is Django` project. I have had no experience in setting the response headers in a plain python project. You have to look into how headers are built and returned by your api response and then manually add the `Access-Control-Allow-Origin: www.someurl.com`


Two ways of optimize the same function

Question: i m reading actually this tutorial about deepLearning and in particurlar about Logistic Regression.
http://deeplearning.net/tutorial/logreg.html#logreg
I don't get why it first says to optimize logistic regression taking the max Probability and after using the Log loss function ?
Sorry you can explain me the point where him will use the Argmax and where Will use the Loss ?
you would not need only 1 of this 2 ?
Answer: I'm not quite sure I understand your question, but the short answer is that the argmax is not involved in optimizing the function.
The idea is that $p(y \vert x)$ contains the probability of an item (or rather, a minibatch of items) being assigned to each of the possible classes. If you want to make a prediction, you would choose the class with the highest probability, as in:
self.y_pred = T.argmax(self.p_y_given_x, axis=1)

On the other hand, to update the model parameters, the example is performing stochastic gradient descent on the log loss, which effectively tries to maximize the probability of the true label for each item. This is defined in the function:
def negative_log_likelihood(self, y):
    return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]), y])

where y is used to select the probability of the true label for each item from p_y_given_x.
It may look to you like the argmax is being computed before the log likelihood, but this is not the case. The key thing to understand is that the lines above are defining operations on symbolic variables in theano, and they will not be actually used until needed. In this case, the optimization for a minibatch happens when the train_model function is called:
minibatch_avg_cost = train_model(minibatch_index)

train_model is a theano function, defined as:
train_model = theano.function(
    inputs=[index],
    outputs=cost,
    updates=updates,
    givens={
        x: train_set_x[index * batch_size: (index + 1) * batch_size],
        y: train_set_y[index * batch_size: (index + 1) * batch_size]
    }
)

This definition tells you that when it is called, it will update the parameters according to:
updates = [(classifier.W, classifier.W - learning_rate * g_W),
           (classifier.b, classifier.b - learning_rate * g_b)]

classifier.W and classifier.b are the relevant model parameters. g_W and g_b in turn are gradients which are computed automatically by the lines:
g_W = T.grad(cost=cost, wrt=classifier.W)
g_b = T.grad(cost=cost, wrt=classifier.b)

Finally, these gradients are computed using the cost function:
cost = classifier.negative_log_likelihood(y)

which is the symbolic operation defined above. In other words, the argmax is not used in updating the model parameters. That argmax function is only called when the model needs to make a prediction, as in the predict_model function or the errors function, which compares $y$ to $\hat y$.
Comment: @T-student No. Remember, this is classification, not regression. b is a vector. x*W + b would give you a vector of unnormalized scores for each class (of which there are 10 in this example, one for each of the 10 digits). To make a prediction you would choose the class with the highest score.
Comment: Welcome Dallas. I have edited your post to display some of the math as math. You should check that I have not introduced any errors.
Comment: actually after you learn the parameters you should have an equation  y = parameters * variabiles + b. after you have calculated the parameters, you should have to substitute just the variabiles of the New sample , multiply and have as result Y .   Now at this point why you need Argmax ? i dont get it !
Comment: I'm still not sure I understand your confusion. Are you talking about the line "self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b)".  As I explained above, this computes the probability of x being assigned to each of the classes. Remember, though, that this is multi-class classification, so there 10 possible classes (the 10 digits in MNIST). You would need to take the argmax in order to determine which of the 10 classes has the highest probability. If I am still misunderstanding your question, perhaps you could point to a specific part of the code that is confusing you?
Comment: @DallasCard my point is very simple ... T.nnet.softmax(T.dot(input, self.W) + self.b)  learn for you W and b is this correct? after you do... x*W(matrix) + b = vector Y . Vector Y is already the result of the prediction right?
Comment: ok i understand

Two ways of optimize the same function


Do I have a really low voice, or does every pop singer have a really high register

Question: I had a tendency to sing most pop songs an octave lower, but now that I noticed and started singing them properly, I have noticed I cannot reach most pop songs at all, at least without falsetto. Like, the intro to Reptilia by The Strokes, which most people would say sounds low, takes me to the very edge of what my chest voice is capable of. In general, I've realized I can't reach most pop songs, and that there is no modern artists or songs that ever go to my lower registry, or in fact any place where I can sing comfortably.
I am really comfortable singing stuff in the correct pitch for things like Bill Withers and Hendrix, so I am wondering if my vocal range just plain sucks for  pop music, or if I need to work on it and if I can bring it up.
Comment: Go to a piano, and match your singing to notes played to find your range, then look it up on the many charts available. That way, you'll get a far more accurate answer than asking here.
Comment: The song you have given here reaches a D4, which is a high note for a (non professional) singer in the bass register. As Tim said, try to find out your range, this will tell you what voice type you are.
Comment: Why would you need to 'work on it' to bring your voice up to 'pop music range' (whatever that is!)? Stay as you are, use what you've been given, and plough your own furrow. There are many, many singers who are doing just that, successfully. If you need to sing existing pop songs, do like a lot of others , and *transpose*.
Comment: See how you get on with something like Chris Isaak's Wicked Game. Starts nice & low with a bit of a stretch to the chorus; though it's not as high as it gives the impression of being.
Answer: I'll try to answer your question slightly differently maybe, but I hope it will get to the point:

Your natural voice height is determined by the size of your vocal cords (the organ in your body primarily responsible for the voice height). The only way how to know your voice height is to visit a specialist who will measure the size.

There are (slight simplification) two voice positions based on the resonance: the chest voice and the head voice. To reach d'' you definitely need the head voice (basically regardless of your vocal cords size), which is tricky for most untrained guys after puberty as guys simply tend to use the chest voice that's deeper. Gals tend to stick to the head voice and thus speak and sing higher. Also, their cords are usually smaller as well, meaning their natural position is higher than in guys, for both the chest voice and the head voice.

The range you can get (within the chest voice and within the head voice) depends on the state of your vocal cords -- if you train them for a wider range, you can usually achieve that, the muscles can be trained as any other muscles. However, if you overdo it, you can damage them and then your voice will be in various states of broken. Also, training is easier when you're young and harder when you're old, just like any other training.

So to answer the question: You probably have just a very normal "guy" voice, and many singers work hard to extend their range quite outside of what you can do.
Answer: I'm not an expert on pop music, but my impression is that pop music has an abundance of (a) tenors and (b) falsetto (in male singers).
Maybe it's time to get to know Johnny Cash's oeuvre better, so your voice will sound more normal to your ears.


Is there an exact or recursive representation for the number of ways one can draw colored balls without putting them back?

Question: Example
There are ten balls in an urn, six are red, three black and one white. $n$ balls are drawn from the urn without putting them back (of course, $n \le 10$). How many different permutations of the three colors (all balls of one color are indistinguishable) exist when not putting the balls back into the urn?
For $n = 1$, the result set $\Omega$ stays simple: $\Omega_1 = \{r;b;w\}$. The answer is $|\Omega_1| = 3$.
For $n = 2$, the result set looks like this: $\Omega_2 = \{rr;rb;rw;br;bb;bw;wr;wb\}$. The answer is $|\Omega_2| = 8$ and not $3^n = 3^2 = 9$ as expected when the balls would have been put back, because the white ball can not be drawn a second time.
For $n = 3$, we easily recognize how this problem can be calculated recursively. If a red or black ball is drawn as the first one, the remaining two have all the possibilities as in $\Omega_2$. If a white ball is drawn, the remaining two balls can not be white: $\{wrr;wrb;wbr;wbb\}$. This set is a subset of $\Omega_3$. The answer is $|\Omega_3| = 2*|\Omega_2| + 4$.
However, at $n = 4$ or $n = 5$, the black balls are also exceeded in some results which has to be taken into consideration when trying to calculate the number of results.
Generalizing
We are drawing $n$ balls out of an urn containing $b$ balls, without putting them back. There are $k$ different kinds of balls ($k$ different colors). For each color, there are $b_i$ balls in the urn: $b_1 + b_2 + \dots + b_k = b$.
This problem is very similar to this question, but "red-black" is a different result than "black-red", so one can not use the stars and bars method; knowing that we've drawn 3 red balls just isn't enough.
Is there an exact or recursive representation of $|\Omega_n|$, either for the example above or general $k$, $b$ and $b_1, b_2, \dots, b_k$ ?
If needed, a few conditions, or piecewise terms would be fine, but the formula would be prettier without them, of course.
Edit:
The term "distinguishable permutation" (thanks to Cameron Buie) or "multiset permutation" is a great way to determine the answer for $n = 10$: $\textit{840}$. But wikipedia, besides mentioning the "k-permutation of a multiset" meaning a limited multiset permutation with only k draws, does not show a mathematical formula.
Comment: Look up "distinguishable permutations" on wikipedia (or on this site).
Answer: The coefficient of $x^n$ in
$$n!\prod_{i=1}^{k}\left(\sum_{j=0}^{b_i}\frac{x^j}{j!}\right)$$
To use your example of $6,3,1$ balls:
$$n!\left(\frac{x^0}{0!} + \frac{x^1}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!} + \frac{x^4}{4!} + \frac{x^5}{5!} + \frac{x^6}{6!}\right)\left(\frac{x^0}{0!} + \frac{x^1}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!}\right)\left(\frac{x^0}{0!} + \frac{x^1}{1!}\right)$$
Expanded:
$$n!\left(1 + 3x+ 4 x^2+\frac{10 x^3}{3}+\frac{47 x^4}{24}+\frac{101 x^5}{120}+\frac{11 x^6}{40}+\frac{17 x^7}{240}+\frac{7 x^8}{480}+\frac{x^9}{432} + \frac{x^{10}}{4320}\right)$$
There are $n!$ ways to arrange the $n$ balls that are drawn, but if we draw $j$ balls of any particular color, we must divide by $j!$ to render them indistinguishable amongst themselves.
Another way to solve this is with dynamic programming/memoization (Python script below):
<code>@memoize
def draw(n, balls):
    if n==0:
        return 1
    total = 0
    for i in xrange(len(balls)):
        if balls[i] > 0:
            newBalls = balls[:]
            newBalls[i]-=1
            total += draw(n-1, newBalls)
    return total

balls = [6, 3, 1]
b = sum(balls)
for n in xrange(0, b+1):
    print "n =", n, "ways =", draw(n, balls)
</code>
A quick table of results:
<code>n = 0 ways = 1
n = 1 ways = 3
n = 2 ways = 8
n = 3 ways = 20
n = 4 ways = 47
n = 5 ways = 101
n = 6 ways = 198
n = 7 ways = 357
n = 8 ways = 588
n = 9 ways = 840
n = 10 ways = 840
</code>
Comment: Thank you for the script; it's quite similar (in the way it operates) to what I would have thought of. Can you clarify what the x in your equations means, possibly by a full calculation for any specific n to show it matches the output of the script?
Comment: Edited in an expanded version. For any particular $n$, just take the coefficient of $x^n$ and multiply it by $n!$. The $x$ itself doesn't matter (this is a generating function) -- the idea is that the coefficients of the polynomial accumulate / collect the terms corresponding to all choices that could possibly lead to it.
Comment: I see! That's why the coefficient of $x^{10}$ is exactly one tenth of $x^9$. Because the result of $10!$ is exactly ten times $9!$. Thank you, this is such a clever solution.


Windows CLI: Built-In Utility to list open Ports in XP / Vista / 7

Question: I need a utility built into several versions of Windows (XP to Win7 specifically) that can tell me if a particular port is in use. I've found a few different ways for each OS, but I'm looking for a single command that works across all of them.
Comment: There is not one tool, just a bunch of well-documented commands that have been around forever. What EXACTLY are you trying to do?
Comment: by the way: i have seen this thread http://serverfault.com/questions/35218/in-windows-using-the-command-line-how-do-you-check-if-a-port-is-open, but it does not answer my queestion ;-)
Comment: I want to see if a port is in use or not. and it must work on all windows system mentioned
Answer: Perhaps Netstat? netstat -h will give options.
Comment: Ok, netstat is also an option thanks a lot! Somebody who knows about windows could confirm if it is present in all the windows systems? Thanks
Comment: It's kind of a standard tool from the earlier days of UNIX, so I'd be surprised if it didn't go back to NT 3.x http://en.wikipedia.org/wiki/Netstat
Comment: `netstat -ab` will dump a list of binaries and their ports.  I'm sure there are gui versions of netstat, but I've no experience with any.
Comment: @dmeu, `netstat` is available on every version of Windows since at least NT4/Win95.
Comment: netstat was also there in 3.5 but prtqry is more specific
Answer: If you use netstat -n it will tell you the current in use ports. The -n tells it not to try to resolve hostnames from IPs.  You could also run it with -a to have it TRY to resolve hostnames.
Comment: also very correct! but i can only select one answer as correct one... :-(
Answer: netstat -bona

This will link ports to processes. Very useful if you want to know what process is running on port X.
For easy reviewing I redirect the output to a file
netstat -bona > file.txt


How to write Junit test cases for spring boot application?

Question: I have to write some junit test cases to check entity. I'm using postgres as my database.
My entity class
@Entity
@Table(name = "display")
public class Display {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String group;

public Display() {

}
public Display(Long id, String title, String grp) {
    this.id = id;
    this.title= title;
    this.group= grp;
}

public void setId(Long id) {
    this.id = id;
}

public Long getId() {
    return this.id;
}

public void setGroup(String id) {
    this.group = id;
}

public String getGroup() {
    return this.group;
}

public void settitle(String title) {
    this.title = title;
}

public String gettitle() {
    return this.title;
}

}

My repository
@Repository
public interface DisplayRepository extends CrudRepository<Display, Long> {

}

Interface
public interface IDisplayService {
    List<Display> findAll();
}

Service class
@Service
public class DisplayService implements IDisplayService {
    @Autowired
    private DisplayRepository repository;

@Override
public List<Display> findAll() {
    List<Display> d = (List<Display>) repository.findAll();
    return d;
}
}

I tried writing junit test cases but I get Could'nt load Application. Whats the right way to write junit test cases for this?
This is the test case I wrote for service
folder : test/java/example/demo/Test.java
@RunWith(MockitoJUnitRunner.class)
@TestPropertySource("classpath:conn.properties")

public class DisplayServiceTest {

@Value("${id}")
private String value;

@Mock
private DisplayRepository DisplayReps;

@InjectMocks
private DisplayService DisplayService;

@Test
public void whenFindAll_thenReturnProductList() {

Menu m = new Menu()
m.setId(value);

    List<Display> expectedDisplay = Arrays.asList(m);
    doReturn(expectedDisplay).when(DisplayReps).findAll();
    List<Display> actualDisplay = DisplayService.findAll();
    assertThat(actualDisplay).isEqualTo(expectedDisplay);
}

in test/java/example/demo/resources
    conn.properties
    id=2
Its returning 0 for value
Whats the issue?
Thanks
Comment: Can you post the test class you created? Did you annotated the class to run with SpringRunner? You may also want to add @SpringBootTest. Look also here for a tutorial https://www.baeldung.com/spring-boot-testing
Comment: I have updated my post with test case
Comment: Use HSQL or H2 DB as Proxy DB
Comment: The DisplayRepository stores `Menus`. Please post that class.
Comment: Its display class itself not Menus. Can someone write a demo test class and explain?
Comment: Did you try to remove AutoConfigureTestDatabase and run the test, again?
Answer: I have managed to make your code to work. I will post only the changed classes:
The interface:
public interface DisplayRepository extends CrudRepository<Display, Long> {

   Optional<Display> findByTitle(String name);
}

The test class:
@RunWith(SpringRunner.class)
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
@DataJpaTest
public class DisplayRepositoryTest {

@Autowired
private TestEntityManager testEntityManager;

@Autowired
private DisplayRepository productRespository;

@Before()
public void setUp(){

    Display m = new Display();
    // m.setId(2L); // The ID is autogenerated; can retrieve it from the persistAndFlush result
    m.setCategory("Group1");
    m.setTitle("Product2");

    testEntityManager.persistAndFlush(m);
}

@Test
public void whenFindByName_thenReturnProduct() {
    // when
    Display product = productRespository.findByTitle("Product2").orElseThrow(() -> new RuntimeException("Product not found"));

    // then
    assertThat(product.getTitle()).isEqualTo("Product2");
}

@Test
public void whenFindAll_thenReturnProductList() {
    // when
    List<Display> products = (List<Display>) productRespository.findAll();

    // then
    assertThat(products).hasSize(1);
}
}

When trying to run the code you provided, there were a few issues:

you were using the reserved word group as a field in the Display class. Because of this, Hibernate couldn't create the table, so I renamed it to category.
there was a compilation issue because the method findByName wasn't defined in the repository; also, there was no field name in the Display class to which the mapping to be made; because of this, I've added the method findByTitle because it's an existing field and it seemed to match the value you queried in the test method.
because the ID field is autogenerated, the test setup() failed when persisting the Display.

If you want to use @Mock for mocking classes, you must call:
@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
}

You can then mock responses as usual: Mockito.when(DisplayReps.findByTitle("A")).thenReturn(Optional.of(new Display(2L, "ALFA", "GRP1")));
Comment: I tried this but now I'm getting SQL error telling ERROR: column "category" is of  type integer but expression is of type character varying. but category is of string value only. I dont know why it's showing integer.
Comment: I've tested with a H2 DB. Can you switch to it to see if the code runs? In case you are using an existing DB instance, maybe you should drop the table for the Display and try again or configure Hibernate to drop the tables `hibernate.hbm2ddl.auto=create` (only while running this tests).
Comment: Hey I have updated the unit test. Can you tell me whats wrong with that? If I manually enter id as 2 it will work but through properties file it wont work
Comment: You should run the test class with `@RunWith(SpringRunner.class)`. The way you had it set, Spring didn't try to load it's context and the config was ignored.
Comment: I've edited the response. Please mark the question as answered because you are starting to ask other thing unrelated to the original question and you should make new questions for them.


Expected Value Inequality - bounded by sums of probabilitys

Question: I have to show the following inequality:
$$ \sum_{k=0}^{\infty} P[Y>k] \leq E[Y] \leq \sum_{k=1}^{\infty} P[Y>k]$$
under the condition that Y is a non negative continuous random Variable.
My first idea was to rewrite the inequality using integrals, but then i end up having to integrate over a sum, which is nothing im particulary excited about.
Any Ideas on how i can do this?
Comment: This has been asked many times. See both the continuous and discrete cases in http://math.stackexchange.com/questions/1795529/why-is-mathbbex-1-sum-infty-k-1-mathbbpx-k-true
Comment: Also: http://math.stackexchange.com/q/64186/9464
Answer: Hint: If $Y$ is nonnegative, then
$$E[Y] = \int Y \, dP = \int \int_0^\infty I\{x < Y\} \, dx \, dP = \int_0^\infty \int I\{x < Y\} \, dP \, dx = \int_0^\infty P(Y > x) \, dx$$
Now apply the standard comparison between integrals and sums.
Comment: are you - by any chance - talking about this one: $ \sum_{i \in I} f(i) = \int_I {f(x) #dx} $
Comment: No, that is not a valid equation. I am talking about a comparison between Riemann-integral and infinite sum.


Predicate function exists()

Question: Using the exists(expression) one can check for a property or path in a graph database. Looking at the example from the docs. You can use exists on properties as in exists(n.surname) and also on path. But from the example in the docs
SELECT * FROM cypher('graph_name', $$
 MATCH (n)
 WHERE exists(n)-[]-(name: 'Willem Defoe')
 RETURN n.full_name
$$) as (full_name agtype);

I'd expect the query to be
SELECT * FROM cypher('graph_name', $$
 MATCH (n)
 WHERE exists((n)-[]-(name: 'Willem Defoe'))
 RETURN n.full_name
$$) as (full_name agtype);

Since the syntax is exists(path). Also since the expression name: 'Willem Defoe' is a property I'd also expect that to be in curly braces thus the final query looks like this
SELECT * FROM cypher('graph_name', $$
 MATCH (n)
 WHERE exists((n)-[]-({name: 'Willem Defoe'}))
 RETURN n.full_name
$$) as (full_name agtype);

Or am I missing something?
Answer: Yes, you are right. The query given in the documentation has a syntax error and the correct query as stated would be:
SELECT * FROM cypher('graph_name', $$
MATCH (n)
WHERE exists((n)-[]-({name: 'Willem Defoe'}))
RETURN n.full_name
$$) as (full_name agtype);

You can raise an issue here on GitHub.
Answer: The correction that you have suggested is how it should be written. If you run the first query, it will produce an error that says:
ERROR:  syntax error at or near ")"
LINE 3:  WHERE exists(n)-[]-(name: 'Willem Defoe')
                       ^

Similarly, the second query produces an error that says:
ERROR:  syntax error at or near "'Willem Defoe'"
LINE 3:  WHERE exists((n)-[]-(name: 'Willem Defoe'))
                                    ^

Only the third query successfully parses and produces a proper output.
Answer: You are absolutely correct, the documentation surely has syntax error and the query you suggested should be the correct one.
Answer: You are right. EXISTS clause take argument property which is a vertex or a path so, in this example it should be like this
WHERE exists((n)-[]-({name: 'Willem Defoe'}))

Predicate function exists()


Two True Positive for one ground truth in object detection

Question: I am wondering is it possible to have two true positive predictions for one bounding box ground truth only. Following this section from Stanford. They define truth positive like this:

We start with IoU to decide if each prediction is correct or not. For
a ground truth object and nearby prediction, if

the predicted class matches the actual class, and
the IoU is greater than a threshold,

we say that the network got that prediction right (true positive).
Otherwise, the prediction is a false positive.

Now, take an example, in the image below, the red box is ground truth and the black box is prediction. Assume both black boxes predict correctly in the classification task, and both yellow areas are bigger than IoU threshold. In this case, both of them satisfy the condition to be true positive, then the true positive is not "true" anymore?
Comment: You could avoid the issue if the intersection-over-union threshold is over $\frac12$
Comment: @Henry, thank for your answer. However, I think there is a chance that two bounding boxes with the same ground truth but their overlap is not enough to remove one of them by non-max-supression
Answer: As logical it seems to me, I would opt for calculating one true positive per ground truth. That is, if you have a ground_truth array and a prediction array, to care only about the corresponding red bounding box of these two arrays:
if IoU(ground_truth[red_bbox], prediction[red_bbox]) >= 0.5:
    true_pos = true_pos + 1

So, the yellow area in this case will be > 0.5 of the bbox in ground truth, meaning a correct detection.
Now, if the prediction black bounding boxes are extending far away from the ground truth bbox, then again it is correctly detected even though the algorithm will have so many false positive pixels.
Answer:
Note that a single ground-truth box may assign positive labels to
multiple anchors. Usually the second condition is sufficient to
determine the positive samples; but we still adopt the first condition
for the reason that in some rare cases the second condition may find
no positive sample. We assign a negative label to a non-positive
anchor if its IoU ratio is lower than 0.3 for all ground-truth boxes.
Anchors that are neither positive nor negative do not contribute to
the training objective. (Ren, et al., 2015)

There might be different approaches, but one of the hallmark papers does not shy away from using multiple positives from the same ground truth. Since this behavior will transfer over to inference, people use non-maximum suppression to remove predictions likely to cover the same object. You can tune the threshold used to define true positives and the threshold used to perform non-maximum suppression independently. This technique would not work if the boxes weren't touching. For that reason, let's reformulate the question: how small must our ground truth threshold be for this to occur?
To simplify the problem, let's assume three things.

The boxes are the same size.
They overlap diagonally.
They overlap symmetrically.

For the two outer boxes to be considered ground truth, we need that
$$\mathrm{IoU}(B_1, B_2) = \frac{1 - 2 x + x^2}{1 + 2 x - x^2} \geq t$$ where $x$ denotes the offset between each outer box and the inner box, and $t$ the ground truth threshold. The boxes will stop overlapping when their corners meet in the middle, leading to the additional constraint
$$2x \geq 1$$ that we can use to solve for $t$. We get that $t \leq \frac{1}{7}$ can lead to this scenario, but the threshold is usually set to a greater value. For example, the paper above uses $t \geq \frac{1}{2}$. If use this value instead, we get
$$\mathrm{IoU}(B_1, B_3) = \frac{-4x^2 +4x-1}{4x^2-4x-1} \geq \frac{1}{71} (24 \sqrt{6} - 41) \approx 0.25.$$


Como comparar valor de variável com operador de incremento?

Question: Segue a função:
$conteudo = $textodobanco;
function addBanner($i) {
    static $conta1 = 0;
    $retorno = $i[0];
    if (++$conta1 == 5) {
        $retorno .= 'BANNER 1';
    }
    return $retorno;
}

$txt = preg_replace_callback('#(
)#', 'addBanner', $conteudo);

A função acima funciona perfeitamente, porém, preciso que ao invés de utilizar o número 5 expresso na comparação, que ele seja substituído por uma variável, para assim dinamizar o sistema que estou desenvolvendo.
Porém, todas as tentativas como por exemplo abaixo em substituir o número 5 por uma variável não funcionou, ou seja, a função não retorna resultado algum.
Substituição pela variável, porém, sem sucesso
function addBanner($i) {
    static $conta1 = 0;
    $retorno = $i[0];
    if (++$conta1 == $numerovar) {
        $retorno .= 'BANNER 1';
    }
    return $retorno;
}

Comment: O enunciado não tem relação com o problema. A comparação do seu código está correta, o que não está é a falta de uma passagem ou criação da variável (você compara com `$numerovar` mas não passa essa variável para a função de nenhuma forma). Seria importante [edit] reduzir o código a um [mcve] se quiser mais detalhes. A resposta abaixo explica as soluções ideais (independente do valor vir do DB ou não, o caminho é o mesmo). - Fundamental estudar o manual do PHP em relação ao [escopo das variáveis](https://www.php.net/manual/pt_BR/language.variables.scope.php).
Comment: @rray como mostrei no primeiro exemplo, o número 5 está expresso. Preciso colocar uma variável no lugar dele. $i é outra coisa.
Comment: Na função informe dois parâmetros, depois do `$i` pode chamar um `$limite` pode definir uma valor padrão para ele. ex: `function addBanner($i, $limite=3)`
Comment: @Gladison a variável que irá utilizar para realizar a comparação está gravado no banco?? já verificou o que está sendo retornado?
Comment: @Oliveira Sim, está vindo do banco de dados e verifiquei o valor retornado, no caso, 5. Porém, esta função não funciona se eu substituir o número cinco pela variável. Não sei porque isso acontece.
Answer: Para fazer a comparação com uma variável primeiro você precisa ou cria-la ou passar ela como Parâmetro...
Segue um exemplo passando ela como Parâmetro:
function addBanner($i, $numerovar)
{
   static $conta1 = 0;
   $retorno = $i[0];
   if(++$conta1 == $numerovar)
   {
      $retorno .= 'BANNER 1';
   }
   return $retorno;
}

Segue um exemplo Criando ela:
function addBanner($i)
{
   static $conta1 = 0;
   $retorno = $i[0];
   $numerovar = 10;
   if(++$conta1 == $numerovar)
   {
      $retorno .= 'BANNER 1';
   }
   return $retorno;
}

Comment: Caro Marccus a sua ideia esta quase certa, se eu entendi direito, mas o código do AP é com `preg_replace_callback`, da forma que esta não funciona, uma sugestão seria usar uma função anonima, exemplo: https://gist.github.com/brcontainer/f85ff8dc606840d77803b04cabc2b24e (cc @Gladison veja se consegue entender) (código atualizado no gist, faltava um detalhe)
Comment: Infelizmente não funcionou. A minha variável `$numerovar` recebe um número do banco de dados. Não posso colocar na função um número pré-definido.
Comment: No seu primeiro exemplo eu atribuí a variável `$num` um valor do banco e nada.
Comment: Conseguindo alguma coisa?
Comment: @GuilhermeNascimento Perfeito, funcionou!!! Muito obrigado.


Number of regions in the planar embedding of a connected, $3$-regular graph with $24$ vertices

Question:
Let $G$ be a connected, $3$-regular, and planar graph with $24$ vertices. How many regions are in a planar embedding of $G$?

Any help to solve this problem would be highly appreciated. Thanks in advance!
Comment: Your approach here would be to use Eulers formula I believe. That is $V - E + F =2$. Remember to look at the outside region as another face.
Comment: As you're told the number of vertices, "3-regular" lets you work out the number of edges.
Comment: Yes, using Euler's formula, I have $F=3$, for $K_{2,3}$ and from that, $G$ has $14$ planar regions. Thank you both for your hints!
Comment: You could write that up, and post it as an answer, Anik.
Comment: Today would be a good day to do that, to help the next person who comes along with a similar question.
Comment: Making progress, Anik?
Comment: Yeah, I just did. I hope it's right @GerryMyerson
Answer: By Euler's formula, we know that
$V-E+F=2$
For $K_{2,3}$, $V=5$, $E=6$. Hence,
$5-6+F=2$, which means $F=3$
Now $G$ is $3$-regular. Hence $d(v) = 3 \\ \forall v \in V$
Now $2|E| = \sum_{v \in V}d(v) =3 \times24 \implies |E|=36$
Here, $E=36, V=24$
By Euler's formula, $F=2-V+E=2-24+36=14$
Comment: Well, if you cut it out, and just left the part starting with "Now $G$ is $3$-regular," wouldn't that be a complete answer to the question as posted?
Comment: Good! But the part about $K_{2,3}$ seems irrelevant.
Comment: @GerryMyerson and why is that?


Determining the multiplicative group of a ring of polynomials

Question: Let us say that we have the polynomial ring R[x]. Would it be possible to determine the order of the multiplicative group of R[x] modulo a polynomial f?
Answer: There is an equivalent of Euler's theorem for polynomials:

Theorem: (Euler's Theorem for Polynomials).
Let $m \in K[x]$, where $K$ is a finite field, and let
$$\varphi(m) = \#\{f \in K[x] : 0 <= deg(f)< deg(m) \text{ and } gcd(f, m) = 1\}$$
Where here $\#(A)$ denotes the cardinal of the set $A$ up to a multiplication by a constant ($f$ and $af$ with $a\in K$ are counted only one time). Then for any $f \in K[x]$ with $gcd(f, m) = 1$,
$$f^{\varphi(m)}= 1 (\mod m)$$

see this document for more information.
Comment: I corrected it, maybe if you look in the internet for "Euler's Theorem for Polynomials" you can find better than this document


Show that $\overline{f(\overline{z})}$ is holomorphic on the domain $D^*:=\{\overline z: z\in D\}$ using Cauchy Riemann equation.

Question: Please do not vote to close it as I want to find errors in my proof, which cannot be rectified on previously answered question.
I want a different proof using Cauchy Riemann equation.
Let $D\subset \mathbb C$ be a domain and suppose f is holomorphic on $D$.
Show that $\overline{f(\overline{z})}$ is holomorphic on the domain $D^*:=\{\overline z: z\in D\}$.
Attempt:
let $z= x+i y$ and $f(z)=u(x,y)+iv(x,y)$
$f$ is holomorphic on $D \Rightarrow u_x=v_y$ and $u_y=-v_x$
To show: $\overline{f(\overline{z})}$ is holomorphic on the domain $D^*$
Let $w\in D^* \Rightarrow w=\overline z$ for some $z \in D$
To show: $\overline{f(\overline{w})}$ satisfy Cauchy Riemann equation.
i.e. To Show: $\overline{f({z})}$ satisfy Cauchy Riemann equation.
$\overline{f({z})}= u(x,y)-iv(x,y)$
Let $v_1=-v$
$\overline{f({z})}= u(x,y)+iv_1(x,y)$
i.e. To show: $u_x={v_1}_y$ and $u_y=-{v_1}_x$
But $-v_y={v_1}_y$ and $-v_x=-{v_1}_x$
$\Rightarrow u_x=-v_y$ and $u_y=v_x$
which is not what I want.
Where I go wrong ?
Comment: @NajibIdrissi, Can you please help me in finding error in this.
Comment: Please do not vote to close it as I want to find errors in my proof, which cannot be rectified on previously answered question.
Comment: @Najibldrissi, I have doubt in that solution too, can you help me.
Comment: It seems the discussion of the domain $D$ or $D^* = \overline D$ is a bit confused.  As the original Question would seem to suggest, the point is that if $f(z)$ is holomorphic on $D$, then $\overline{f(\overline z)}$ is also holomorphic on $D$.
Answer: Your problem: $f(\bar z) = u(x,-y) + i v(x,-y)$.
Alternative solution: use that $f$ is locally a power series.
EDIT:
$$f(z) = f(x+iy) = u(x,y) + i v(x,y),$$
$$
\tilde u(x,y) + i\tilde v(x,y) = \tilde f(z) =
\overline{f(\overline z)} = u(x,-y) - i v(x,-y),
$$
$$\eqalign{
   \frac{\partial\tilde u}{\partial x}(x,y) = \frac{\partial u}{\partial x}(x,-y)\frac{\partial x}{\partial x} = \frac{\partial u}{\partial x}(x,-y)
&= \frac{\partial v}{\partial y}(x,-y) = \frac{\partial(-v)}{\partial y}(x,-y)\frac{\partial(-y)}{\partial y} = \frac{\partial\tilde v}{\partial y}(x,y),\cr
   \frac{\partial\tilde u}{\partial y}(x,y) = \frac{\partial u}{\partial y}(x,-y)\frac{\partial(-y)}{\partial y} = -\frac{\partial u}{\partial y}(x,-y)
&= \frac{\partial v}{\partial x}(x,-y) = -\frac{\partial(-v)}{\partial x}(x,-y)\frac{\partial x}{\partial x} = -\frac{\partial\tilde v}{\partial x}(x,y).
}$$
Comment: @Human, exactly! Using a new name for $\bar z$ does not fix anything.
Comment: @Human, the function $z\mapsto\overline{f(z)}$ isn't holomorphic. The function $w\mapsto\overline{f(\overline w)}$ is holomorphic.
Comment: @Human, do you mean $\overline z$?
Comment: @Human, the function **is** $w\mapsto \overline{f(\overline{w})}$.  $z\mapsto \overline{f({z})}$ is **another** function.
Comment: @Human, exactly. $D^*$ is simply the *domain*.
Comment: @Human, see the detailed calculations.
Comment: Thank you so much, got it where I was going wrong.
Answer: We have to prove that the function
$$g(w):=\overline{f(\bar w)}$$
is holomorphic on $D^*$. To this end fix a point $w\in D^*$ and consider a variable complex increment vector $W$ attached at $w$. Then
$$g(w+W)-g(w)=\overline{f(\bar w+\bar W)-f(\bar w)}=\overline{f'(\bar w)\bar W+o(|\bar W|)}\qquad(W\to0)\ .$$
It follows that
$$g(w+W)-g(w)=\overline{f'(\bar w)}\>W+o(|W|)\qquad(W\to0)\ ,$$
and this shows that $g$ is complex differentiable at $w$ with $g'(w)=\overline{f'(\bar w)}$.


Slide down modal navigation

Question: I have a UIViewController which contains a button. On the click of the button, it transitions as a modal (slide up transition) to a TabViewController which has two tab items.
On both the tab items, I have a Close button on which I want to transition back to the UIViewController mentioned above but in a slide-down transition.
How can I do that?
Answer: Let us say, you have button close target as dismissMe,
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 100, 75);
button.titleLabel.text = @"Close";
[button setBackgroundColor:[UIColor greenColor]];
button.center = self.view.center;
[button addTarget:self action:@selector(dismissMe) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

then please add the following code to target method.
-(void)dismissMe {
    [self dismissViewControllerAnimated:YES completion:nil];
}

Also you can use this instead
[self.parentViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

Answer: From the view controllers with the close button, call this when the button's UIControlEventTouchUpInside event is raised:
[self.parentViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

Slide down modal navigation


Formulate mathematical equation

Question: I have a function:
$$f(n) = 1 - \left(\frac{1}{n}\right)$$
I want to add another variable $k$ to this equation such that:
if $k=n$ , then $f(n,k)=1$
if $n=1$, then $f(n,k)=0$
How can we do that ?
Answer: Consider the function
$g(n,k) = 1 - \frac{1}{n} + \frac{1}{k}$
Then if $k = n$, you have $g(n,k) = 1$. (Note that $n$ and $k$ can not be equal to $0$.)

As Ross Millikan suggested, for your new question, you can consider the function:
$$h(n,k) = \begin{cases}
g(n,k) & \quad n \neq 1 \\
0 & \quad n = 1
\end{cases}$$
$h$ is a perfectly good function and satisfy the property that $n \neq 1$ and $k = n$, then $h(n,k) = 1$; and if $n = 1$, then for all $k$, $h(n,k) = 0$.
Comment: I am sorry I did not notice your answer, I've changed the question. I need also if n=1 , then f(n,k) = 0
Comment: @Mohammed:  What is wrong with if n=1, f(n,k)=0, else what William proposed?
Comment: if n=1 and k=50 for example , it do not return 0
Comment: @Mohammed: my if statement will solve that.  Are $n$ and $k$   naturals or reals? +1 for William
Answer: For the first part, a key point is that if $ab=0$, then $a=0$ or $b=0$. So this suggests that whatever you add on should be multiplied by what you already have, since the equation as you give it solves $f(1,k)=0$. Multiplying all that by something won't change the result.
Similarly, for the second part, what simple relationship gives $1$ when two numbers are equal? There's a basic operation that does exactly what you want for that part. Stick them together and you should have a suitable solution.
Answer: Assuming that the following statement will never be true - n=k=1 - you can also do this without an "if" statement. Try $(1-\frac{1}{n})^{n-k}$. If n=1, then the fraction $\frac{1}{n}$ will be $\frac{1}{1}=1$, and $1-1=0$. $0^{anything} = 0$. The other issue is if $n=k$. In this case, $n-k = 0$ and $anything^0 = 1$.
I think this equation is also good because at $f(1,1)$, your constraints have given both of the values $0$ and $1$, so it is undefined at $f(1,1)$. At $f(1,1)$, my equation gives you $0^0$, which is undefined.
Comment: your function works well , but It did not return a value between [0,1]
Comment: @Mohammed None of these answers do, if $n,k \in \mathbb{R}$.For the other answer, take n=.00001.
Comment: You should specify that constraint in your question.

Formulate mathematical equation


How to prove that the integral of $\int \dfrac{1}{x}dx=\log(x)+C$

Question: How to prove that the integral of $$\int \dfrac{1}{x}dx=\log(x)+C$$
I know that by the fundamental theorem of calculus, the derivative of $\log(x)$ is $\dfrac{1}{x}$, but I don't know how to prove this without recoursing to the fundamental theorem of calculus. Can you prove this using the Riemann sum?
Historically, this integral is found by Gregoire Sainte Vincent. But his method is archaic and I don't know how to read Latin to follow his argument.
Please help, thank you!
Comment: I know. That doesn't tell me how you define it. Many of us define $$\log(x)=\int_1^x\frac1t\,dt.$$ That would give a fairly straightforward answer to your question.
Comment: To echo the other comments, we have to know which definition of $\log x$ you consider to be fundamental. I think a good approach is taking $\log$ to be the unique function such that $\log(e) = 1$ and $\log(xy) = \log(x) + \log(y)$. Of course, we still have to make a decision about how to define $e$, but using the usual $e = \lim_{n \to \infty} (1 + \frac{1}{n})^n$ seems sufficiently elementary to me.
Comment: The integrands in your body and title are rather different
Comment: The answer to your question depends on how you define $\log(x)$. So, how do you define $\log(x)$?
Comment: "It is the log of base e" and what is the definition of $e$.  ANd for that matter what is the definition for $b^x$ where $x$ is not nescessarily an integer or a rational number.
Comment: Fixed, thank you, sorry of the typo.
Comment: It is the log of base e.
Comment: Do you mean $\log(x)$ as the natural logarithm (i.e. $\ln x$) or as a common logarithm (i.e. $\log_{10} x$)?
Answer: I agree with  Charles Hudgins.
Define log as differentiable
and satisfying
$f(xy) = f(x)+f(y)$
with a boundary condition
to be decided later.
Then
$f(1) = 0$
(set $y = 1$)
and
$\begin{array}\\
f(x+h)
&=f(x(1+h/x))\\
&=f(x)+f(1+h/x)\\
\text{so}\\
f(x+h)-f(x)
&=f(1+h/x)\\
&=f(1+h/x)-f(1)\\
\text{so}\\
\dfrac{f(x+h)-f(x)}{h}
&=\dfrac{f(1+h/x)-f(1)}{h}\\
&=\dfrac1{x}\dfrac{f(1+h/x)-f(1)}{h/x}\\
\end{array}
$
Letting $h \to 0$,
$f'(x)
=\dfrac{f'(1)}{x}
$
so,
since $f(1) = 0$,
$f(x)
=f'(1)\int_1^x \dfrac{dt}{t}
$.
"Natural" log has
$f'(1) = 1$.
Comment: This proof still goes through if instead of differentiable we only require that $f$ is continuous (just apply $f$ to both sides of the definition for $e$. The existence of $e$ can be shown by elementary means). We'll find that $f'(1) = f(e)$. We could also require that $f$ is increasing instead, as shown [here](https://mathoverflow.net/questions/22706/uniqueness-of-the-logarithm-function). In any case, the unique (up to constant multiplication) existence of $f$ follows from fairly weak assumptions.
Comment: I note as a supplement that such an $f$ really exists since $F(x) = \int_1^x \frac{1}{t} dt$ is defined for all values of $x$, is differentiable by FTC, and satisfies $F(ab) = F(a) + F(b)$. To see this last fact, observe that $F(ab) = \int_1^{ab} \frac{1}{t} dt = \int_1^a \frac{1}{t} dt + \int_a^{ab} \frac{1}{t} dt = F(a) + \int_1^b \frac{1}{au} adu = F(a) + \int_1^b \frac{1}{u} du = F(a) + F(b)$.
Answer: Well, we can either define $\ln x = \int_{1}^x \frac 1t dt$ or we can define $e$ to be the unique positive real where $\frac {de^x}{dx} = e^x$.
Either way if we define $e^x$ and $\ln x$ are inverse functions of each other, then $e^{\ln x}=\ln e^x =x$ and the chain rule tells ust that $\frac {dx}{dx} =1$ so $\frac {de^{\ln x}}{d\ln x}\frac {d\ln x}{dx} = \frac {d\ln(e^x)}{de^x}\frac {de^x}{dx} = 1$.
So if we defined $e$ so that $\frac{de^x}{dx} = e^x$ we get $\frac {de^{\ln x}}{d\ln x}\frac {d\ln x}{dx}= e^{\ln x} \frac {d\ln x}{dx}=x\frac {d\ln x}{dx} = 1$ so $\frac {d\ln x}{dx} = \frac 1x$.
Or if we defined $\ln x= \int_1^x \frac 1t dt$ we get $\frac {d\ln(e^x)}{de^x}\frac {de^x}{dx}=\frac 1{e^x} \frac {de^x}{dx} =1$ so $\frac {de^x}{dx } = 1$.
So one implies the other.  The question is.... which is the definition and which is the derivation?
Answer: If I understand you correctly you want an argument why the antiderivative of $\frac 1x$ is equal to $\log x + C$ without using any integration technique.
So, I assume you have the following facts at hand only using differentiation:

$(e^x)' = e^x$
$\log x$ is defined as the inverse function of $e^x \Rightarrow e^{\log x} = x$
$\stackrel{e^x>0}{\Rightarrow} \log x$ is differentiable for all $x>0$
You know the chain rule.
You know that if $f'(x)=g'(x)$ on an interval $(a,b)$, then $f(x) = g(x) +C$ for some constant $C$

Hence, for $x>0$ you have
$$1 = \left(x\right)'=\left(e^{\log x}\right)' = e^{\log x}\cdot (\log x)'=x(\log x)'$$
$$\Rightarrow (\log x)' = \frac 1x\Rightarrow \int \frac{dx}{x} = \log x +C $$


Orbits of Special Linear Group acting on $\mathbb{C}^n$

Question: Letting $SL_n(\mathbb{C})$ act on $\mathbb{C}^n$, what orbits do you get?
Answer: Clearly $A0 = 0$ for all $A \in SL_n(\mathbb{C})$. Also as $A$ is invertible, $A0 = 0 \iff v = 0$, so $0$ forms a singleton orbit.
Now given any two vectors $ v \neq w \in \mathbb{C}^n$, there is a matrix  $A \in SL_n(\mathbb{C})$ such that $Av = w$, so the orbits are $0$ and $\mathbb{C}^n \setminus 0$.
Is this last assertion about there existing an A true, and if so, why (using elementary group theory)
Answer: Let $v=v_1$ be any nonzero vector in $\mathbb{C}^n \setminus \{0\}$. Complete it to a basis $v_1, v_2, \dots, v_n$ of $\mathbb{C}^n$ and let $A$ be the matrix whose columns are the $v_i$. Then $A$ is invertible and satisfies $Ae_1 = v$. Assuming $n \geq 2$, change the matrix $A$ by replacing any of the $v_i$ with $i \geq 2$ by $((\det{A})^{-1})v_i$. Call the resulting matrix $A'$. Then $A'$ is in $\text{SL}_n{\mathbb{C}}$ and satisfies $A'e_1 = v$. This shows that the induced action of $\text{SL}_n(\mathbb{C})$ on $\mathbb{C}^n \setminus \{0\}$ is transitive (since we have shown that the orbit of $e_1$ is $\mathbb{C}^n \setminus \{0\}$).
Comment: I guess my explanation is clearer.  You will have Bu=v where   B= 1/det(A). A and u=(detA)e_1 so B \in SL(C^n).
Answer: Note that the assertion is not quite true for $n=1$.
For $n > 1$, though, it is a matter of showing that if you fix the first column of a matrix $A$ to be an arbitrary non-zero vector, then you can complete $A$ to an element of the special linear group. Without lack of generality, suppose the first column is
$$
\begin{bmatrix}
a_{1}\\a_{2}\\ \vdots \\a_{n}
\end{bmatrix},
$$
with $a_{1} \ne 0$. Then take
$$
A =
\begin{bmatrix}
a_{1} & 0 & 0 & \dots & 0\\
a_{2} & a_{1}^{-1} & 0 & \dots & 0\\
a_{3} & 0 & 1& \dots & 0\\
&&& \ddots \\a_{n} & 0 & 0 & \dots & 1\\
\end{bmatrix}
$$


json values not parsing in ajax

Question: I could not pass my json values to the url

var url="http://192.168.2.143/php/welcome.php";

 $.ajax({
 type: 'POST',
 url: url,
 data: ({fname:"hi",age:"21"}),
 success: success,
 error:function(e)
 {
         alert(e);
 }

});

 }

 function success()
 {
         alert("done");
 }

Well if I change the url to  welcome.php its not working. I have welcome.php in my folder , i want to access it from another system so i changed the url,
thank u in advance.
Comment: You cannot make cross domain AJAX request. Only requests to the same domain are allowed for security reasons.
Comment: oh , then wats the solution for it , i want it by any means , can u suggest any other way of doin it @Lex
Comment: Is your server on the same LAN ? 192.168.2.143 is a local address, not an internet one. Are you sure you don't have firewall problems ?
Comment: 192.168.2.143 is a computer next to me , i guess its not having any fire wall block @dystroy
Comment: You cannot do cross domain AJAX calls. If you want this to work you could use JSONP if you have control over the remote script that you are trying to call. But the jquey implementation of JSONP doesn't support POST method, only GET.
Comment: @DarinDimitrov even i tried jsonp it aint working , i am always taken to error call back and also changed the method to get
Comment: This code works and gives you the `alert` if you remove that pesky curly brace. It throws the error - alerting `[object Object]`. Run it in your console
Comment: How did you try JSONP? Did you modify your `welcome.php` script to return JSONP instead of JSON?
Comment: You can make a script that is called by AJAX on the same domain that does an HTTP request to another domain.
Answer: You have an extra curly brace
 } // HERE

function success()
{
     alert("done");
}

json values not parsing in ajax


Open quote is expected for attribute "{1}" associated with an element type "name"

Question: Getting the following error:

(Configuration) :org.xml.sax.SAXException:Open quote is expected for attribute "{1}" associated with an  element type  "name".

while uploading the below xml file.
<code><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE UWLConfiguration PUBLIC "-//SAP//UWL1.0//EN" "uwl_configuration.dtd">
<UWLConfiguration version="1.0">
    <ItemTypes>
        <ItemType name= "uwl.task.webflow.TS8880003.SAP_ECC_HumanResources" connector="WebFlowConnector" defaultView="DefaultView" defaultAction="viewDetail" executionMode="default">
            <ItemTypeCriteria systemId="SAP_ECC_HumanResources" externalType="TS8880003″ connector="WebFlowConnector"/>
            <Actions>
                <Action name="Approve" groupAction="yes" handler="UserDecisionHandler" returnToDetailViewAllowed="yes" launchInNewWindow="no">
                    <Properties>
                        <Property name="decisionKey" value="1″ />
                    </Properties>
                    <Descriptions default="Approve"/>
                </Action>
                <Action name="Reject" groupAction="yes" handler="UserDecisionHandler" returnToDetailViewAllowed="yes" launchInNewWindow="no">
                    <Properties>
                        <Property name="decisionKey" value="2″ />
                    </Properties>
                    <Descriptions default="Reject"/>
                </Action>
            </Actions>
        </ItemType>
    </ItemTypes>
</UWLConfiguration>
</code>
Comment: Your xml has `"` instead of `"`
Answer: Now i am getting new error "The markup in the document preceding the root element must be well-formed".
<code>    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE UWLConfiguration PUBLIC "-//SAP//UWL1.0//EN" "uwl_configuration.dtd">
</UWLConfiguration version="1.0">
    </ItemTypes>
    <ItemType name="uwl.task.webflow.TS8880003.SAP_ECC_HumanResources connector"="WebFlowConnector" defaultView="DefaultView" defaultAction="viewDetail" executionMode="default">
    <ItemTypeCriteria systemId="SAP_ECC_HumanResources" externalType="TS8880003" connector="WebFlowConnector"/>
    <Actions>
    <Action name="Approve" groupAction="yes" handler="UserDecisionHandler" returnToDetailViewAllowed="yes" launchInNewWindow="no">
    <Properties>
    <Property name="decisionKey" value="1" />
    </Properties>
    <Descriptions default="Approve"/>
    </Action>
    <Action name="Reject" groupAction="yes" handler="UserDecisionHandler" returnToDetailViewAllowed="yes" launchInNewWindow="no">
    <Properties>
    <Property name="decisionKey" value="2" />
    </Properties>
    <Descriptions default="Reject"/>
    </Action>
    </Actions>
    </ItemType>
    </ItemTypes>
</UWLConfiguration>
</code>


Get data with base url by values()

Question: I have a image table that have more than 10k images.
models.py
class Image(models.Model):
    image = models.ImageField(upload_to='image')
    tag = models.CharField(max_length=100)

views.py
Image.objects.filter(tag__icontains=tag).values('image')

By above query i am getting data in this format.
[
    {
            "image": "image/mcml4__9_ipmVd6E.jpeg",

    },
    {
            "image": "image/mcml4__df9_ipmfgdfVd6E.jpeg",

    }
]

but i need this with base url.
[
    {
            "image": "http://{{baseurl}}/media/image/mcml4__9_ipmVd6E.jpeg",

    },
    {
            "image": "http://{{baseurl}}/media/image/mcml4__9_ipmVd6E.jpeg",

    }
]

How can i do this with orm query only without manipulation in python memory.
Comment: Please do *not* use `.values` in the first place. It is a severe anti-pattern to use this for serialization. Usually one uses a *serializer* for that: https://www.django-rest-framework.org/api-guide/serializers/
Comment: and after using _serialization_, try [this](https://stackoverflow.com/questions/35522768/django-serializer-imagefield-to-get-full-url) to build the full url.
Comment: is it possible without using serializers ??
Comment: @DEEPAKKUMAR: exactly what is your objection againt serializers? It is a layer that can validate, encode, decode, etc. objects. By using it in an APIView, it can even remove most of the boilerplate code for most API calls (obtaining a list, obtaining details, creating a new object, etc.)
Answer: Try to modify this snippet:
views.py
from django.db.models.functions import Concat
from django.db.models import Value

Image.objects.filter(tag__icontains=tag).values(image=Concat(Value('http://base_url.com', 'image', output_field=CharField())))

I haven't tried it, but it has to work.

Get data with base url by values()


Erro ao criar uma validação customizada em ASPNET MVC ( Client Side )

Question: Estou com um problema ao criar uma validação customizada usando dataanotations pelo aspnet mvc.
Minha Model:
    public class Usuario
    {

        public string Nome { get; set; }

        [Idade(18)]
        public string Senha { get; set; }
     }

Minha Classe de Validação:
    public class Idade : ValidationAttribute
    {
        private readonly int _idade;
        public Idade(int idade)
        {
            _idade = idade;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {

            if(value != null)
            {
                var idadeInformada = _idade;

                if (idadeInformada <= 18) {

                    return new ValidationResult(null);

                }
                else
                {
                    return new ValidationResult("Você não possui idade suficiente para se cadastrar");
                }
            }

            return ValidationResult.Success;
        }
    }

O grande problema é que não está funcionado essa validação customizada, quando dou um POST na página ele não exibe o erro da validação, diferente de quando coloco as validações "nativas" do dataannotations
Answer: A sua lógica está um pouco equivocada, você não está comparando o valor com o filtro declarado e a condição está invertida.
public class Idade : ValidationAttribute
{
    private int _idadeMinima;
    public Idade(int idadeMinima)
    {
        _idadeMinima = idadeMinima;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {

        if (value != null)
        {
            var idadeInformada = int.Parse(value.ToString());

            if (idadeInformada < _idadeMinima)
            {
                return new ValidationResult("Você não possui idade suficiente para se cadastrar");
            }
        }

        return ValidationResult.Success;
     }
}

Assim como você deve preparar a sua view para exibir a mensagem de erro e devolver a model para ela após a validação do post na controller.
@Html.ValidationSummary(true, "", new { @class = "text-danger" })

    @Html.LabelFor(model => model.Idade, htmlAttributes: new { @class = "control-label col-md-2" })

        @Html.EditorFor(model => model.Idade, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Idade, "", new { @class = "text-danger" })

Comment: realmente a lógica estava errado, porém o erro persiste, a mensagem não aparece.
Comment: A sua view está preparada para isso? Você está devolvendo a ViewModel para ela após o post?
Comment: dando uma pesquisada mais afundo vi que tem tem que ser criado um jquery.unobtrusive, é isso mesmo?
Comment: @William Conseguiu?
Comment: Do seu jeito ainda não consegui, fiz um teste estendendo o jquery validate e funcionou mais ou menos.


abstract declarator Node* used as declaration

Question: I am using an abstract class SetOfInt; where a Btree baseclass inherits from. The declaration of the virtual function find gives me a compiler error and I cannot figure out why?
This is the exact error:

SetOfInt.h:21: error: expected unqualified-id before 'virtual'
  SetOfInt.h:21: error: abstract declarator 'Node*' used as declaration
  SetOfInt.h:21: error: expected ';' before 'virtual'
  SetOfInt.h:30: error: expected unqualified-id before 'virtual'
  SetOfInt.h:30: error: abstract declarator 'Node*' used as declaration
  SetOfInt.h:30: error: expected ';' before 'virtual'

Any help would be highly appreciated!
/*  1 */ #include <cstdlib>
/*  2 */ #include <iostream>
/*  3 */
/*  4 */ using namespace std;
/*  5 */
/*  6 */ class Node
/*  7 */ {
/*  8 */ public:
/*  9 */     Node (int x);
/* 10 */     int m_data;
/* 11 */     Node *m_left;
/* 12 */     Node *m_right;
/* 13 */ };
/* 14 */
/* 15 */ class SetOfInt
/* 16 */ {
/* 17 */ public:
/* 18 */     void virtual add(int x)=0;
/* 19 */     bool virtual test(int x)=0;
/* 20 */     bool virtual remove(int x)=0;
/* 21 */     Node* virtual find(int x)=0;
/* 22 */ };
/* 23 */
/* 24 */ class Btree : public SetOfInt
/* 25 */ {
/* 26 */ public:
/* 27 */     void virtual add(int x);
/* 28 */     bool virtual test(int x);
/* 29 */     bool virtual remove(int x);
/* 30 */     Node* virtual find(int x);
/* 31 */     Node *m_root;
/* 32 */ };

Answer: The return types should go after the virtual keyword.
i.e.
virtual void add(int x);

instead of
void virtual add(int x);

Comment: `void virtual add(int x);` is fine. The problem is with `Node* virtual find(int x);`, which should be `Node virtual *find(int x);` to stay with this style of writing the return type first.


Tensorflow multivariate linear regression not converging

Question: I am trying to train a multivariate linear regression model with regularization using tensorflow. For some reason I am not able to get the training piece of the below code to calculate the error I want to use for the gradient descent update. Am I doing something wrong in setting up my graph?
def normalize_data(matrix):
    averages = np.average(matrix,0)
    mins = np.min(matrix,0)
    maxes = np.max(matrix,0)
    ranges = maxes - mins
    return ((matrix - averages)/ranges)

def run_regression(X, Y, X_test, Y_test, lambda_value = 0.1, normalize=False, batch_size=10):
    x_train = normalize_data(X) if normalize else X
    y_train = Y
    x_test = X_test
    y_test = Y_test
    session = tf.Session()

    # Calculate number of features for X and Y
    x_features_length = len(X[0])
    y_features_length = len(Y[0])

    # Build Tensorflow graph parts
    x = tf.placeholder('float', [None, x_features_length], name="X")
    y = tf.placeholder('float', [None, y_features_length], name="Y")
    theta = tf.Variable(tf.random_normal([x_features_length, y_features_length], stddev=0.01), name="Theta")
    lambda_val = tf.constant(lambda_value)

    # Trying to implement this way http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex5/ex5.html
    y_predicted = tf.matmul(x, theta, name="y_predicted")
    regularization_cost_part = tf.cast(tf.mul(lambda_val,tf.reduce_sum(tf.pow(theta,2)), name="regularization_param"), 'float')
    polynomial_cost_part = tf.reduce_sum(tf.pow(tf.sub(y_predicted, y), 2), name="polynomial_sum")

    # Set up some summary info to debug
    with tf.name_scope('cost') as scope:
        cost_func = tf.mul(tf.cast(1/(2*batch_size), 'float'), tf.cast(tf.add(polynomial_cost_part, regularization_cost_part), 'float'))
        cost_summary = tf.scalar_summary("cost", cost_func)

    training_func = tf.train.GradientDescentOptimizer(0.03).minimize(cost_func)

    with tf.name_scope("test") as scope:
        correct_prediction = tf.sub(tf.cast(1, 'float'), tf.reduce_mean(tf.sub(y_predicted, y)))
        accuracy = tf.cast(correct_prediction, "float")
        accuracy_summary = tf.scalar_summary("accuracy", accuracy)

    saver = tf.train.Saver()
    merged = tf.merge_all_summaries()
    writer = tf.train.SummaryWriter("/tmp/football_logs", session.graph_def)
    init = tf.initialize_all_variables()

    session.run(init)
    for i in range(0, (len(x_train)/batch_size)):
        session.run(training_func, feed_dict={x: x_train[i*batch_size:i*batch_size+batch_size], y: y_train[i*batch_size:i*batch_size+batch_size]})
        if i % batch_size == 0:
            result = session.run([merged, accuracy], feed_dict={x: x_test, y: y_test})
            writer.add_summary(result[0], i)
            print "step %d, training accuracy %g"%(i, result[1])

    print "test accuracy %g"%session.run(accuracy, feed_dict={x: x_test, y: y_test})

    save_path = saver.save(session, "/tmp/football.ckpt")
    print "Model saved in file: ", save_path
    session.close()

my output looks like this
step 0, training accuracy 39.1802
step 10, training accuracy 39.1802
step 20, training accuracy 39.1802
...
step 210, training accuracy 39.1802
test accuracy 39.1802
Model saved in file:  /tmp/football.ckpt

Comment: Have you tried playing with the learning rate? Also try using reduce_mean instead of reduce_sum in your cost calculation. In this way the learning rate becomes more independent from the batch_size. Also try adding a bias term to "y_predicted = tf.matmul(x, theta, name="y_predicted");" (why the semicolon here?). Maybe the method is underfitting and thus the high training error. Obs: You should also change the names of your variables to reflect their meaning.
Comment: Why does your target (y variables) have features? It should be 1-D array? Also your regularization factor is not being updated anywhere, so it's not dependent on your input.
Comment: I've tried updating the learning rate and using the reduce _mean, but for some reason the accuracy function does not converge. I'm not sure if maybe I have it configured wrong with the variables. I am trying to implement regression with regularization to prevent overfitting as described [in this article](http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex5/ex5.html) which is where the polynomial_part and regularization_part variable names come from.
Comment: @JHafdahl ,  Did you find the source of your problem? I suspect I have run into a similar issue :/
Answer: it seems indeed a problem with the learning rate: 0.03 may be too high depending on how does your data look like. Also, you probably want to create your graph separated from the session in a more explicit way, or even use the normal equations to reach the optimal solution without having to iterate, if your dataset has a mid/low dimensionality. Here I posted some examples that you may hopefully find helpful! Also, the TF tutorials cover it well (search for "Complete program" in that page).
But regarding your code, here is a version that worked for me: I changed some deprecated functions, and basically set the learning rate to the much lower value alpha=1e-8, which (on the synthetic dataset also generated in the code) seems to converge:
test accuracy 2176.11
test accuracy 1898.6
test accuracy 1663.69
test accuracy 1458.53
test accuracy 1287.57
test accuracy 1116.9
test accuracy 969.474
test accuracy 841.028
test accuracy 738.592
test accuracy 649.891
test accuracy 565.188
test accuracy 495.33
test accuracy 438.351
test accuracy 381.161
test accuracy 333.213
test accuracy 289.575
test accuracy 254.394
test accuracy 222.836
test accuracy 197.36
test accuracy 172.788
test accuracy 152.251
test accuracy 132.664
test accuracy 115.982
test accuracy 101.021
final test accuracy 90.2555

CODE:
import tensorflow as tf
import numpy as np

# generate some dataset
DIMENSIONS = 5
DS_SIZE = 5000
TRAIN_RATIO = 0.5 # 50% of the dataset isused for training
_train_size = int(DS_SIZE*TRAIN_RATIO)
_test_size = DS_SIZE - _train_size
f = lambda(x): sum(x) # the "true" function: f = 0 + 1*x1 + 1*x2 + 1*x3 ...
noise = lambda: np.random.normal(0,10) # some noise
# training globals
LAMBDA = 1e6 # L2 regularization factor
# generate the dataset, the labels and split into train/test
ds = [[np.random.rand()*1000 for d in range(DIMENSIONS)] for _ in range(DS_SIZE)]
ds = [([1]+x, [f(x)+noise()]) for x in ds] # add x[0]=1 dimension and labels
np.random.shuffle(ds)
train_data, train_labels = zip(*ds[0:_train_size])
test_data, test_labels = zip(*ds[_train_size:])

def normalize_data(matrix):
    averages = np.average(matrix,0)
    mins = np.min(matrix,0)
    maxes = np.max(matrix,0)
    ranges = maxes - mins
    return ((matrix - averages)/ranges)

def run_regression(X, Y, X_test, Y_test, lambda_value = 0.1, normalize=False, batch_size=10, alpha=1e-8):
    x_train = normalize_data(X) if normalize else X
    y_train = Y
    x_test = X_test
    y_test = Y_test
    session = tf.Session()
    # Calculate number of features for X and Y
    x_features_length = len(X[0])
    y_features_length = len(Y[0])
    # Build Tensorflow graph parts
    x = tf.placeholder('float', [None, x_features_length], name="X")
    y = tf.placeholder('float', [None, y_features_length], name="Y")
    theta = tf.Variable(tf.random_normal([x_features_length, y_features_length], stddev=0.01), name="Theta")
    lambda_val = tf.constant(lambda_value)
    # Trying to implement this way http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex5/ex5.html
    y_predicted = tf.matmul(x, theta, name="y_predicted")
    #regularization_cost_part = tf.cast(tf.multiply(lambda_val,tf.reduce_sum(tf.pow(theta,2)), name="regularization_param"), 'float')
    #polynomial_cost_part = tf.reduce_sum(tf.pow(tf.subtract(y_predicted, y), 2), name="polynomial_sum")
    # Set up some summary info to debug
    with tf.name_scope('cost') as scope:
        #cost_func = tf.multiply(tf.cast(1/(2*batch_size), 'float'), tf.cast(tf.add(polynomial_cost_part, regularization_cost_part), 'float'))
        cost_func = (tf.nn.l2_loss(y_predicted - y)+lambda_val*tf.nn.l2_loss(theta))/float(batch_size)
        #DEPRECATED*** cost_summary = tf.scalar_summary("cost", cost_func)
        cost_summary = tf.summary.scalar('cost', cost_func)# Add a scalar summary for the snapshot loss.
    training_func = tf.train.GradientDescentOptimizer(alpha).minimize(cost_func)
    with tf.name_scope("test") as scope:
        correct_prediction = tf.subtract(tf.cast(1, 'float'), tf.reduce_mean(tf.subtract(y_predicted, y)))
        accuracy = tf.cast(correct_prediction, "float")
        #DEPRECATED*** accuracy_summary = tf.scalar_summary("accuracy", accuracy)
        #accuracy_summary = tf.summary.scalar("accuracy", accuracy)
    saver = tf.train.Saver()
    #DEPRECATED*** merged = tf.merge_all_summaries()
    merged = tf.summary.merge_all()
    #DEPRECATED*** writer = tf.train.SummaryWriter("/tmp/football_logs", session.graph_def)
    writer = tf.summary.FileWriter("/tmp/football_logs", session.graph)
    #DEPRECATED*** init = tf.initialize_all_variables()
    init = tf.global_variables_initializer()
    session.run(init)
    for i in range(1, (len(x_train)/batch_size)):
        session.run(training_func, feed_dict={x: x_train[i*batch_size:i*batch_size+batch_size], y: y_train[i*batch_size:i*batch_size+batch_size]})
        if i % batch_size == 0:
            print "test accuracy %g"%session.run(accuracy, feed_dict={x: x_test, y: y_test})
            #result = session.run([merged, accuracy], feed_dict={x: x_test, y: y_test})
            # writer.add_summary(result[0], i)
            # print "step %d, training accuracy %g"%(i, result[1])
            #writer.flush()
    print "final test accuracy %g"%session.run(accuracy, feed_dict={x: x_test, y: y_test})
    # save_path = saver.save(session, "/tmp/football.ckpt")
    # print "Model saved in file: ", save_path
    session.close()

run_regression(train_data, train_labels, test_data, test_labels, normalize=False, alpha=1e-8)

As I said, you will probably want to change the structure to favor readability and scalability, but hopefully this helps!
Cheers,
Andres


Can I somehow compute variance explained by PC after Oblique rotation in PCA?

Question: Let´s say that my PCA analysis extracted 2 components, which explain 80% of the variance before rotation. The components were then rotated using oblique (Direct Oblimin) rotation, so SPSS cannot compute how much percentage of variance each component explain. When I plot the graph, usually editors require these percentages in brackets after the components. So I want to compute relative percentage of variance for each component. So if data looks like this
BEFORE ROTATION:
PC1 accounts for 60% of variance, eigenvalue 6.000;
PC2 accounts for 20% of variance, eigenvalue 2.000;
Total - 80% variance,  eigenvalue 8.000;
AFTER ROTATION:
PC1: eigenvalue 5.000;
PC2: eigenvalue 4.000;

can I compute these percentages this way?
I expect, that after rotation total variance explained by both components doesn´t change, so it should be 80%. Am I right?
In unrotated solution, you can compute variance explained by component 2 this way: eigenvalue of component 2/total eigenvalue * total percentage explained = 2/8*80%=20%
We can also compute, that 1.000 eigenvalue = 10% of variance
Can I use this equation (1.000 eigenvalue=10%) to compute variance explained by each component after rotation, so variance explained by PC1 after rotation should be 50% (eigenvalue=5 so 50%) and variance explained by PC2 after rotation should be 40%(eigenvalue=4 so 40%)? Of course in this case we cannot compute total variance by PC1+PC2 because components are correlated and total variance should be still 80% (maybe).
I don´t think that it is so easy, because SPSS would give me these numbers. So can I somehow compute (from data above line) how much % of variance is explained by PC1 after oblique rotation?
Thanks
Comment: You can't speak of "eigenvalues" after rotation, even orthogonal rotation. Perhaps you mean sum of squared loadings for a principal component, after rotation. When rotation is oblique, this sum of squares tells nothing about the amount of variance explained, because components aren't orthogonal anymore. So, you shouldn't report any percentage of variance explained.
Comment: In my view there's nothing wrong with reporting the variance explained by each of a set of correlated components, just as there's nothing wrong with showing each predictor's zero-order r-squared in a regression context.  Just don't make the mistake (as has been noted) of thinking you can add all of these up to arrive at the overall variance explained.
Comment: The solution as a whole explains the same % of variance, whether you rotate the components obliquely or not.  In the case you describe, that % is 80%.
Comment: Another note. The question title is strange. As far as you are aware, there's two types of loadings after oblique rotation, pattern coefficients (which are regression coef-s) and structure coefficients (which are correlation coef-s). Then, what else "loadings" are you striving to compute?
Comment: Ok, my fault. So what these rotation sums of squared loadings for PC can tell me? Because in orthogonal rotation they are something like eigenvalues, from which I can compute this way amount of variance explained.
Comment: Yes, in orthogonal rotation these sum of squared loadings are the proportion of variance explained. I don't think that in oblique rotation they are informative. In your place, I wouldn't explore or report them.
Comment: Ok but I have fount several papers in which authors were able to report proportion of variance explained after oblique rotation. Are they not right?http://share.eldoc.ub.rug.nl/FILES/root2/2001/Qualoflip/Suurmeijer_2001_Arthrit_Care_Res.pdf - page 118                       http://pubman.mpdl.mpg.de/pubman/item/escidoc:726189:6/component/escidoc:726188/maess.pdf - page 458                      http://gump.qimr.edu.au/general/daleN/SNPSpD/Horne2004.pdf and            http://xa.yimg.com/kq/groups/15062060/2048952508/name/Henson+2006.pdf - PAGE 410 - read expectation 6
Comment: And second thing, if rotated sums of squared loading are "not" informative in oblique solution, why SPSS write them in output?
Comment: Must be very interesting papers, thank you @Noro. I will read them at some length. Unfortunately, I'm too busy currently to respond.
Comment: Yes, but we are solving 2 things: Is statistically right to count variance explained by PC after oblique rotation? Second: If yes, is my equation good to do this?
Comment: I do not think that summing up the coefficients is equal to the variations accounted for by each PC's coefficient, either un-rotated or rotated one. I'm in the same boat; I need to know how calculate the variations explained after rotation!
Thanks
Comment: I have encountered the same problem. And I have also seen others reporting % of variance explained after using Oblimin rotation. So I think there is a way of computing it? Have you managed to it and may I know how if so?
Answer: I think you're on solid ground.  Another useful thing to do is to call up a loading plot ('/plot rotation'), then to reanalyze using varimax rotation and again ask for a loading plot.  But I hope you're analyzing objective data and not opinion data:  using PCA on the latter is well-known to be a mistake, because it treats as part of an underlying dimension some information that should be treated as unique to particular variables, or even as measurement error.
Comment: See ttnphns' comments.


Determinar o consumo de memória

Question: Olá, sou iniciante em Go e estou com dificuldades em determinar quanta memória o código em Go precisou para executar algum código. Eu fiz a função memory() que até retorna algo, mas não sei como seria para trazer o retorno que quero, seria tipo: o código precisou de 15MB para ser executado, por exemplo, teria como fazer isso? Agradeço desde já!
package main

import (
    "fmt"
    "log"
    "runtime"
    "time"
)

func ack(m, n uint) uint {
    if m == 0 {
        return n + 1
    } else if n == 0 {
        return ack(m - 1, 1)
    } else {
        return ack(m - 1, ack(m, n - 1))
    }
}

func memory(){
    for {
        var m runtime.MemStats
        runtime.ReadMemStats(&m)
        log.Printf("\nAlloc = %v\nTotalAlloc = %v\nSys = %v\nNumGC = %v\n\n", m.Alloc / 1024, m.TotalAlloc / 1024, m.Sys / 1024, m.NumGC)
        time.Sleep(5 * time.Second)
    }
}

func main() {

    var inicio = time.Now().UnixNano()
    fmt.Println(ack(1,2))
    var fim = time.Now().UnixNano()
    fmt.Println(fim - inicio)

    memory()

}

Comment: Você precisa de stack, heap, ou tempo?
Comment: Preciso do total de memória que foi utilizado para executar o código
Answer: A pergunta é antiga, mas acho que sempre vale responder.
Sugiro dar uma olhada no pacote de teste (https://golang.org/pkg/testing/) nativo de Go. Há várias funções interessantes para esse tipo de coisa.
Para seu caso especifico, usaria a funcionalidade de test de Benchmark, que te dá informações como alocação de memória, assim como, claro, o bench da sua função.
Para usar a funcionalidade de benchmark in Go, crie um novo arquivo seguindo o formato:
[nome do arquivo]_test.go
Nesse arquivo, importe o package de test e crie uma função no seguinte formato:
*func Benchmark[nome da função](b testing.B) {}
É crucial que a função comece com o termo Benchmark pois de outra forma Go não vai reconhecer essa como uma função de Benchmark e dará erro.
Segue um exemplo com o seu código:
func ack(m, n uint) uint {
    if m == 0 {
        return n + 1
    } else if n == 0 {
        return ack(m-1, 1)
    } else {
        return ack(m-1, ack(m, n-1))
    }
}

func BenchmarkAck(b *testing.B) {
    ack(1, 2)
}

Para rodar use o comando:  go test -bench=. com a flag -benchmem para ver informações de alocação de memória etc:
go test -bench=. -benchmem

Determinar o consumo de memória


Inverse Mellin transform of $a^{-s} \zeta (s)$

Question: The Mellin inverse is given by
$$ \frac{1}{2\pi i}\int_{c-i\infty}^{c+i\infty}dsF(s)x^{-s} $$
Is it possible to compute the inverse transform of:
$$F(s)=a^{-s} \zeta (s) $$ where: $a>0$
Mathematica gave me solution:
$$\mathcal{M}_s^{-1}\left[a^{-s} \zeta (s)\right](x)=\frac{1}{a x} ? $$  for:  $\ln (a)>0\land 0<x<1$
,but if I
$$\mathcal{M}_x\left[\frac{1-\theta (x-1)}{a x}\right](s)=\frac{1}{a (-1+s)}+\frac{2 \pi  \delta (i (-1+s))}{a}$$ I not get $a^{-s} \zeta (s) $ ?
Using functional equation formula for Zeta and Borel Regularization for the series I have:
$$\mathcal{M}_s^{-1}\left[a^{-s} \zeta (s)\right](x)=\mathcal{M}_s^{-1}\left[\frac{a^{-s} \zeta (1-s)}{2 (2 \pi )^{-s} \cos \left(\frac{\pi  s}{2}\right) \Gamma
   (s)}\right](x)=\mathcal{M}_s^{-1}\left[\frac{a^{-s} \sum _{k=1}^{\infty } \frac{1}{k^{1-s}}}{2 (2 \pi )^{-s} \cos \left(\frac{\pi  s}{2}\right) \Gamma (s)}\right](x)=\sum _{k=1}^{\infty }
   \mathcal{M}_s^{-1}\left[\frac{a^{-s}}{\left(2 (2 \pi )^{-s} \cos \left(\frac{\pi  s}{2}\right) \Gamma (s)\right) k^{1-s}}\right](x)=\sum _{k=1}^{\infty } \frac{2 \cos \left(\frac{2 k \pi }{a
   x}\right)}{a x}=-\frac{1}{a x}$$ My and Mathematica solution are correct ?
I tried find solution in Tables 1 2 3 of Mellin Transforms ,but I didn't find my example.
Answer: Since
$$\zeta(s)=s\, \mathcal{M}_x[f(x)](-s)=s \int\limits_0^\infty f(x)\, x^{-s-1}\, dx=\sum\limits_{n=1}^\infty \frac{1}{n^s}\,,\quad\Re(s)>1\tag{1}$$
where
$$f(x)=\sum\limits_{n=1}^x 1=\sum\limits_{n=1}^x \theta(x-n)=\sum\limits_{n=1}^\infty \theta(x-n)\tag{2},$$
perhaps you should instead consider the inverse Mellin transform
$$\mathcal{M}_s^{-1}\left[\frac{G(s)}{s}\right]\left(\frac{1}{x}\right)=\sum\limits_{n=1}^\infty \theta(x-a n)\tag{3}$$
where
$$G(s)=a^{-s}\, \sum\limits_{n=1}^\infty \frac{1}{n^s}=a^{-s}\, \zeta(s)\,,\quad\Re(s)>1\tag{4}.$$

In other words
$$s\, \mathcal{M}_x[g(x)](-s)=s \int\limits_0^\infty g(x)\, x^{-s-1}\, dx=a^{-s}\, \zeta(s)\, ,\quad\Re(s)>1\tag{5}$$
where
$$g(x)=\sum\limits_{n=1}^\infty \theta(x-a n)\tag{6}.$$

But if your really want to consider
$$\mathcal{M}_s^{-1}\left[a^{-s}\, \zeta(s)\right](x)\tag{7}$$
then
$$\frac{2}{a x} \sum\limits_{k=1}^{\infty} \cos\left(\frac{2 k \pi}{a x}\right)\ne-\frac{1}{a x}\tag{8}$$
since
$$\mathcal{M}_x\left[-\frac{1}{a x}\right](s)=-\frac{2 \pi}{a}\, \delta(i (s-1))\ne a^{-s}\, \zeta(s)\tag{9}.$$

Rather I believe
$$h(x)=\frac{2}{a x} \sum\limits_{n=1}^{\infty} \cos\left(\frac{2 n \pi}{a x}\right)=\frac{1}{a x} \sum\limits_{n=1}^\infty \delta\left(\frac{1}{a x}-n\right),\quad x>0\tag{10}$$
and note the term-wise Mellin transform
$$\mathcal{M}_x\left[\frac{1}{a x}\, \delta\left(\frac{1}{a x}-n\right)\right](s)=\int\limits_0^{\infty} \frac{1}{a x}\, \delta \left(\frac{1}{a x}-n\right) \, x^{s-1} \, dx=(a n)^{-s}\tag{11}$$
is consistent with
$$\mathcal{M}_x\left[h(x)\right](s)=\int\limits_0^{\infty} h(x)\, x^{s-1} \, dx=a^{-s} \sum\limits_{n=1}^\infty n^{-s}=a^{-s}\, \zeta(s)\,,\quad\Re(s)>1\tag{12}.$$


Find $P(X>Y)$ if $X,Y$ are independent exponential random variables

Question: Let $ X,Y $ be two independent exponential random variables with means $ 1 $ and  $3$, respectively. Find  $P(X>Y)$.
I know $F(X,Y)$ is $\iint 3e^{-x-3y}\,dx\,dy$. But I'm confused on the limits of $P(X>Y)$.
Comment: You should have said something like: "I know that the probability/integral asked for is ..." It is totally unclear what you mean with $F(X,Y)$. The uppercase $F$ smells like a CDF, but that is contradicted again by the fact that the arguments $X,Y$ are random variables.
Comment: https://math.stackexchange.com/q/2233955/321264, https://math.stackexchange.com/q/2561014/321264, https://math.stackexchange.com/q/1571684/321264
Answer: It helps to draw a picture of the plane, and draw the region containing points $(x,y)$ such that $x<y$.

 In the end, either $\int_0^\infty \int_y^\infty \cdots \, dx \, dy$ or $\int_0^\infty \int_0^x \cdots \,dy \, dx$ will workwork.
Answer: We want $x\ge y$ for the integral, and $y$ should range from $0$ to $\infty$. So the integral is
$$\int_{y=0}^{\infty}\int_{x=y}^{\infty}\frac{1}{3}e^{-x-\frac{1}{3}y}\, dx \, dy.$$
(I realised the PDF you wrote was incorrect because we are told the means. If an exponential random variable has mean $\beta$, then its PDF is $\frac{1}{\beta}e^{-\frac{x}{\beta}}$ (for $x \ge 0$).)
Comment: To get a probability like this, we integrate the joint PDF $f(x,y)$ over the region corresponding to the event, which in this example is the region $\left\{(x,y)\in \Bbb{R}^2 : y > 0 \text{ and } x > y\right\}$. To integrate over this region, if you draw a diagram of the region in the $x$-$y$ plane, you can see that $y$ varies from $0$ to $\infty$ (the outer integral), and for any fixed $y > 0$, we have $x$ vary from $y$ to $\infty$ (the inner integral). Therefore, the integral we want is equal to $$\int_{y=0}^{\infty}\int_{x=y}^\infty f(x,y)\, dx \, dy.$$
Comment: How did you get the double integral? Is it from the definition of PDF for continuous random variables?
Answer: This answer is not completely okay. It assumes that $\lambda=3$ (this on base of the integral that was mentioned in the question) while actually $\lambda=\frac13$. The idea is the same, though and I have decided to leave it like this. See the answer of Minus One-Twelfth (who observed the mistake and edited) for a correct version.

Let $\chi(x,y)$ take value $1$ if $x>y$ and take value $0$ otherwise.
Then here:
$$P(X>Y)=\mathbb E[\chi(X,Y)]=\int\int\chi(x,y)f_X(x)f_Y(y)dxdy=\int_0^{\infty}\int_y^{\infty}3e^{-x-3y}dxdy$$

edit (I could not resist the comment)
$\begin{aligned}\int_{0}^{\infty}\int_{y}^{\infty}3e^{-x-3y}dxdy & =3\int_{0}^{\infty}e^{-3y}\int_{y}^{\infty}e^{-x}dxdy\\
 & =3\int_{0}^{\infty}e^{-3y}\left[-e^{-x}\right]_{y}^{\infty}dy\\
 & =3\int_{0}^{\infty}e^{-3y}e^{-y}dy\\
 & =3\int_{0}^{\infty}e^{-4y}dy\\
 & =3\left[-\frac{1}{4}e^{-4y}\right]_{0}^{\infty}\\
 & =\frac{3}{4}
\end{aligned}
$
Comment: on intragrating this $P(X>Y)=\mathbb E[\chi(X,Y)]=\int\int\chi(x,y)f_X(x)f_Y(y)dxdy=\int_0^{\infty}\int_y^{\infty}3e^{-x-3y}dxdy$ the question gives the value is  $e^(-3y)$  then what is the value of y
Comment: thanks brother you help me a lot.
Comment: I edited the way how to do it. Check me on mistakes, though.
Comment: i got the answer this  1-P(X<Y) =1-0.75=0.25
Comment: I see now that the *mean* of $Y$ is $3$ which means that its parameter is $\lambda=\frac13$. That is the source of the mistake. $0.25$ is correct if $\lambda=\frac13$ and $0.75$ would have been correct if $\lambda=3$.
Answer: we have $$f_X(x) = e^{-t}, f_Y(y) = \frac{1}{3}e^{-\frac{y}{3}}$$
Hence $$P(Y \lt y) = F_Y(y) = \int_{-\infty}^{y} f_Y(y^\prime) dy^\prime = \int_{0}^{y} \frac{1}{3}e^{-\frac{y^\prime}{3}} dy\prime = 1 - e^{\frac{y}{3}}$$
So
$$P(X>Y) = \int_{0}^{\infty} \int_{0}^{t} f_X(t) f_Y(y) dydt = \int_{0}^{\infty}f_X(t)P(Y \lt t)dt$$ $$= \int_{0}^{\infty}e^{-t}(1-e^{-t/3})dt = \int_{0}^{\infty}e^{-t}-e^{-4t/3}dt=1/4$$


Negative values in predictions for an always-positive response variable in linear regression

Question: I'm trying to predict a response variable in linear regression that should be always positive (cost per click). It's a monetary amount. In adwords, you pay google for clicks on your ads, and a negative number would mean that google pays you when people clicked :P
The predictors are all continuous values. The Rsquared and RMSE are decent when compared to other models, even out-of-sample:
<code>  RMSE        Rsquared
1.4141477     0.8207303
</code>
I cannot rescale the predictions, because it's money, so even a small rescaling factor could change costs significantly.
As far as I understand, for the regression model there's nothing special about zero and negative numbers, so it finds the best regression hyperplane no matter whether the output is partly negative.
This is a very first attempt, using all variables I have. So there's room for refinement.
Is there any way to tell the model that the output cannot be negative?
Comment: You can ensure positive predictions by using a generalized linear model with logarithmic link function. By the way, although your $R^2$ value is quite encouraging, a better check of whether the model follows the main shape of the data is a plot of residual vs predicted. Plots of observed vs predicted may also help illuminate your problem.
Comment: @NickCox gave one suggestion. I would plot the data in more ways than just residual vs. predicted. However, you can certainly rescale money variables. One common method is to take log(cost) as the dependent variable. (I think this winds up equivalent to the log link function, but might be easier to comprehend). Log(cost) can, of course, be negative. And logs of money variables are often sensible because, e.g. a difference between 0.01 and 0.02 per click is important, but difference between 1.01 and 10.2 per click is not.
Comment: @Peter Flom I think meant 1.02 not 10.2.
Comment: A little sample data would help people illustrate potential solutions.
Answer: I assume that you are using the OLS estimator on this linear regression model. You can use the inequality constrained least-squares estimator, which will be the solution to a minimization problem under inequality constraints. Using standard matrix notation (vectors are column vectors) the minimization problem is stated as
$$\min_{\beta} (\mathbf y-\mathbf X\beta)'(\mathbf y-\mathbf X\beta) \\s.t.-\mathbf Z\beta \le \mathbf 0 $$
...where $\mathbf y$ is $n \times 1$ , $\mathbf X$ is $n\times k$, $\beta$ is $k\times 1$ and $\mathbf Z$ is the $m \times k$ matrix containing the out-of-sample regressor series of length $m$ that are used for prediction. We have $m$ linear inequality constraints (and the objective function is convex, so the first order conditions are sufficient for a minimum).
The Lagrangean of this problem is
$$L =  (\mathbf y-\mathbf X\beta)'(\mathbf y-\mathbf X\beta) -\lambda'\mathbf Z\beta = \mathbf y'\mathbf y-\mathbf y'\mathbf X\beta -  \beta'\mathbf X'\mathbf y+ \beta'\mathbf X'\mathbf X\beta-\lambda'\mathbf Z\beta$$
$$= \mathbf y'\mathbf y -  2\beta'\mathbf X'\mathbf y+ \beta'\mathbf X'\mathbf X\beta-\lambda'\mathbf Z\beta $$
where $\lambda$ is a $m \times 1$ column vector of non-negative Karush -Kuhn -Tucker multipliers. The first order conditions are (you may want to review rules for matrix and vector differentiation)
$$\frac {\partial L}{\partial \beta}= \mathbb 0\Rightarrow  -  2\mathbf X'\mathbf y +2\mathbf X'\mathbf X\beta - \mathbf Z'\lambda  $$
$$\Rightarrow \hat \beta_R = \left(\mathbf X'\mathbf X\right)^{-1}\mathbf X'\mathbf y + \frac 12\left(\mathbf X'\mathbf X\right)^{-1}\mathbf Z'\lambda = \hat \beta_{OLS}+ \left(\mathbf X'\mathbf X\right)^{-1}\mathbf Z'\xi \qquad [1]$$
...where $\xi = \frac 12 \lambda$, for convenience, and $\hat \beta_{OLS}$ is the estimator we would obtain from ordinary least squares estimation.
The method is fully elaborated in Liew (1976).
Comment: I have upvoted this because it's a legitimate solution, but it is risky in practice. After all, the solution is arbitrarily sensitive to values of $\mathbf Z$: a single high-leverage value will steer the estimates far from a decent fit merely to enforce the constraint. Thus, at a minimum, this procedure *must* be accompanied by a careful goodness-of-fit test to the data.
Comment: @whuber You are right. So, OP, tread carefully here.


cannot find WebDriverWait api in selenium-webdriver when using typescript

Question: I am automating the various functionalities of the  mobile app using Typescript+Appium+SeleniumWebdriver+nodejs for this I need to use WebDriverWait
I am unable to use the WebDriverWait as when I try to import it errors out
So if I use import {WebDriverWait} from 'selenium-webdriver';
what I have tried is to first uninstall the selenium-webdriver and installed it back but still see this issue
npm uninstall selenium-webdriver
npm install email@example.com

import {WebDriverWait} from 'selenium-webdriver';

this does not work :
WebDriverWait' is declared but its value is never read.ts(6133)
Module '"../../../../../../../../../******/****/node_modules/@types/selenium-webdriver"' has no exported member 'WebDriverWait'.ts(2305)

Answer:
node_modules/@types/selenium-webdriver has no exported member 'WebDriverWait'

This error means that the types for selenium-webdriver were found, but the specific item you're trying to import isn't included in them.
You can check the types by opening index.d.ts in that path, or via github here: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/selenium-webdriver/index.d.ts
The error is correct: WebDriverWait isn't there. That either means that the types are wrong (always possible: they're written by hand), or that this property really doesn't exist.
In this case, it's the latter: WebDriverWait isn't exported by the module at all. You can see this by testing with:

Run npm install selenium-webdriver
Start node, and run:

> require('selenium-webdriver').WebDriverWait
undefined

I suspect you're following a guide for Python or Java, which do use this name when waiting on certain conditions. In JavaScript (and thus in TypeScript too) you don't need that. Instead, to wait for a condition you just need to run something like:
driver.wait(until.titleIs('My Title'), 1000);

It doesn't look like the current Selenium docs cover this case for JS, but the readme on npm has some useful examples: https://www.npmjs.com/package/selenium-webdriver