Create a multistep Webform and set to "non-linear" with sidebar showing which steps have been completed?

Question: I have several steps in my Webform that require certain values to pass each step.
This is done by Webform Validation and seems to work fine, however how can I turn the Webform into something that's "non-linear" allowing users to jump to any page they want to complete with ease.
Ideally I'd want to move the titles for each multistep page to a sidebar menu which would show the status of whether that page is completed or not.
Is there any modules that do something similar already?
Answer: I searched for how to create this functionality, and found this on the drupal site Link for creating multi-step webforms
Comment: Well I have a multistep Webform already set-up, but I don't want the "next" and "back" buttons. I want each step to act like a page, and then to use a sidebar menu that shows the steps but set that menu to use a different CSS class depending on whether that step/page is completed (validated).


Isolate scope binding in angular directive

Question: I'm trying to build a simple directive that creates a link on the page to google with dynamic values via the '@' binding strategy, I have it working, but I'm not sure what '@' is doing.
My directive looks like this:
(function() {
var app = angular.module('app', []);

app.directive('myDirective', function() {
    return {
        restrict: 'A',
        replace: true.
        scope: {
            myUrl: '@', // binding
            myLinkText: '@'
        },
        template: '{{myLinkText}}'
    }
});

})();

And here's my HTML:

How is this different from creating a controller and why do we need to do this in directives? In other words, why do we need to create another scope? I'm not understanding the concept of isolate scope and binding. Thanks for your help.
Answer: By default, your directive uses scope inheritance.  This means that if a scope variable is not defined in the current scope, it will search for the variable in the parent scope, and then its parent scope, until finally, it stops searching at the $rootScope.  Isolating your scope does not allow scope inheritance - it is a way to make your directive more self contained, and more modular with fewer dependencies.  The most common way that variables from your outside scope are imported to your isolated scope is through the attributes.  This is where the @ comes into play. There three ways to import a scope variable:

@ - As a string (which may be interpolated).
= - With two-way binding to a model object.
& - As an expression that is evaluated in parent scope (mainly for method calls that evaluated in parent scope).


Localizations of quotients of polynomial rings (2) and Zariski tangent space

Question: I am sorry, in the whole text below $k$ is just meant to be $\mathbb{C}$.
This question is closely related to my previous one here.
I am considering the two rings $k[X]=k[x,y,z]/\langle xy,xz,yz\rangle$ and $k[Y]=k[x,y]/\langle xy(x-y)\rangle$. The question is if $X\subseteq\mathbb{C}^3$ and $Y\subseteq\mathbb{C}^2$ are isomorphic as schemes, and a hint is to define and compute the tangent space of such a scheme at the origin.
If I regard $X$ as an affine scheme $\operatorname{Spec}k[X]$, and if $\mathcal{O}_X$ is its structure sheaf, then the origin 'is' the maximal ideal $\mathfrak{p}=\langle x,y,z\rangle\subseteq k[X]$. Then $\mathcal{O}_{X,\mathfrak{p}}\cong k[X]_\mathfrak{p}$, which is a local ring with maximal ideal $\mathfrak{m}:=\mathfrak{m}_{X,\mathfrak{p}}=\mathfrak{p}k[X]_\mathfrak{p}$. I can then define the tangent space of $X$ at the origin as the dual space of $\mathfrak{m}_{X,\mathfrak{p}}/\mathfrak{m}_{X,\mathfrak{p}}^2$ I guess.
But what is the standard procedure to compute such tangent spaces? Can one do this without 'computing' the ring $k[X]_\mathfrak{p}$ first (maybe by finding a basis of the tangent space by looking at $\mathfrak{m}$ directly)? In any case I would like to know what $k[X]_\mathfrak{p}$ and $k[Y]_{\langle x,y\rangle}$ are, but as usual I can't think of a way to 'simplify' these. Does it ever help that localization commutes with taking quotients in actual computations like these?
The maximal ideal $\mathfrak{m}$ consists of fractions $\frac{f}{g}$, where $f\in\mathfrak{p},g\notin\mathfrak{p}$, i.e. where $f$ doesn't have a constant term while $g$ has a non-zero one. $\mathfrak{m}^2$ has as generators products of two elements of the above form, in particular the numerator has only degree-2 or higher terms. So elements in the quotient have a numerator with only degree-1 terms? This would be consistent with the naive $\mathfrak{m}/\mathfrak{m}^2=\mathfrak{p}_\mathfrak{p}/(\mathfrak{p}_\mathfrak{p})^2=(\mathfrak{p}/\mathfrak{p}^2)_\mathfrak{p}$, from which I don't really know if I am allowed to calculate like this.
As you can see, I'm confused ;) Answers to the various questions above would be very much appreciated, as wells as hints on how to go on with computing the tangent space, and on how to compute the local rings above. My (very vague) guess would be that we get a basis of the dual of the tangent space of $X$ at the origin by taking $x/1,y/1,z/1$, hence a 3-dimensional vector space, which for $Y$ can never happen, as this should be the only way to show $X$ and $Y$ are not isomorphic via tangent spaces at a certain point!? But I could also be totally wrong here.
Edit: Again, I forgot the geometry: Can I guess from the geometric picture ($X$ is the union of the three coordinate axes in $\mathbb{C}^3$, while $Y$ consists of the two coordinate axes plus the line $y=x$ in $\mathbb{C}^2$) what the tangent spaces at these two (clearly singular) points should be?
Thank you in advance!
Answer: Your intuition is 100% correct and I'll just help you write down the argument  formally.
First let us simplify our lives. The Zariski cotangent space  is the right and natural invariant : dualizing in order to get the tangent space is useless and can only lead to loss of information.
a) Let us begin with the plane curve $Y=Spec(A/I)$ with $A=k[x,y],\;I=(xy(x-y)) $ .
  Call $M=(x,y)\subset k[x,y], \;\mathfrak m=(\bar x, \bar y)\subset A$ and $\mathfrak n=\mathfrak m A_{\frak m}\subset  A_{\frak m}$.
The cotangent space at $O$ (corresponding to $\mathfrak m$)  of $Y$ is $\mathfrak n/\mathfrak n^2=\mathfrak m/\mathfrak m^2=(\bar x, \bar y)/(\bar x^2, \bar x \bar y,\bar x^2)\cong M/M^2=(x,  y)/( x^2,  xy, x^2)\cong k^2$ .
b) Let us now  study $X=Spec(B/J)$ with $B=k[x,y,z],\;J=(xy, xz,yz) $
Call $P=(x,y,z)\subset k[x,y,z], \;\mathfrak p=(\bar x, \bar y, \bar z)\subset B$ and $\mathfrak q=\mathfrak p B_{\frak q}\subset  B_{\frak p}$.
The cotangent space at $O'$ (corresponding to $\mathfrak p$)  of $X$ is
$\mathfrak q/\mathfrak q^2=\mathfrak p/\mathfrak p^2=(\bar x, \bar y,\bar z)/(\bar x^2, \bar x \bar y,...,\bar z^2)\cong M/M^2=(x, y,z)/( x^2,  xy, ,..., z^2)\cong k^3$
Conclusion
Since no point of $Y$ has a cotangent space of dimension 3, and since $X$ has such a point $O'$, the schemes $X$ and $Y$ are not isomorphic.
[Note that the calculation in a) was actually not necessary: a subscheme of $\mathbb A^2_k$ has cotangent space of dimension $\leq 2$ at any point. However I wanted to make that computation as a warm-up for b)]
Edit: as an answer to Frederik's comment below, let me mention  that if  $Y\subset Z$ is a subvariety and $y\in Y$ , we have  $dim(Y)\leq dim_k (T^*_y(Y))\leq dim_k(T^*_y(Z))$ and if $ Z=\mathbb A^n_k$, then $dim_k(T^*_y(\mathbb A^n_k))=n$
Commutative algebra reminder
I have used that if  $\mathfrak m\subset A$ is a maximal ideal of a ring and if $\mathfrak n=\mathfrak m A_{\frak m}\subset  A_{\frak m}$, then $\mathfrak n/\mathfrak n^2=\mathfrak m/\mathfrak m^2$. You can find this on page 24 of Milne's freely available algebraic geometry notes (with however the superfluous condition  that $A$ be an integral noetherian domain).
Comment: Hello @Georges, and thank you once again for a very encouraging and helpful answer! I'm going to check Milne's proof of that CA fact now. As a side-question: So the actual computation of those tangent spaces becomes more difficult once you drop e.g. 'noetherian'. Does that definition of (co)tangent space still make sense in case of a general affine scheme? And couldn't one define this cotangent space even for non-closed points of such an affine scheme just the same way?
Comment: I hope I'm not being too demanding here, but another - hopefully short - question came up. Regarding how one proves that those two schemes (and thus rings) are not isomorphic, I guess 'computing' (simplifying) the actual localized rings themselves and checking if they are isomorphic is rather impracticable in general (working with the maximal ideal seems simpler)?
Comment: @Georges: I have a (potentially dumb) question. Hartshorne states in prop 5.2A that $\dim_k m/m^2 \geq \dim A$ for a noetherian local ring $A$. You seem to state the reversed inequality in the paragraph with header "conclusion". How is this?
Comment: @Fredrik: That confused me a bit, too, but I think it's not a contradiction here. In the case of $Y$, the only non-regular point is the origin, where the cotangent space has dimension 2, which is greater than $\dim(Y)=\dim k[Y]=1$. The dimension of the cotangent space can still have an upper bound, while being bounded by $\dim k[Y]$ from below, or not? At least that's how I understood it :)
Comment: Dear @Rand, it is unfortunate that Milne mentions noetherianity: it is totally irrelevant for his lemma (and I think it isn't even used in his own proof !)
Comment: @Randal'Thor: After thinking it through, I realized of that "of course" the dimension of the cotanget space is bounded by the number of variables in the ambient space. What Hartshorne states is that the dimension is greater than or equal to the dimension of the *variety*
Comment: @Fredrik: Yes, that's what I meant there (the origin being the only non-singular point of the variety $Y$ means there the dimension must be greater than $\dim(Y)=1$, which is what you mentioned), while still the dimension of the cotangent space has a 'natural' upper bound. By the way: funny that I am not the only one often writing 'tanget' when I'm trying to write 'tangent' ;)
Comment: Dear @Frederik: first, your question is not dumb at all but quite pertinent. Hartshorne is quite correct: we actually have two inequalities . I have added a small edit in my answer addressing your concern.
Comment: Dear @Frederik and Thor, since this answer and its comments are getting quite long, I  suggest for the sake of clarity that you (and/or other users) open new  questions if you want to know more about this topic of  differential calculus in algebraic geometry .


Django: ordering numerical value with order_by

Question: I'm in a situation where I must output a quite large list of objects by a CharField used to store street addresses.
My problem is, that obviously the data is ordered by ASCII codes since it's a Charfield, with the predictable results .. it sort the numbers like this;
1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21....

Now the obvious step would be to change the Charfield the proper field type (IntegerField let's say), however it cannot work since some address might have apartments .. like "128A".
I really don't know how I can order this properly ..
Comment: Curious to know if you found a solution to this. Many thanks. N
Comment: This solution is working fine on my side: https://stackoverflow.com/questions/54789490/django-order-by-char-and-numerical/54797177
Answer: If you're sure there are only integers in the field, you could get the database to cast it as an integer via the extra method, and order by that:
MyModel.objects.extra(
    select={'myinteger': 'CAST(mycharfield AS INTEGER)'}
).order_by('myinteger')

Comment: Since django is deprecating `extra()`, here's how to do the same thing using `annotate()`: `MyModel.objects.annotate(myinteger=RawSQL('CAST(mycharfield AS UNSIGNED)', params=[])).order_by('myinteger')`
Comment: Use 'SIGNED' or 'UNSIGNED' instead of INTEGER if your MYSQL version doesn't support it.
Comment: Not all addresses start with a number. This approach works for special-case of "I have numbers in my char field", but will fail to sort mixed data.
Comment: Very interesting use of extra, I rarely play with that method.. But it doesn't seem to work in my situation unfortunately.
Answer: Django is trying to deprecate the extra() method, but has introduced Cast() in v1.10. In sqlite (at least), CAST can take a value such as 10a and will cast it to the integer 10, so you can do:
from django.db.models import IntegerField
from django.db.models.functions import Cast

MyModel.objects.annotate(
    my_integer_field=Cast('my_char_field', IntegerField())
).order_by('my_integer_field', 'my_char_field')

which will return objects sorted by the street number first numerically, then alphabetically, e.g. ...14, 15a, 15b, 16, 16a, 17...
Comment: `Cast` doesn't support values such as `10a`. If it receives one, it throws `DataError: invalid input syntax for integer`. I found a workaround by removing all characters in the value that are not numbers (PostgreSQL):

`from django.db.models.expressions import F, Value, Func`

`queryset.annotate(my_integer_field=Cast(

    Func(F('my_char_field'),
    Value('[^\d]'), Value(''), Value('g'),
    function='regexp_replace'),

    IntegerField())
)`
Comment: @Filly can you give the full example that threw the error?
Comment: @practual your solution works on Sqlite3 but gives error on PostgreSQL like in Filly ' comment.
Comment: when all the values are numeric, it works fine, but when there is a string value on results:

invalid input syntax for type integer: "x13079"
Answer: If you're using PostgreSQL (not sure about MySQL) you can safely use following code on char/text fields and avoid cast errors:

MyModel.objects.extra(
    select={'myinteger': "CAST(substring(charfield FROM '^[0-9]+') AS INTEGER)"}
).order_by('myinteger')

Comment: This is the best if you have to sort by strings that have numbers, with a pattern '.[0-9]+'
Comment: The syntax for MariaDB would be: "CAST(REGEXP_SUBSTR(name, '^[0-9]+') AS INTEGER)".  Thanks!!
Comment: Awesome answer, how can I do this when I am looking up a foreign key for that field?
Comment: This does not work for me, the error that I get is: `ProgrammingError: syntax error at or near "["
LINE 1: ...ECT DISTINCT ('SELECT CAST(substring(value FROM '^[0-9]+') A...`. I use PostgreSQL 11, Python 2.7 and Django 1.11. I think it may be because of old versions I have.
Answer: I know that I'm late on this, but since it's strongly related to the question, and that I had a hard time finding this:
You have to know that you can directly put the Cast in the ordering option of your model.
from django.db import models
from django.db.models.functions import Cast

class Address(models.Model):

    street_number = models.CharField()

    class Meta:
        ordering = [
            Cast("street_number", output_field=models.IntegerField()),
        ]

From the doc about ordering:

You can also use query expressions.

And from the doc about database functions:

Functions are also expressions, so they can be used and combined with other expressions like aggregate functions.
Answer: Great tip! It works for me! :) That's my code:
revisioned_objects = revisioned_objects.extra(select={'casted_object_id': 'CAST(object_id AS INTEGER)'}).extra(order_by = ['casted_object_id'])

Answer: The problem you're up against is quite similar to how filenames get ordered when sorting by filename. There, you want "2 Foo.mp3" to appear before "12 Foo.mp3".
A common approach is to "normalize" numbers to expanding to a fixed number of digits, and then sorting based on the normalized form. That is, for purposes of sorting, "2 Foo.mp3" might expand to "0000000002 Foo.mp3".
Django won't help you here directly. You can either add a field to store the "normalized" address, and have the database order_by that, or you can do a custom sort in your view (or in a helper that your view uses) on address records before handing the list of records to a template.
Answer: In case you need to sort version numbers consisting of multiple numbers separated by a dot (e.g. 1.9.0, 1.10.0), here is a postgres-only solution:
class VersionRecordManager(models.Manager):

    def get_queryset(self):
        return super().get_queryset().extra(
            select={
                'natural_version': "string_to_array(version, '.')::int[]",
            },
        )

    def available_versions(self):
        return self.filter(available=True).order_by('-natural_version')

    def last_stable(self):
        return self.available_versions().filter(stable=True).first()

class VersionRecord(models.Model):
    objects = VersionRecordManager()
    version = models.CharField(max_length=64, db_index=True)
    available = models.BooleanField(default=False, db_index=True)
    stable = models.BooleanField(default=False, db_index=True)

In case you want to allow non-numeric characters (e.g. 0.9.0 beta, 2.0.0 stable):
def get_queryset(self):
    return super().get_queryset().extra(
        select={
            'natural_version':
                "string_to_array(                     "
                "   regexp_replace(                   "  # Remove everything except digits
                "       version, '[^\d\.]+', '', 'g'  "  # and dots, then split string into
                "   ), '.'                            "  # an array of integers.
                ")::int[]                             "
        }
    )

Answer: In my case i have a CharField with a name field, which has mixed (int+string) values, for example. "a1", "f65", "P", "55" e.t.c ..
Solved the issue by using the sql cast (tested with postgres & mysql),
first, I try to sort by the casted integer value, and then by the original value of the name field.
parking_slots = ParkingSlot.objects.all().extra(
        select={'num_from_name': 'CAST(name AS INTEGER)'}
    ).order_by('num_from_name', 'name')

This way, in any case, the correct sorting works for me.
Answer: I was looking for a way to sort the numeric chars in a CharField and my search led me here. The name fields in my objects are CC Licenses, e.g., 'CC BY-NC 4.0'.
Since extra() is going to be deprecated, I was able to do it this way:
MyObject.objects.all()
    .annotate(sorting_int=Cast(Func(F('name'), Value('\D'), Value(''), Value('g'), function='regexp_replace'), IntegerField()))
    .order_by('-sorting_int')

Thus, MyObject with name='CC BY-NC 4.0' now has sorting_int=40.
Comment: any idea on how to handle empty string?
Answer: All the answeres in this thread did not work for me because they are assuming numerical text. I found a solution that will work for a subset of cases. Consider this model
Class Block(models.Model):
      title = models.CharField()

say I have fields that sometimes have leading characters and trailing numerical characters If i try and order normally
 >>> Block.objects.all().order_by('title')
<QuerySet [<Block: 1>, <Block: 10>, <Block: 15>, <Block: 2>, <Block: N1>, <Block: N12>, <Block: N4>]>

As expected, it's correct alphabetically, but makes no sense for us humans. The trick that I did for this particular use case is to replace any text i find with the number 9999 and then cast the value to an integer and order by it.
for most cases that have leading characters this will get the desired result. see below
from django.db.models.expressions import RawSQL

>>> Block.objects.all()\
.annotate(my_faux_integer=RawSQL("CAST(regexp_replace(title, '[A-Z]+', '9999', 'g') AS INTEGER)", ''))\
.order_by('my_faux_integer', 'title')

<QuerySet [<Block: 1>, <Block: 2>, <Block: 10>, <Block: 15>, <Block: N1>, <Block: N4>, <Block: N12>]>

Answer: I have a similar situation.  An old database has a table with mostly numeric data in a column (named page) with type varchar.  I need to sort the results queried from that table in a natural numeric order, but I don't need the data type changed in the results.  I ended up converting the column value to a number for the sorting only:
(ItemPage.objects.filter(volume__id=volumeId)
.order_by('item__topic__name', F('page') * 1, 'item__name'))

I see some other answers here indicate that MySQL's CAST() may not work with strings containing a mixture of digitis and alphabetics, but from experiments, I don't think that's true.  Maybe it's a problem for older versions of MySQL.
Whether that's a problem or not, I decided to multiply the value by one instead.  It works well for me.  At some point, I'll experiment with CAST() and see whether it always works and does it execute faster than multiplying by one.


Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. ReactJS

Question: Eu estou realizando um projeto de um bootcamp e me deparei com esse problema.
Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
A parte que está dando erro é essa:
service.nearbySearch(request, (results, status) => {
  if (status === google.maps.places.PlacesServiceStatus.OK) {
    dispatch(setRestaurants(results));
}

E o componente inteiro é esse:

import React, { useState, useEffect, useCallback } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { GoogleApiWrapper, Map, Marker } from 'google-maps-react';

import { setRestaurants, setRestaurant } from '../../redux/modules/restaurants';

export const MapContainer = (props) => {
  const dispatch = useDispatch();
  const { restaurants } = useSelector((state) => state.restaurants);
  const [map, setMap] = useState(null);
  const { google, query, placeId } = props;

  const searchByQuery = useCallback(
    (map, query) => {
      const service = new google.maps.places.PlacesService(map);

      const request = {
        location: map.center,
        radius: '2000',
        type: ['restaurant'],
        query,
      };

      service.textSearch(request, (results, status) => {
        if (status === google.maps.places.PlacesServiceStatus.OK) {
          dispatch(setRestaurants(results));
        }
      });
    },
    [google, dispatch]
  );

  const getRestaurantById = useCallback(
    (placeId) => {
      const service = new google.maps.places.PlacesService(map);

      const request = {
        placeId,
        fields: ['name', 'opening_hours', 'formatted_address', 'formatted_phone_number'],
      };

      service.getDetails(request, (place, status) => {
        if (status === google.maps.places.PlacesServiceStatus.OK) {
          dispatch(setRestaurant(place));
        }
      });
    },
    [google, map, dispatch]
  );

  useEffect(() => {
    if (query) {
      searchByQuery(map, query);
    }
  }, [map, query, searchByQuery]);

  useEffect(() => {
    if (placeId) {
      getRestaurantById(placeId);
    }
  }, [placeId, getRestaurantById]);

  const searchNearby = (map, center) => {
    const service = new google.maps.places.PlacesService(map);

    const request = {
      location: center,
      radius: '20000',
      type: ['restaurant'],
    };

    service.nearbySearch(request, (results, status) => {
      if (status === google.maps.places.PlacesServiceStatus.OK) {
        dispatch(setRestaurants(results));
      }
    });
  };

  function onMapReady(_, map) {
    setMap(map);
    searchNearby(map, map.center);
  }

  return (
    <Map
      google={google}
      centerAroundCurrentLocation
      onReady={onMapReady}
      onRecenter={onMapReady}
      {...props}>
      {restaurants.map((restaurant) => (
        <Marker
          key={restaurant.place_id}
          name={restaurant.name}
          position={{
            lat: restaurant.geometry.location.lat(),
            lng: restaurant.geometry.location.lng(),
          }}
        />
      ))}
    </Map>
  );
};

export default GoogleApiWrapper({
  apiKey: process.env.REACT_APP_GOOGLE_API_KEY,
  language: 'pt-BR',
})(MapContainer);

Já tentei colocar dentro de um UseEffect, porém era necessário ele no onMapReady
Answer: Provável que aqui neste bloco:

service.nearbySearch(request, (results, status) => {
  if (status === google.maps.places.PlacesServiceStatus.OK) {
    dispatch(setRestaurants(results));
}

O service.nearbySearch() tem um laço while que não acabe dentro dele, ou algo no seu código está sendo alterado em um luping infinito e renderizado na página...


Question regarding definitionof compactification

Question:
Definition: A compactification of a topological space $X$ is an ordered pair $\left(Y,h\right)$, where
$$(1)\quad Y\text{ if a compact } T_2\text{-space}$$
$$(2)\quad h\colon X\to Y\text{ is an embedding}$$
$$(3)\quad h[X]\text{ is dense in } Y;\text{ }\overline{h[X]}=Y.$$

My question is, does $X\subseteq Y$ need to be true? I ask this because the unit circle $\mathbb{S}^1$ is a compactification of $(0,1)$, though, its's simply not true that $(0,1)\subseteq\mathbb{S}^1$....
Comment: It means that $X$ and $h(X)$ are topologically the same. (Not $X$ and $Y$, unless $X$ is already compact, in which case $h(X)=Y$.)
Comment: $h:(0,1)\to S^1$, $h(x) = (\cos(2\pi x), \sin(2\pi x))$ is an embedding
Comment: @RyanK: Thanks, but doesn't really answer the question, i.e. does it need not be that $X\subseteq Y$?
Comment: Ahh sorry, $h[X]\subseteq Y$ but not necessarily $X\subseteq Y$ as this example illustrates.
Comment: @RyanK: I figured, just wanted to make sure. Thanks!
Comment: the definition doesn't specify that $X$ needs to be a subset of $Y$. But, $h(X)$ is s subset of $Y$, and $h(X)$ is homeomorphic to $X$, so a copy of $X$, namely $h(X)$ is indeed a subset of $Y$.
Comment: @Mirko: So $X\cong_\text{hom.}h[X]\subseteq Y$, does this mean topologically $X$ and $Y$ are the same?
Answer: No, but $Y$ does contain a "topological copy" of $X$, to wit $h[X]$, which is homeomorphic to $X$ (by the definition of an embedding). It's technically convenient not to have $X \subseteq Y$, it allows for more flexible ways to construct compacifications. And the $h$ is the way $X$ sits in $Y$, as it were. The notion of equivalent compactifications uses the embedding. It's to make the notion "categorical", if you know categories.
Comment: Thanks! I have little knowledge of categories, but are you saying the morphisms would be the embedding maps?
Comment: @D.Math In the sense that a map between compactifcations of $X$ has to respect the two embeddings too, in a commutative diagram. So it also involves "arrows" not just objects.


Use everything before a specific character

Question: So, I've been learning C# and I need to remove everything after the
":" character.
I've used a StreamReader to read the text file, but then I can't use the Split function, then I tried it by using an int function to import it, but then again I can't use the Split function?
What I want this to do is import a text file that's written like;
name:lastname
name2:lastname2
And so that it only shows name and name2.
I've been searching this for a couple of days but I can't seem to figure it out!
I don't know what I'm doing wrong and how to import the text file without using StreamReader or anything else.
Edit:
I'm trying to post something to a website that goes like;
example.com/q=(name without ":")
Edit 2:
StreamReader list = new StreamReader(@"list.txt");
string reader = list.ReadToEnd();
string[] split = reader.Split(":".ToCharArray());
Console.WriteLine(split);

gives output as;
System.String[]

Comment: I don't really have an example, I removed all the code that I used.
Comment: Have you looked at [StreamReader.ReadToEnd](https://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend(v=vs.110).aspx)?   That returns a string, which you could then use `Split` on.
Comment: Use String.indexOf and String.substring
Comment: I edited it, thanks for the help. It itsn't solved yet.
Comment: @JeroenVelde if you have 1 entry per line you can just read the line as string and use `.Replace(":", " ");` instead of split https://stackoverflow.com/a/23989887/342740
Answer: You've got a few issues here.  First, use File.ReadLines() instead of a StreamReader, its much simpler and easier:
IEnumerable<string> lines = File.ReadLines("path/to/file");

Next, your lines variable needs to be iterated so you can get to each line of the collection:
foreach (string line in lines)
{
    //TODO: write split logic here
}

Then you have to split each line on the ':' character:
string[] split = line.Split(":");

Your split variable is an array of string (i.e string[]) which means you have to access a specific index of the array if you want to see its value. This is your second issue, if you pass split to Console.WriteLine() under the hood it just calls .ToString() on the object you have passed it, and with a string[] it won't automatically give you all the values, you have to write that yourself.
So if your line variable was: "name:Steve", the split variable would have two indexes and look like this:
//split[0] = "name"
//split[1] = "Steve"

I made a fiddle here that demonstrates.
Comment: Nice job explaining every bit of it!
Comment: Thanks, this helped me fix it. Thank you very much.
Answer: I your file size small and your name:lastname in one line use:
var lines = File.ReadAllLines("filaPath");
foreach (var line in lines)
{
     var array = line.Split(':');
     if (array.Length > 0)
     {
         var name = array[0];
      }
}

if name:lastname isn't in new line tell me how it's seprated


How would I go about port-forwarding on azure microsoft 2012 server [azure]

Question: I'm being in a tough spot, having created 2 different virtual machines on Azure, with windows server 2012 R2 OS. I'm trying to host a game server for a game, which requires ports 7777 and 27015 opened.
What I did is simple, I went into the panel, set-up endpoints for 7777, 27015, for UDP and TCP, and added exceptions to firewall as well for incoming/outcoming 7777, 27015 TCP and UDP.
canyouseeme.org still apparently can't find my service and shows me the ports are not opened. It shows my remote connection port is opened though. What am I doing wrong?  Is there anything more that I need to know?
Image showing forwarded ports
Answer: If you opened the ports on your Firewall and on the Endpoints screen you are probably fine to game. The problem is probably with the utility that you're testing with and not the ports themselves.
I logged onto an Azure VM that I know I can remotely connect to, tested an open port that I know is open with that website, and it said it did not find it. Maybe that site is using Ping, which gets stuck in Azure's load balancer. To test connectivity, try using PSPing. This will let you test connections to specific ports. https://technet.microsoft.com/en-us/sysinternals/bb896649


How can you find the limit of this: $\lim\limits_{x \to \; 0} \frac{x^2}{\sqrt{1+x\sin(x)} \; - \; \sqrt{\cos(x)}}$?

Question: $\lim\limits_{x \to \; 0} \frac{x^2}{\sqrt{1+x\sin(x)} \; - \; \sqrt{\cos(x)}}$
I would start with expanding it with $* \frac{\sqrt{1+x\sin(x)} \; + \; \sqrt{\cos(x)}}{\sqrt{1+x\sin(x)} \; + \; \sqrt{\cos(x)}} \;$ but I don't know how to progress from there.
I can't use L'Hospital's rule. I also have 3 other exercises that are like this but if I can see one solved, I think I will be able to do the other ones as well.
Thanks
Answer: As $x\to0$, famously $\frac{\sin x}{x}\to1$ and $\frac{1-\cos x}{x^2}=\frac12$, so $\frac{x\sin x+1-\cos x}{x^2}=\frac32$ and$$\frac{x^2}{\sqrt{1+x\sin x}-\sqrt{\cos x}}=\frac{x^2}{1+x\sin x-\cos x}(\sqrt{1+x\sin x}+\sqrt{\cos x})=\frac23\cdot2=\frac43.$$
Answer: Hint:
Multiplying numerator and denominator with $\sqrt{1+x\sin x}+\sqrt{\cos x}$, you obtain
$$\frac{x^2}{1+x\sin x-\cos x}\cdot\bigl(\sqrt{1+x\sin x}+\sqrt{\cos x}\mkern1.5mu\bigr). $$
You can rewrite the fraction as
$$\frac 1{\cfrac{1-\cos x}{x^2}+\cfrac{\sin x}x},$$
in which each term in the denominator corresponds to a standard limit.
Answer: hint
After multiplying by the conjugate as you done, the denominator becomes
$$1+x\sin(x)-\cos(x)=$$
$$2\sin(\frac x2)\Bigl(x\cos(\frac x2)+\sin(\frac x2)\Bigr)$$
The function can written as
$$\frac{x}{2\sin(\frac x2)}\frac{1}{\cos(\frac x2)+\frac 1x\sin(\frac x2)}$$
$$×(\sqrt{1+x\sin(x)}+\sqrt{\cos(x)})$$
the limit is then
$$1×\frac{1}{1+\frac 12}×2=\frac 43$$
Answer: I'm going to write it out.
$\lim\limits_{x \to \; 0} \frac{x^2}{\sqrt{1+x\sin(x)} \; - \; \sqrt{\cos(x)}}$
$\lim\limits_{x \to \; 0} \frac{x^2}{\sqrt{1+x\sin(x)} \; - \; \sqrt{\cos(x)}} * \frac{\sqrt{1+x\sin(x)} \; + \; \sqrt{\cos(x)}}{\sqrt{1+x\sin(x)} \; + \; \sqrt{\cos(x)}}$
$\lim\limits_{x \to \; 0} \frac{x^2*\sqrt{1+x\sin(x)} \; + \; x^2*\sqrt{\cos(x)}}{1+x\sin(x) \; - \; \cos(x)}$
$\lim\limits_{x \to \; 0} \frac{x^2*\sqrt{1+x\sin(x)} \; + \; x^2*\sqrt{\cos(x)}}{x^2*\frac{\sin(x)}{x} \; x^2*\frac{1- \cos(x)}{x^2}}$
$\lim\limits_{x \to \; 0} \frac{\sqrt{1+x\sin(x)} \; + \; \sqrt{\cos(x)}}{\frac{\sin(x)}{x} \; +\frac{1- \cos(x)}{x^2}}$
$\frac{\sqrt{1+0}+1}{1+\frac{1}{2}} = \frac{2}{\frac{3}{2}} = \frac{4}{3}$
Comment: Also, how can you make big brackets?


Copiar (clone) um elemento SVG

Question: Quero "clonar" um rect SVG em JavaScript quando clico num botão. Tentei este código, mas não funcionou.
<svg id="svg">
    <rect id="rect" x="5" y="25" width="50" height="50" stroke="#0E0E0E" style="fill:red; stroke-width:1" />
    <text id =txtrect x="5" y="35" font-family="Verdana" font-size="11" fill="white" >
        Rect1
    </text>
</svg>

function clone(){
    var newrect = document.getElementById('rect').cloneNode(true);
    newrect.setAttribute("x", 300);
    newrect.setAttribute("y", 300);
    newrect.style.position = 'absolute';
    document.getElementById("svg").appendChild(newrect);
}

Comment: Pode explicar melhor o "não funcionou"? O seu código funciona para mim. Eventualmente deve mudar o `x` e `y` para valores menores (para não sair fora da tela) e mudar a ID do elemento clonado. Pode mostrar como o seu código chama essa função?
Answer: Primeiro, insira o clone no DOM, depois modifique os atributos (e certifique se de estar chamando a função clone() corretamente no seu código):
function clone(){
  var newrect = document.getElementById('rect').cloneNode(true);
  document.getElementById('svg').appendChild(newrect);
  newrect.setAttribute("x", 100);
  newrect.setAttribute("y", 100);
}

Demonstração:

function clone(){
  var newrect = document.getElementById('rect').cloneNode(true);
  document.getElementById('svg').appendChild(newrect);
  newrect.setAttribute("x", 100);
  newrect.setAttribute("y", 100);
}
<svg id="svg">
  <rect id="rect" x="5" y="25" width="50" height="50" stroke="#0E0E0E" style="fill:red; stroke-width:1" />
  <text id="txtrect" x="5" y="35" font-family="Verdana" font-size="11" fill="white" >
    Rect1
  </text>
</svg>
<button onClick="clone();this.disabled=true">Clonar</button>
Comment: Obrigado, funciona. E agora quero mudar o id do objeto. Tento isso newrect.id = "rect2"; mas nao muda.
Comment: Tente `newrect.setAttribute( 'id', 'rect2' );` após por no DOM. De browser pra browser acho que as implementações variam um pouco.

Copiar (clone) um elemento SVG


Como comparar duas listas por propriedade?

Question: Possuo duas listas de aluno. Gostaria de compará-las pelo atributo Nome, e retornar apenas os valores únicos do aluno01. No exemplo abaixo, retornaria apenas o Aluno01.Nome = "5555".
        List<Aluno1> aluno01 = new List<Aluno1>();
        aluno01.Add(new Aluno1 { Id = 1, Nome = "1111" });
        aluno01.Add(new Aluno1 { Id = 2, Nome = "2222" });
        aluno01.Add(new Aluno1 { Id = 2, Nome = "5555" });

        List<Aluno2> aluno02 = new List<Aluno2>();
        aluno02.Add(new Aluno2 { Id = 1, Nome = "1111" });
        aluno02.Add(new Aluno2 { Id = 2, Nome = "2222" });
        aluno02.Add(new Aluno2 { Id = 2, Nome = "3333" });
        aluno02.Add(new Aluno2 { Id = 2, Nome = "4444" });

Grato,
Answer: Você pode usar using System.Linq;
var intersect = aluno01.Where(a => !aluno02.Select(b => b.Nome).Contains(a.Nome));

Veja funcionando em dotnetfiddle
Comment: Perfeito, era isso!


Problemas com relacionamento de tabelas no laravel

Question: Quando eu digito php artisan migrate ele gera esse erro:
  Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005
  Can't create table `blog`.`produtos` (errno: 150 "Foreign key constraint
  is incorrectly formed") (SQL: alter table `produtos` add constraint
  `produtos_user_id_foreign` foreign key (`user_id`) references `users`
  (`id`) on delete cascade)

  Exception trace:

 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table
 `blog`.`produtos` (errno: 150 "Foreign key constraint is incorrectly

Veja como está o meu código
Schema::create('produtos', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedInteger('user_id');
        $table->foreign('user_id')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');

        $table->string('nome');
        $table->integer('quantidade');
        $table->integer('valor');
        $table->timestamps();
    });

Onde devo estar errando?
Answer: O formato de dado da chave estrangeira deve ser igual do valor externo (chave primária) que etá fazendo relação.
É comum a chave primária da tabela de usuários (blog.users.id) ser criada com o método bigIncrements. Dessa forma, a sua coluna user_id da tabela produtos deve ser criada com o método unsignedBigInteger.
Dessa forma, ambas as colunas que está relacionando serão do formato biginteger(20) unsigned


LTI: How to derive the impulse response of this system?

Question:
Well, i transform g and x into the frequency domain.
u[n] = 1, n ≥ 0
u[n] = 0, n < 0

\begin{aligned}
x[n] & = u[n] \\
h_1[n] & = (\frac{1}{2})^n u[n] \\

g[n] & = (\frac{1}{2})^n u[n] \\

G(e^{j*\phi}) & = \frac{1}{1-0.5 * e^{-j*\phi}} \\
X(e^{j*\phi}) & = \frac{1}{1- e^{-j*\phi}} \\

H(e^{j*\phi}) & = \frac{G(e^{j*\phi})}{X(e^{j*\phi})}

\end{aligned}

But i don't know how to go on.
Comment: $x$ is nowhere defined in your text. Neither are $h_1$ and $h_2$. You'll have to provide more background. Maybe this is standard notation in system/signal theory, but unless a specialist passes by, you'll have to wait for a long time before getting an answer if you leave the question as it is now.
Comment: If I understand the question correctly, you feed the system a Heaviside step function, which is here symbolized by $u[n]$ and as a result you get $g_2[n]$. They ask what the response is when you feed an impulse $\delta[n]$ to the system. But since $\delta[n]=u[n]-u[n-1]$ do you really need to go to the Fourier transform?
Comment: @Raskolnikov i added x[n], h2 i need to calculate. I think u are on the right way, yes. But i am new to signal processing, so i don't know what the best way is to get the solution.
Comment: A couple more remarks, at some point, you use $\phi$ where you actually mean $\omega$. The $\delta$ in your transform of $x$ is not necessary I think. I'm not sure where you get that from. The rest should be okay.
Comment: There was a fourier transform table added to this. And for x[n] the transform was that fraction plus the δ. So just dividing should get me to my solution?
Comment: I could indeed be overlooking something. But not having the problem at hand,it's hard to tell. Just have a look at my answer and see if it helps.
Answer: PART 1: I think you nearly got everything. You know your transmission function in the Fourier domain is:
$$H(\omega)=\frac{G(\omega)}{X(\omega)}=\frac{1- e^{-j\omega}}{1-0.5 e^{-j\omega}} \; .$$
The Fourier transform of $u[n]$ being $X(\omega)$, the Fourier transform of $u[n-1]$ is then
$$\frac{e^{-j\omega}}{1- e^{-j\omega}} \; .$$
Combining everything you get that the Fourier transform of $\delta[n]=u[n]-u[n-1]$ is
$$\frac{1}{1- e^{-j\omega}}-\frac{e^{-j\omega}}{1- e^{-j\omega}} = 1$$
and after passing through your system it will become
$$\frac{1-e^{-j\omega}}{1-0.5 e^{-j\omega}} \; .$$
Working out the components of the series expansion should give you the inverse Fourier transform.
PART 2: As I suggested before, you can solve the exercise without ever doing a Fourier transform. Since the system acts linearly on any input, and $\delta[n]=u[n]-u[n-1]$, it immediately follows that the output is
$$\left(\frac{1}{2}\right)^n u[n] - \left(\frac{1}{2}\right)^{n-1} u[n-1] \; .$$
Comment: Ok, thank you. But i don't understand where i need the δ[n]=u[n]−u[n−1]. Can't i just try to make the inverse Fourier transform out of H(ω)?
Comment: @madmax, in the problem, they ask you to compute the impulse response, which is the effect of the system $H(\omega)$ on a pulse $\delta[n]$. You may be right about that being just $H(\omega)$ since the Fourier transform of a $\delta$ pulse is just $1$. I must have made some mistake there. Can you find the exact definition of the Heaviside step function $u[n]$? I must have made my mistake there.
Comment: @madmax, never mind, I found my mistake. It was in the Fourier transform of $u[n-1]$. I edited my answer.
Comment: hmm, i thought the output is the step response g[n] in this case. Isn't just asked for h[n]? Sorry, but it is not that easy to understand for me right now.
Comment: The step response is given, but they ask for the impulse response, which as you say is $h[n]$. The output in the case of input $u[n]$ is what is called step response $g[n]$, the output in the case of input $\delta[n]$ is what is called impulse response $h[n]$.


External validation of a published Cox PH model

Question: My aim is to externally validate a risk prediction model published in the medical literature that is based on a Cox regression model. I have a dataset with all the variables from the score. I read Prof. Harrell's RMS course notes and am somehow familiar with the rms package in R. I have also searched the resources at https://discourse.datamethods.org/ but I couldn't find an answer on how to proceed.
As far as I understand the documentation of val.surv, I need a fit object generated with cph which I don't have access to so I cannot use that function for external validation. I have read the article Royston P, Altman DG. External validation of a Cox prognostic model: principles and methods. BMC Med Res Methodol. 2013 Mar 6;13:33. which is great but I need some more guidance on how to perform the steps proposed in R.
The first step seems pretty clear. The authors write:
A recognised approach to validation is to estimate the regression coefficient on the PI or risk score in the validation dataset [4,12], sometimes known as the 'calibration slope'.
I calculate a linear predictor (=PI) using the published coefficients beta1-beta3 as follows (please note: I used sample data, not the larger full dataset with many more events for all the code displayed here) :
PI <- x1 * beta1 + x2 * beta2 + x3 * beta3

Then I regress on the PI using a Cox model:
S <- Surv(event = df$event, time = df$time)
cph(S ~ PI, data = df)

## Sample output
Cox Proportional Hazards Model

 cph(formula = S ~ PI, data = df)

                        Model Tests    Discrimination
                                              Indexes
 Obs       950    LR chi2      8.76    R2       0.045
 Events     17    d.f.            1    Dxy      0.383
 Center 2.5133    Pr(> chi2) 0.0031    g        0.921
                  Score chi2   8.00    gr       2.512
                  Pr(> chi2) 0.0047

    Coef   S.E.   Wald Z Pr(>|Z|)
 PI 0.6259 0.2264 2.76   0.0057

I then use the coefficient of PI (in this case 0.6259) to interpret if <1 meaning discrimination is poorer, and > 1 meaning discrimination is better in the validation compared to the derivation set, correct?
And do the R2 and Dxy (and c-statistic calculated as $c = Dxy/2 + 0.5$) I get from this regression refer to the measures of discrimination I would want to interpret for the model performance of my data?
For the second step, authors say *"One reason why the slope on the PI may differ from 1 in the validation dataset is that the regression coefficients for one or more covariates may differ between the datasets. This can be tested formally (ignoring uncertainty of estimates in the derivation dataset) by running a Cox regression on the covariates x in the validation dataset, 'offsetting' the original PI evaluated in the validation dataset. [...] From the point of view of successful validation, the 'best' result is that all the coefficients $β$ are 0."
How do I do this? One approach I tried gave me an error:
cph(S ~ x1 + x2 + x3 + PI, data = df)
# Gives the error: X matrix deemed to be singular; variable PI

Any guidance on this is highly appreciated.
Update:
Following the suggestion from EdM below, I refitted the model with PI as offset term:
a <- cph(S ~ x1 + x2 + x3 + offset(IP), data = df)

print(a)
## Output:
Cox Proportional Hazards Model

 cph(formula = S ~ x1 + x2 + x3 + offset(IP), data = df)

                         Model Tests    Discrimination
                                               Indexes
 Obs        950    LR chi2      4.44    R2       0.023
 Events      17    d.f.            4    Dxy      0.390
 Center -0.6589    Pr(> chi2) 0.3494    g        0.940
                   Score chi2   6.89    gr       2.560
                   Pr(> chi2) 0.1417

                 Coef    S.E.   Wald Z Pr(>|Z|)
 x1              -0.0256 0.0239 -1.07  0.2839
 x2               1.4074 0.7718  1.82  0.0682
 x3=level1        0.1526 0.7678  0.20  0.8424
 x3=level2       -0.1006 0.5923 -0.17  0.8651

In this hypothetical example I see that x2 is rather far away from 0, meaning that there might be some problem there, is that correct?
Update 2:
Thanks to EdM's useful explanation, I think I have understand how to proceed. As indicated by EdM and Royston and Altman, they recommend performing a joint test for the coefficients first, which would correspond to the TOTAL row in the anova.rms function output:
anova(a)

# Output:
                Wald Statistics          Response: S

 Factor     Chi-Square d.f. P
 x1         1.15       1    0.2839
 x2         3.33       1    0.0682
 x3         0.15       2    0.9269
 TOTAL      5.77       4    0.2169

As EdM pointed out, the sample dataset I chose was probably not great since it contains only very few events, and therefore statistical power is very low.
Nevertheless, I hope this post and its answer will be useful for others too.
Comment: After looking at the Royston and Altman reference, I see that they intend the `PI` in your last model to be an offset, with coefficient  constrained to be exactly 1. That's done with a term `offset(PI)` instead of just `PI` in your last model. Please try that and edit your question accordingly.
Comment: Your last line of code leads to the "singular" X matrix because the `PI` value is a linear combination of the 3 other included predictors. Try running that code without the `PI` value to see what your estimates of the coefficients might be, independent of what you found in the literature. With only 17 events your estimates will tend to be imprecise; the number of events, not the total number of cases, matters for precision of coefficient estimates.
Comment: Thanks, I was assuming that this was the reason for the error too. Regarding the number of events: I used only parts of my dataset to illustrate my problem, the full dataset fortunately has many more outcomes. I edited the question accordingly.
Comment: Sorry I didn't see your comment above before posting my response. I updated the question. Maybe you can post an answer that I can accept? You already helped me a lot.
Answer: What you've done is correct, although there are some cautions to consider.
The first regression of your survival data against the corresponding linear predictor values, calculated from the coefficients reported in the literature and your covariate values, evaluates 2 things in particular.
One is the ability of the model to discriminate the order of events among individuals in your cohort. The C-index, as you calculate it from the Dxy value, is 0.69. That means that the order is correct for 69% of pairs of individuals for which the comparison can be made. As Royston and Altman caution, however, the c-index "is known to be be biased away from the null due to right-censoring of times to event," and you do have a very large fraction of censored cases. The very low $R^2$ also raises red flags about discrimination.
The second is the potential optimism in the reported model when applied to your data. That's evaluated in terms of the slope coefficient in that first regression. The coefficient is only 0.63, well below the value of 1 that would be expected if the model worked as well on your data as it did in the original report. I would be very worried if I got that low a slope during internal resampling validation of a model; that would suggest a highly overfit model that wouldn't generalize well to new data. In fairness, you probably can't reliably distinguish that slope in your regression example from a value of 1; the small number of events leads to a large standard error.
Your second regression, incorporating that linear predictor as an offset in the model along with your own covariate values, helps identify a potential difference between your data and those in the original literature report. A coefficient substantially different from 0 in that regression indicates a different association between that predictor and outcome from what was seen in the original report. In your case, x2 raises questions as you note. Again, with the small number of events in your example, that coefficient is not "significantly" different from 0. Furthermore, Royston and Altman suggest first performing a joint test that all covariate coefficients are 0, in this model with an offset for the linear predictor, to minimize false-positives from multiple comparisons.
Answer: I have written a blog post with R code about the steps in the paper: Royston P, Altman DG. External validation of a Cox prognostic model: principles and methods. BMC Med Res Methodol. 2013 Mar 6;13:33
Here is the link


Should I use the cross validation score or the test score to evaluate a machine learning model?

Question: Let's say I want to compare two machine learning models (A and B) on a classification problem. I split my data into train (80%) and test set (20%). Then I perform 4-fold cross-validation on the training set (so every time my validation set has 20% of the data).
The average over the folds cross validation accuracy I get is:
model A - 80%
model B - 90%
Finally, I test the models on the test set and get the accuracies:
model A - 90%
model B - 80%
Which model would you choose?
The test result is more representative of the generalization ability of the model because it has never been used during the training process. However the cross-validation result is more representative because it represents the performance of the system on the 80% of the data instead of just the 20% of the training set. Moreover, if I change the split of my sets, the different test accuracies I get have a high variance but the average cross validation accuracy is more stable.
Comment: Try performing a n-try, K-fold cross validation. For each try, you split the dataset into K folds and when you optimize over a hyperparameter you use the same K folds to get your CV error. Finally, you can plot a boxplot of your hyper parameter on the x-axis and error on the y-axis. This will not give you a nice clean value but will provide a range of values, choose your hyper parameter accordingly. This could prevent the overfitting mentioned by @Berkay Bulut
Comment: Thanks for the reply.
I don't really think that model B is over-fitting. My impression is that the test set (being only the 20% of the dataset) is not representative enough. If a take a different split of the data then model B is better in both metrics. But, is it proper to change the split of your data to get a satisfying result?
Comment: The final goal of every ML model is to get good results for new data. Hence, it is not good to pick a particular split that gives a "lucky" (by setting the seed) result. Looking at your comment below, I would say that sometimes we can even get lucky with the K folds if we perform it only once. Therefore if we perform n-try K-fold we can get an idea of the range of error.
Comment: Yes, this could be a good idea. Ideally, we could also have n-try train-test split and then for each train set have n-try k-fold. However this would require training hundreds of models and unfortunately each training needs a couple of days..
Comment: Nice question! I'm really curious if you actually came across this (if so please post the full data and analysis) or a clever [Gedankenexperiment](https://en.wikipedia.org/wiki/Thought_experiment)?
Comment: What do you do in the cross validation? Do you use it to tune hyperparameters?
Comment: great question!
Comment: I actually came across this problem. Unfortunately, I am not allowed to disclose the data. The model was a deep neural network. Cross-validation was performed to tune a few parameters.
Answer: First of all, if the cross validation results are actually not used to decide anything (no parameter tuning, no selection, nothing) then you don't gain anything by the test set you describe:

your splitting in to training/test is subject to the same difficulties as you subsequent splitting of the training set into the surrogate training and cross validation surrogate test sets. Any data leakage (e.g. due to confounders you did not account for) happens to both.
in addition, as you say, the 20 % test yset is smaller. Whether this is a problem or not depends largely on the absolute number of cases you have. If 20 % of your data are sufficient to yield test results with a suitable precision for your application at hand, then you are fine.

That being said, selecting a model is part of the training of the final model. Thus, the selected model needs to undergo independent validation.
In your case, this means: select according to your cross validation, e.g. model B (although you may want to look into more sophisticated selection rules that take instability into account). Then do an independent test of the selected model. That result is your validation (or better: verification) result for the final model. Here: 80 %.
However, you can use an additional outer cross validation for that, avoiding the difficulty of having only few test cases for the final verification.
Answer:
However the cross-validation result is more representative because it represents the performance of the system on the 80% of the data instead of just the 20% of the training set.

This is not the whole picture. Yes, the cross-validation error uses unseen ("out-of-bag") data. However, note that you are using the CV error in fitting your model and tuning (hyper-)parameters. And then the final model you are working with has seen these "unseen" data.
Cross-validation is part of model training. CV errors are not indicative of out-of-sample performance.
This would argue for using model A, which performs better out-of-sample. However...
Note that now you are using your test set in selecting a model. Thus, for your final model, the test set is not unseen any more!
Another thought experiment: assume you are fitting a huge amount of models to your data (maybe some of these models add random noise to your predictions?) and assess all of these models on your test set. Then one model will perform best on the test set. But if you then choose this model as your final model, its good performance on the test set may be due to chance alone. You may have overfit to the test set.
Conclusion: test set performance is only then a guide to true out-of-sample performance if it is not used in selecting, tuning or "improving just a little bit" your final model.

Moreover, if I change the split of my sets, the different test accuracies I get have a high variance but the average cross validation accuracy is more stable.

High variance in test set performance is a red flag. It does seem like you are overfitting. You may simply have too little data for your model, and beyond some point, even cross-validation may not save you. Consider regularizing your model, or constraining it in some other way (e.g., using the one standard error rule).
Also, accuracy is not a good evaluation measure.
Answer: What you are doing is creating test data at 20% of your total data set. However, the main purpose of Cross Validation Testing is to evaluate your models on different random samples loosing minimum information.
It is also important to consider how you cross validate and create your test data, whether you stratify sample the data or straight split. I suggest using stratified sampling in both CV and test for the data to more representative.
The information you present on the accuracy on two different models leads to conclusion that model A can be improved by using more data, it is seems to have underfitted and your model B has overfitted to your train data. These maybe due to the nature of the algorithms you have used, the features in your model, the regularisation you may have used or the sampling/splitting method you have used in the splits.
Comment: "Out-of-bag" refers to samples not included in a particular run of a bootstrap aggregation (bagging) algorithm, not what's happening here. Can you say more about how you came to the conclusions you did in the last paragraph?
Comment: Thanks for the reply.
The CV is done within the 80% of the training set which is split into 4 for the 4 folds each having 20% of the data. My impression is that model A just got too "lucky" with the test set. However, it was not as much lucky when tested on the 4x20%=80% of the dataset


Non L2 loss-function in gradient boosting

Question: As I understand the idea of gradient boosting in the (m+1)-th step we take the partial derivatives of the loss with respect to our new parameters $f^{[m]}(x^{(i)})$: $\tilde{y}^{(i)}=-\frac{\partial (\sum_{i=1}^nL(y^{(i)}, f^{[m]}(x^{(i)})))}{\partial f^{[m]}(x^{(i)})}$
and afterwards approximate this gradient step with a function $f_\theta$ from our hypothesis space that comes close to this gradient step, i.e. $\tilde{f}= \text{argmin}_{f \in H}\sum_{i=1}^n\left[\tilde{y}^{(i)}- f_{\theta}(x^{(i)}) \right] ^2$
we then multiply $\tilde{f}$ by some constant (depending on the learning rate) to get $f^{[m+1]}$
Now my question is about the scenario in which our loss is not $0.5(y-f(x))^2$ but some other arbitrary loss function $L^*$. Then $\tilde{y}^{(i)} \neq (y^{(i)}-f^{[m]}(x^{(i)}))$. According to algorithm 1 in the Friedman paper (page 1193) we would then still minimize $\sum_{i=1}^n\left[\tilde{y}^{(i)}- f_{\theta}(x^{(i)}) \right] ^2$ in order to get our new function.
My question is why we would not simply minimize $\sum_{i=1}^nL(y^{(i)}-f^{[m]}(x^{(i)}), f_\theta(x^{(i)}))$
Is this only because the approach as in algorithm 1 by Friedman is cheaper or is there another reason?
Comment: https://xgboost.readthedocs.io/en/latest/tutorials/model.html In the case of a classification problem, xgboost uses cross-entropy loss as a default. The derivation of how to update weights is a straightforward application of Taylor approximations; you can use the same strategy to optimize arbitrary loss functions.
Answer: First of all, I think your question should be formulated this way:
\begin{equation}
\sum_{i=1}^n L(\tilde{y}^{(i)},f_\theta(x^{(i)}))
\end{equation}
Since the perk of Gradient Boosting Machines is precisely to allow the optimisation (by gradient descent) of any kind of Loss we decide to consider. Writing the formula your way, you're already committing yourself to the gradient of the Squared Loss, instead we want to minimise the gradient of our chosen Loss, i.e. $\tilde{y}^{(i)}$.
Said so, we shall consider now the problem at hand just as an optimisation problem: we just care about finding the best parameters $\hat{\theta}$  to achieve good properties (in an optimisation perspective) for the function $f_\theta(x^{(i)})$, across the $n$ units of the dataset.
The problem resembles very much the regression setting: on a $n$ points dataset, find the line which best approximates the points. Based on what desiderata we want to achieve, there are many options how to find the best parameters. In Statistics the usual solution is given by Ordinary Least Squares (OLS), i.e. minimising the squared difference between each point and its prediction obtained through the regression line.
Notice that the OLS minimisation technique is the same used in Gradient Boosting, on which you ask for clarification.
The reason why OLS is used in Regression is (paraphrasing Greene, W. H. (2003). Econometric analysis. Pearson Education India):

it obtains unbiased estimates;
across the unbiased estimators, it is the one with least variance.

Moving to the Gradient Boosting optimisation setting, the properties stated above may be translated into:

When two points in the dataset have exactly the same values of the $X$ variables, but different $y$ values, gradient boosting should return the mean of the two $y$ as prediction for those $x$ values;
Given the first property, we are guaranteed the prediction function is the one with minimum variance. Formally, we shall regard the variance as a dispersion measure on the overall dataset. Giving an intuitive interpretation: minimum variance is achieved when the prediction is the least distant from all the points in the dataset (This is neither formally rigorous nor accurate, it is just to give an intuition).
Comment: Regarding point 1 about gradient boosting: the squared loss is only used when fitting a tree to the negative gradient. This step is used only to find the final regions, *not* the leaf values. The resulting 'intermediate' leaf values are discarded. The final leaf values are found in the next step of the algorithm (line search) where the prediction is not necessarily the mean (e.g. it is the median for absolute loss).


Show that $z^6 + 5z^4 - z^3 + 3z$ has at least two real roots given that all roots are distinct.

Question:
Show that $z^6 + 5z^4 - z^3 + 3z$ has at least two real roots given
  that all roots are distinct. Also, show that $|3z - z^3 + 5z^4| < |z^6|$ when $|z| > 3$.

I can see that 0 is a real root; however, I am having trouble starting this one. I couldn't seem to see a way to factorize this. Other than testing points in the derivative and maybe using the intermediate value theorem, I don't know what direction to take on this one.
Answer: Since the coefficients are real, all roots have conjugate roots as well. This means that excluding the $z=0$ root, you have five other distinct roots, so at least one of them is real. So we have at least two real roots.
Answer: Let $P(z) = z^6+5z^4-z^3+3z$, then $P(-1/2) = -67/64$, but the highest power of $P$ is even with positive coefficient, so $\lim_{x\to+\infty}P(x) = +\infty$ and $\lim_{x\to-\infty}P(x) = +\infty$, so by the intermediate value property of continuous functions $P$ possess at least two real roots.
Edit: And for the second part, looks like triangle inequality suffices.
Comment: An elegant answer.
Comment: Actually I prefer yours ^^ it seems that it is what was intended in the question (to learn such technique, etc.). Still, sometimes it is more useful to use more basic tools, we shouldn't forget about them ;-)
Comment: This, or $P(0)=0$ and $P'(0)\ne0$.
Answer: You have the root $z=0$, so any other root would also be a root of $z^5 + 5z^3 - z^2 + 3$.  Call that expression $g(z)$.
$g(0)=+3$ while $g(-3)= -384$ so by continuity and the intermediate value theorem there is a real root of $g(z)$ in $(-3,0)$, and this will be a real root of your original expression.
Meanwhile for $|z| \gt 3$ you have $$|3z - z^3 + 5z^4| \le  3|z| +|z^3| +5|z^4| \lt \left(\frac{3}{3^5}+\frac{1}{3^3}+\frac{5}{3^2}\right)|z^6| \lt |z^6|.$$
Comment: "Any other real root is also a root of $g$" is the wrong direction (it's true but not useful because we don't yet know that there _is_ another real root). What you need is "Any real root of $g$ is also a real root of the original polynomial""
Comment: @Henning: I think that is pedantry: we "know" there is another real root because we trust the question to be honest.  But I have changed the wording slightly to make it more hypothetical.
Comment: The matter is not whether we trust the exercise, but that the reasoning you describe is no the reasoning the exercise asks us to do. It _doesn't matter_ which properties "any other root" would have, because once we have a root to apply that knowledge to, we're already done. What _does_ matter is the opposite implication, that roots _of $g$_ are also roots of the original polynomial.
Comment: To quote Bill Clinton "It depends upon what the meaning of the word 'is' is."
Comment: @Henning Note that he does state that the converse holds in the second paragraph. It would be clearer to write: if $\rm\:r\ne 0\:$ then $\rm\:f(r) = r\:g(r) = 0\iff g(r)=0.\quad$
Answer: $P(-\frac{1}{2})=- \frac{67}{64}$ and $P(-1)=4$.  Therefore the other real root is in the interval $(-1,-\frac{1}{2})$. In any case, when you factor out zero you get a polynomial of fifth degree. A polynomial of odd degree has at least one real root.  That root in our case is not zero therefore it has at least two distinct real roots.
Answer: The derivative at zero is non-zero, so it must be strictly negative at some point $a$ close to $0$. Also, the polynomial tends to $+\infty$ in both directions. So it must have another root, with the same sign as $a$.
Answer: For the first part of the question:
We do not need to know that the polynomial has distinct roots. $0$ is an obvious simple root, dividing by $z$ gives a polynomial of degree 5, which must have a (clearly different) real root by the intermediate value theorem -it tends to both $\pm\infty$.
nbubis' proof is very nice but uses algebraic closure of $\mathbb C$ -the fundamental theorem of algebra. The intermediate value theorem is simpler to prove.
EDIT: Well I should have read Henry's answer, mine overlaps alot with his. The main thing I add perhaps is that we do not need the distinct roots assumption.


Conditional class on  element a la boilerplate

Question: This might be a totally mundane question, but I hope not.
I want to add conditional classes onto the  element of my theme like boilerplate. I'd like to keep things neat and not complicate my page.tpl.php.
<?php
  $htmlvar = 'xmlns="http://www.w3.org/1999/xhtml" xml:lang="' .
    $language->language . '" lang="' .
    $language->language . '" dir="'. $language->dir .'"';
?>

 <html <?php print $htmlvar ?>>

Reactions? Suggestions? Taunts?
Answer: There isn't really a great way to do this: Drupal 7 modified drupal_add_css() to make adding conditional CSS possible outside the template file, but not for arbitrary HTML.  Drupal 6 doesn't have anything.
In Drupal 6, and for arbitrary conditional HTML, you either need to add the markup directly  to page.tpl.php, or you could create a variable in your theme's implementation of template_preprocess_page() that contains the relevant markup as a string:
Drupal 7
function foo_preprocess_html(&$variables) {
  $htmlvar = 'xmlns="http://www.w3.org/1999/xhtml" xml:lang="' .
    $variables['language']->language . '" lang="' .
    $variables['language']->language . '" dir="'. $language->dir .'"';

  $variables['conditional_html'] = '';
  // Concactenate the rest of the conditional HTML here
}

Then add $condtional_html to the appropriate place in html.tpl.php.
Drupal 6
function foo_preprocess_page(&$variables) {
  $htmlvar = 'xmlns="http://www.w3.org/1999/xhtml" xml:lang="' .
    $variables['language']->language . '" lang="' .
    $variables['language']->language . '" dir="'. $language->dir .'"';

  $variables['conditional_html'] = '';
  // Concactenate the rest of the conditional HTML here
}

Then add $condtional_html to the appropriate place in page.tpl.php.

If you're interested in Drupal 7's conditional CSS handling, you can see Bartik's implementation of template_preprocess_html() for an example:
function bartik_preprocess_html(&$variables) {
  // ...

  // Add conditional stylesheets for IE
  drupal_add_css(path_to_theme() . '/css/ie.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'lte IE 7', '!IE' => FALSE), 'preprocess' => FALSE));
  drupal_add_css(path_to_theme() . '/css/ie6.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'IE 6', '!IE' => FALSE), 'preprocess' => FALSE));
}

Comment: I think we might be talking about two different things. My post isn't about loading conditional stylesheets, it's about emulating the technique/hack of adding a simple class onto the html element like boilerplate does. Thanks for the tip on d7's _preprocess_html, I'm not able to use d7 yet but that looks like it would come in handy and is more along the lines of what I'm trying to do.
Comment: @ha5bro As I said in my answer, Drupal doesn't have anything in its API to specifically support non-CSS (arbitrary) conditional comments: either you write it manually as you've been doing or you add a string to templates via preprocess hooks. I mention `drupal_add_css()` to reference the places where Drupal *does* support conditional comments in its API.
Answer: Drupal 7's Zentropy base theme does this, here is a live demo: http://d7.webdrop.net.br/
That said, and since I'm the theme's lone maintainer, I wouldn't use it as all development efforts have gone towards a Drupal 6 backport, available on here on github.
And also, this functionality was eventually removed from the D6 backport.
Answer: Browser class module is the way to go here:
http://drupal.org/project/browserclass
Example :

...

Supported browsers
ie, opera, safari, chrome, netscape, ff, konqueror, dillo, chimera, beonex, aweb, amaya, icab, lynx, galeon, operamini
Browser version checking
The module creates version classes.
ie[version], opera[version], chrome[version], safari[version], netscape[version], ff[version], konqueror[version], operamini[version]
Supported platforms
win, ipad, ipod, iphone, mac, android, linux, nokia, blackberry, NetBSD, FreeBSD, OpenBSD
Mobile detecting
The module checks if the device is mobile and adds "mobile" or "desktop" class.


Can we ever find a linear transformation T : R 3 → R 4 which is onto?

Question: I was trying to solve the statement by rank nullity theorem but got stuck.
Comment: Where did you get stuck? Please show us your progress, so we can help.
Comment: Note that a linear map $T$ from vectorspaces $U$ and $V$ will necessarily have $\text{rank}(T)\leq (\text{dim}(U),\text{dim}(V))$.  Do you see why this is true or how you can prove it?  Now, for a linear transformation $T$ from $U$ to $V$ to be "onto", what does that say about the rank of $T$ compared to the dimension of $V$?
Comment: dim(range(T))+null(T)=dim(v) and dim(v)=3 , dim(range(T))=4 and null(T)>=0,so i think i have got my answer .Thank you.
Answer: HINT
If $T:\mathbb{R}^3 \to \mathbb{R}^4$ is a linear transformation, there is a $4 \times 3$ matrix $A$ that characterizes $T$. Indeed, from the Rank-Nullity Theorem, we know that
$$
rank(A) + nullity(A) = \dim \left( \mathbb{R}^3  \right)= 3.
$$
Can you explain what $rank(A)$ is, and if $T$ was onto, what should the value of $rank(A)$ be?
Comment: rank(A)=dim(range(T)) which should be 4.
Comment: thank you,I got my answer Actually i was using rank nullity theorem used in matrices not in linear transformation .(kernel and range )
Comment: @AYUSHAZAD indeed from your comment above sounds like you got it
Answer: No.  Linear transformations don't increase dimension.  You can use the rank-nullity theorem to see it.  In this case, the rank is at most three.  But the rank is the dimension of the image.
Answer: Another way: A linear trnasformation is defined when the images of a basis is known. It follows that the image can be generate only by three vectors (in $\mathbb R^4$ in this case) so the space image can be at most of dimension three.


How to convert object of any type values, to object of string values (Typescript)?

Question: I have an object.
let obj1: A;
/*
type A = {
  property1: any;
  property2: any;
}
*/

I know that the values in the object are all strings, but I don't want to forcefully typecast.
// I don't want to do this
const obj2 = obj1 as Record<keyof typeof obj1, string>

Instead, I want to infer it in the right way, using typescript predicates. This is my attempt to do it.
function getIsCorrectType<T extends Record<string, any>>(
  obj: T
): obj is Record<keyof T, string>{
  return true; // assume that I manually checked each value to be a string
}

However I now get an error
A type predicate's type must be assignable to its parameter's type.
  Type 'Record<keyof T, string>' is not assignable to type 'T'.
    'Record<keyof T, string>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Record<string, any>'.
      Type 'string' is not assignable to type 'T[P]'.

To me this sounds crazy. I should be able to assign string to T[P] = any, right? What am I doing wrong? Is there any alternative solution to this?
Comment: Consider [this](https://tsplay.dev/WYRVbw)
Comment: The `key` of the generic `T extends Record` is not enforced and could be a number so, this is technically working: `getIsCorrectType({1: 1337})`. Add a generic K like this to enforce the type of the key: `function getIsCorrectType>`
Comment: @r3dst0rm Interesting observation. I [tried out your suggestion](https://www.typescriptlang.org/play?#code/MYewdgzgLgBCBGArAjDAvDA3gKBjADgE4j4CCAXDAEwwCGEdYAngDS4HH4BClN9jrbAF9s2BCmwB6STAB6AflEAzAK5hgUAJbgYAcwCmUAJIQAwiEKF9GgCpN8+gDwBpGPoAeUfWAAmDaISaYLosMDZunt5+MABK1hY+LqG0zAB8qQAU7OKUNtgAlJTiMJoMcaCEic6hAUG6qTh4VlAqhGAwUIQq+gDcMNJ0EBAqALb6HQAWtLBGMCMpKrQANktMMMAT1gDW+j5utBswAG7L3R0gMPDjtDC1wcKioJCw7uh6hibmltZQdg4ZmGQ5AA5BAfEpgUJ8tggA), but for some reason TS still doesn't care during function invocation. Moreover, the original problem with type predicate isn't solved either. Any ideas?
Comment: @captain-yossarianfromUkraine Beautiful thinking outside the box there! This not only **solves my problem**, but also helped me understand that the inner working of typescript predicate is via an intersection operator, that merely narrows the type. Please consider adding this as an answer.
Answer: In order to make it work, you need to infer just a set of keys instead of whole object:
const isString = (value: unknown): value is string => typeof value === 'string'

const getIsCorrectType = <K extends string>(
  obj: Record<K, unknown>
): obj is Record<K, string> =>
  Object.values(obj).every(isString)

const x = getIsCorrectType({ 1: 'sdf' })

K - is used for keys inference
Also I have added isString custom typeguard


Commerce payment for Rooms reserveration only after the order is confirmed

Question: I want to use Commerce + Rooms and wonder if there's a configuration/module for the following procedure:

User reserves a room, checks out, but isn't allowed to pay yet.
The room owner gets an email and confirms/declines the order.
If confirmed, the user gets an email and can now start the payment process in the checkout.

Rooms offers an "enquiry mode" (instead of the mode for booking directly), but as far as I understand it, this only sends an email (no order is created), so this enquiry never reaches Commerce.
Comment: Not familiar with the Rooms module, does it integrate with Rules?
Answer: Maybe you want to look at the Easy Booking distribution, to check if there is anything in it similar to what you're looking for (specific to the way this distribution uses Rooms and Drupal Commerce). If you find something, it is just a matter of reusing what's appropriate in your own site.
Here are some more details about this distribution (quotes from its project page):

... to set up a website, that will suit all your needs in managing your own hotel, inn or hostel online.
It gives a set of options for visitors to make room reservations, contact hotel administration or just follow hotel's newsletter to be aware of all special offers and discounts.
Easy booking profile is powered by Drupal Rooms and Drupal Commerce - widely used and popular contributed decisions, which means active maintaining and timely technical support.


Returning value from WLST script to Ant Script

Question: <?xml version="1.0" encoding="iso-8859-1"?>
<project name="monitorResources" default="help" basedir=".">
<taskdef name = "wlst" classname="weblogic.ant.taskdefs.management.WLSTTask">
  <classpath>
    <pathelement location="${oracle.home}/wlserver_10.3/jdeveloper/ant/lib/weblogic.jar"/>
  </classpath>
</taskdef>

<property file="Test_Build.properties"/>
<property name="ant-contrib.jar" location="${oracle.home}/ant/lib/ant-contrib-1.0b3.jar"/>
<property name="weblogic.jar" location="${oracle.home}/wlserver_10.3/server/lib/weblogic.jar"/>

<target name="fetchConnections">
  <wlst executescriptbeforefile="true" debug="false">

  </wlst>
</target>
<target name = "checkConnections">
  <echo>###</echo>
</target>
</project>

The above script is used to get values of connections that have exceeded their current capacity. The WLST script has been embedded in an Ant script.
How can I get the value of the "excessConnectionsList" WLST variable in the "checkConnections" target?
Answer: The only way I managed to do this after some research is to use a temporal properties file:
<wlst debug="false" failOnError="true" executeScriptBeforeFile="false" fileName="${script}" >

</wlst>

<loadproperties srcFile="wlstOutput.properties"/>

<delete file="wlstOutput.properties"/>

<echo>WLST RESULT: ${propertyKey1}</echo>


What is the relationship between orthogonality and the expectation of the product of RVs

Question: Is there such thing as a statistical concept of orthogonality? Does somebody could provide a formal explanation about the relationship between orthogonality and conditional expectation of a RV? Here is the motivation for the question. In Greene (2011) Econometric Analysis, pg. 93, he writes
(1) "Assumption A3 states that the disturbances in the population are stochastically orthogonal to the independent variables in the model; that is, $E[\epsilon|\vec{x}]=0$"
A3: $E[\epsilon|\vec{x}]=E[\epsilon|x_1,x_2,...,x_n]=0$
Why $E[\epsilon|\vec{x}]=0$ is the same thing as to say that $e_i$ is stochastically orthogonal of each $x_i$, the independent variables in the model. How to random variables can be orthogonal?
Definition of orthogonality: it is defined in Linear Algebra and it requires at least two vectors. One possible way to say two vectors are orthogonal is that their dot product is zero, that is, if $x=(x_1,...,x_n)$ and $y=(y_1,...,y_n)$ then
$x\cdot y = 0$
Definition of conditional expectation:
$E[\epsilon|\vec{x}] = \int_\epsilon \epsilon f(\epsilon|\vec{x})d\epsilon$
How the two concepts are formally related?
Comment: All you have to do is enlarge your [concept of vector](http://en.wikipedia.org/wiki/Vector_space#Definition): from that (standard) definition it is immediate that random variables (defined on a common sample space) form a real vector space and that the expectation of a product is the perfect analog of the "dot product" in the finite-dimensional case. I would be curious to know what definition you would propose for orthogonality among *sample spaces*!
Comment: This might be one of the few inaccuracies in Greene "Econometrics". It is a little inaccurate to call $E(e|x)=0$ the orthogonality condition, for it has its own name - *exogeneity condition*.  Bit stronger than the *real* orthogonality condition, $E(xe)=0$.
Answer: Notice that orthogonality is concept that derives its self intuitions about vectors in $R^n$ were we say if dot product between two vectors is 0 then they are orthogonal. Well,in linear algebra this idea is generalized function and the dot prodcut is replaced by a general called an inner product, $\langle x,y\rangle$, (which is basically a function, letting $V$ be a vector space, $ V^2\rightarrow \mathbb{R}$ that follows certain criteria). And here we say vectors are orthogonal if their inner product of the vectors is 0.
Thus all you need to have idea of orthogonality with RVs as your vectors, is to define an inner product. Well, the usual inner product used and one that fulfills the criteria for an inner product is
$$\langle X,Y \rangle= E(XY)$$
Thus RVs, X and Y are orthogonal if $E(XY)=0$
Comment: There is a lot more detail to this but hopefully this gives you enough intro that you can do some of your own research
Answer: I think I got the answer for this question. Although orthogonality is a concept from Linear Algebra, and it means that the dot-product is zero, the term is loosely used in statistics. Whenever we have two Random Vectors $(X,\epsilon)$ with $E[X]=E[\epsilon]=0$, the correlation of X and Y is the expectation of their dot-product. If, also, $E[\epsilon|X]=0$, then it must be the case that $X\cdot\epsilon=0$. Formally, Assume $E[\epsilon|X]=0$ (the A3 of the question) and $E[\epsilon]=0$ (this last assumption is less important b/c we can always centralize a RV around its mean to make its expectation be zero). Then
$$Cov(X,\epsilon|X) = E[X\cdot\epsilon|X]=X\cdot E[\epsilon|X] = 0\implies X\cdot\epsilon=0$$
If $X\cdot\epsilon\neq 0$, lets say $X\cdot\epsilon = c$ then we would have $E[X\cdot\epsilon|X] = c\neq 0$. In other words, as $E[X\cdot\epsilon|X] =0\implies X\cdot\epsilon=0$, it is called orthogonality condition.


Trying to make view template

Question: I have a content-type that has an imagefile and a url; I want the url to wrap around the image. So I'm trying to make a viewtemplate based on the information in Theming information
based on this description
/**
  * This template is used to print a single field in a view. It is not
  * actually used in default Views, as this is registered as a theme
  * function which has better performance. For single overrides, the
  * template is perfectly okay.
  *
  * Variables available:
  * - $view: The view object
  * - $field: The field handler object that can process the input
  * - $row: The raw SQL result that can be used
  * - $output: The processed output that will normally be used.
  *
  * When fetching output from the $row, this construct should be used:
  * $data = $row->{$field->field_alias}
  *
  * The above will guarantee that you'll always get the correct data,
  * regardless of any changes in the aliasing that might happen if
  * the view is modified.
  */

I made views-view-field--my-view--block--field-image.tpl.php to look like this:
$url = $row->{$field->URL}
?>
<a href="<?php echo $url; ?>" target="_blank">
  <?php print $output; ?>

But I'm getting Cannot access empty property on the $row->{$field->URL} line. Can I only access the image field from within the template for the image field? I was looking at views-view-fields--my-view--block.tpl.php but not sure how to access things from in there
Comment: When dealing with a filed template I have never been able to retrieve data for any other field besides the one for which I am creating the template. You might want to consider creating a template for the row, or the style of the view. Then you can have access to all fields available to $row. Nevertheless I wanted to say that you can create this functionality without a template. Add a global text field to your view and use the link and image tokens to create the link.
Comment: @Perisdr I thought it should be possible!... could you explain more about how to do that? not sure what links and image tokens are
Comment: I think @Perisdr is correct that it would be easier to do this at the row level vs field, or todo a rewrite also a great idea... :)
Comment: @Damon when you select text rewrite option you will see suggested tokens below it.
Comment: i dont' see a text-rewrite option.. do I need the tokens module for what you're talking about to be an option?
Comment: i also couldn't figure out what elements to use from the row level because I can't print_r the `$fields` variable
Comment: sorry its called `Replacement patterns`
Comment: Where is the replacement patterns - editing a field in a view? Didn't notice anything like that...
Answer: I would do the following:

<?php print_r ($row); ?>

This will give you the full tree of the $row
Comment: Good thinking. I isolated it down to: `$row->_field_data[nid][entity]->field_url[und][0][safe_value]);` Is that safe to use as the value of the field?
Comment: try $row['field_url']['und']['0']['safe_value'];
Comment: sorry, missed your edit... thats why its called safe_value :)
Comment: heh yeah.. I was just concerned that the string might not always be the same, though I don't see why it wouldn't
Answer: Instead of doing this in a template file you could do this by using Rewrite Results within the field settings of a view, either by creating your own Global custom text field or editing the settings of the link field or image field. In the rewrite results section you can create a link around the Image as long as the image and the link are both fields within the view.

Trying to make view template


How to create interfaces for CSR 1000v for GRE tunnels?

Question: Well, my idea is to connect to 4 CSR 1000v routers in different VPC (without VPC peering).
I've basically finished with everything with the pre-configuration creating VPCs, subnets IGW, modifying Route Tables ... etc. Then, using IPsec over GRE tunnels, the 4 instances can see each other. (Thanks to @MLu for suggesting this idea).
After having 4 instances and each of them their Primary Network Interfaces is associated with public IP addresses (184.108.40.206 is for CSR1 and 184.108.40.206 is for CSR2). I saw this video where I almost have the same configuration YouTube Video link.  I started to configure the tunnel between 2 CSRs 1000v routers, where I created an interface inside the router called Tunnel 0 with its source interface  which is here GigabitEthernet 1 so my configuration is the following:
CSR1(config)#interface tunnel 0
CSR1(config-if)#tunnel source GigabitEthernet 1
CSR1(config-if)#tunnel destination 184.108.40.206

CSR2(config)#interface tunnel 0
CSR2(config-if)#tunnel source GigabitEthernet 1
CSR2(config-if)#tunnel destination 184.108.40.206

Then I configured the IP addresses of these both tunnels' interfaces as it shows in the figure below.
CSR1(config)#int tun 0
CSR1(config-if)#ip address 10.10.1.2 255.255.255.252

CSR2(config)#int tun 0
CSR2(config-if)#ip address 10.10.1.1 255.255.255.252

My problem now is that the status of these tunnels' interfaces are down as it shows show ip int br:
Interface              IP-Address      OK? Method Status           Protocol
GigabitEthernet1       184.108.40.206      YES DHCP   up                  up
Tunnel0                10.10.1.1       YES manual up                 down
VirtualPortGroup0      192.168.35.101  YES NVRAM  up                  up

I realized that I have to create a subnet for the tunnel which I did at the end 10.10.1.0/28 then I created other interfaces in all instances but I was not allowed to associate it with a public IP address because The maximum number of addresses has been reached. in order to change GRE tunnel configuration.
My idea was to attach those new interfaces with a generated public IP address then I will get a new interface which is GigabitEthernet 2 obviously, and I continue the configuration using this new interface GigabitEthernet 2 instead of Tunnel 0, but I'm not allowed to create more Elastic addresses. I'm not sure if I am on the right track.
Comment: With this Cisco-specific question you may be better served at our [Network Engineering site](https://networkengineering.stackexchange.com/).
Comment: You should not need multiple external interfaces on a single router in order to create GRE tunnels to multiple remote routers.  Multiple tunnels can have the same source interface.
Comment: @Michael-sqlbot for the source is not a problem to create external interfaces. I'm getting confused about why I cannot see the status of `tunnel0` interface in the router is `down`. Shall it be `GigabitEthernet ` ?
Answer: Either VPC peering or Transit Gateway must be set between both VPCs in order to run  GRE tunnel.


How to optimize Dojo loading time?

Question: I'm working on a business application built upon PHP & Dojo tool kit. The interface is similar that you see on dojo dijit theme tester.
On internet it takes lot of time to load all those js one by one..
I want to know what is the best technique that is being used by theme tester demo that it loads much faster than one we built.?
I'm interested to know the best practices on optimizing its loading time?
Comment: http://stackoverflow.com/questions/9640757/dojo-huge-footprint-am-i-doing-something-wrong/9641570#9641570
Answer: You have rightly observed the biggest cause of runtime performance issue is the many many roundtrips it is doing to the server to fetch the small JS files.
While the modularized design of Dojo is very beneficial at design time (widget extensions, namespacing etc), at runtime, it is expected you optimize the dojo bits - the way to do that is to do a custom build.
Doing a  custom build will give you a big performance boost - the hundreds of roundtrips will be reduced to one or 2 and the size of the payload will also dramatically decrease. We have seen a 50x performance improvement with custom build
Custom build will create an optimized, minified JS file that will contain only the code you use in the app.
You can define multiple layers depending on how you want to segregate your application JS files (for example, one single compressed file versus multiple files included in different UIs)
depending on the version of dojo you are using, see:
http://dojotoolkit.org/reference-guide/1.7/build/index.html#build-index
http://dojotoolkit.org/reference-guide/1.7/build/pre17/build.html#build-pre17-build
While it looks daunting at first, sitck with it and you will be able to create an optimized version and see the benefits :)
Comment: Thanks Vijay, this helped me to bring down the time. Thank you.

How to optimize Dojo loading time?


What is the difference between these functions in Javascript

Question: I would like to know which is the difference between these expressions:
case a:
const functionA = () => {
   executeA(), executeB()
}

case b:
const functionA = () => {
  executeA();
  executeB();
}

Comment: Personal opinion, but I'd say that the first one is just trying to be clever for no reason.  The comma operator has a purpose, and its purpose isn't used there.  Better to just invoke each function as its own statement for clarity.  (If there's a linter configuration to warn on expressions with ignored results, I'd recommend enabling it.)
Comment: The first version is a way less readable way to do the same thing used by people who like to write "optimized" code.
Answer: They do exactly the same thing, but the first one is weird and the second one is the normal way of writing code.
Don't use the first version.
Answer: The first one is an expression that uses comma operator
The other one is two statements.
There is no difference between these two in this situation.


Pattern recognition in state sequences

Question: I have a sequence of states of a system. Each state is defined by an abstract identifier e.g "Eating", "Sleeping" etc... and a duration. So a state is basically <code>{id,duration}</code>.
Additionally I have recordings of interesting "event" states that are defined by specific sequences of states; e.g "Eating for 20 minutes followed by driving for 10 minutes =  Event  state "Eating dinner out".  (I know the sample makes no real sense, its just for general illustration. Note that events can have an arbitrary number of states.)
Now I would like to analyze the input state sequences to find those event states in them (in a fuzzy way) Note that there will be very few events in relation to the input data sequences.
I have experimented with basic neural networks, hidden Markov models etc... but nothing seems to give me good results.
Does anybody have some pointers on what kind of algorithms to use on this kind of problem?
Comment: Do you know the "interesting states" that you are looking for? Is this essentially a clustering exercise?
Comment: Yes those are predefined, and also for each of those predefined interesting states i have several recordings of patterns that denote them.
Comment: Eg for "eating out" i have recordings like this:
Recording 1: {"Eating",10} {"Driving: 5"}
Recording 2: {"Eating",30} {"Driving: 20"}
Recording 3: {"Eating",30} {"Walking": 10} {"Driving":5}
Comment: Are the sequences of interesting states embedded in a larger sequence, i.e., do you have sequences like {Sleeping:20},{Showering:5},{Driving:5},{Eating:10},{Driving:10},{Working:100},{Driving:15} and you want to identify the subsequence {Driving:5},{Eating:10} as eating out?
Comment: Your states seem ill defined.  Duration does not seem to be a state attribute, but rather a measurement of time between state transitions.
Answer: Given the sequence-like format, the Hidden Markov Model seems appealing.However, since the states are predefined you can't use it directly on data. Not sure how you've used the HMM but you can train an Hidden Markov Model for each class , like you do for speech recognition.
Also , I guess you could try other supervised learning classification algorithms such as support vector machines(they usually work very well) ,k-nn , logistic regression(maybe?) or naive bayes( I know it is used for spam finding issues).

Pattern recognition in state sequences


Is this a steal or a block and rebound?

Question: This was recorded as a steal. I'm wondering how this can be justified. It looks like McDaniels tries to tip it in. If that's the case, then how can it be ruled a steal? Here is the clip.
Comment: While McDaniels certainly touched the ball, it's not clear that he actually caused it to move *up* towards the basket, which I think is required for this to be considered a shot.
Comment: He clearly causes it to move towards the basket. The ball was arcing downwards and then moves upwards after he makes contact.
Answer: You'd have to ask the scorekeeper to be sure; however, I suspect the direct answer is that the ball never cleared the rim after the touch, so they decided he did not successfully get a shot off.  He probably intended to try and shoot it, but when the positioning and timing didn't work out, it became a bad pass instead of a shot that was rebounded.
Comment: I don't see how that could be interpreted as a pass rather than a shot. Who is he passing to?
Comment: The pass was in to McDaniels (which is *clearly* a pass, that's something they practice extensively).
Comment: To be clear, the pass wasn't really all that bad - the timing/power of McDaniels' jump was the issue.  However, the turnover tends to be credited to the passer in these circumstances even so (imagine the extreme example of a lob from the opposite baseline to half court where the other player is supposed to run to a spot, and instead runs towards the baseline, so nobody is home - that's the fault of the runner, not the thrower, but it's considered a bad pass anyway.)
Comment: I thought you were implying that McDaniels was attempting to pass rather than shoot.

Why does the ball have to clear the rim for it to be a shot attempt? The player just has to be attempting to score, right?
Comment: It's all judgement, but that's a reasonable way to see it - he clearly didn't actually "shoot" per se (there's no proper shooting motion), so the difference between a failed tip in and a failed reception of a pass is a matter of judgement.  Here, I suggest that since the ball didn't clear the rim (nor particularly go towards the basket), he didn't actually shoot - just hit the ball.
Comment: I can see why one would think it's a judgement call but I think that given that he moves the ball towards the basket, it's clearly an attempt to score the basket. The fact that it didn't clear the rim should be irrelevant. But even so, the ball definitely moves towards the basket from McDaniels contact when you watch frame by frame.
Comment: All I can say is the official scorer said otherwise, and I'd say they're a better judge of this than you or me.
Comment: Well they probably made the decision in real-time, not having the benefit of replay.

Is this a steal or a block and rebound?