Question on the philosophy and functioning of hypothesis testing with parametric bootstrapping

Question: In the book of Bradley Efron and Robert Tibshirani (1993) "An Introduction to the Bootstrap" chapter 16, a bootstrap method for hypothesis testing is presented. It is precised that the two quantities that have to be specified when carrying out a bootstrap hypothesis test are :

A test statistic $t(X)$,
A null distribution $\hat{F}_0$ for the data under the null hypothesis $H_0$.

$X$ represents the observed data and an estimate of the p-value is given by  $\hat{p} = \# \{t(X^b) \geq t(X) \}/B$, where $X^b$ is the $b$-bootstrap resampling and $B$ is the number of bootstraps, i.e. $b \in \{1,2,\cdots,B\}$.
In some cases, $t(X)$ can be used as a parameter to define $\hat{F}_0$. Let us then write the 'bootstrap' distribution of $X$ under the null hypothesis $\hat{F}_0(\cdot, t(X))$. So each $t(X^b)$ is obtained from a sample $X^b$ drawn from $\hat{F}_0(\cdot, t(X))$.
Can someone explain why $\hat{F}_0(\cdot, t(X))$ is a good choice for the distribution under the null hypothesis? For me it looks like we would never be able to reject the null hypothesis because the probability to observe $t(X^b)$ close to $t(X)$ under $\hat{F}_0(\cdot, t(X))$ is really high. Is there something I am missing?
Answer: I don't find $\hat F_0(\bullet,t(X))$ suggested as null hypothesis in Chapter 16 of Efron and Tibshirani (1993). In general you need to know in advance what your null hypothesis of interest is, and then the bootstrap test null hypothesis needs to be chosen so that it is in line with this, as explained in the book. Indeed it doesn't make much sense to test the null hypothesis that a parameter is equal to $t(X)$ where $t(X)$ is the value of the statistic observed from your data. But then I don't think this is recommended anywhere.
What people in fact do, and this is mentioned in the book, is that they "translate* the empirical distribution so that it has the null hypothesis value of interest, i.e., if you want to test $\mu=0$ where $\mu$ is the mean of the underlying distribution, and say your $t(X)=5$ (observed mean on your data), then $\hat F_0$ would be the empirical distribution of your observations minus 5 (or if you run a parametric bootstrap, $F_0(\bullet,0)$, the distribution of your assumed parametric model at mean 0, potentially with estimated variance or other parameters).
Comment: @lulufofo OK, I see what you mean. The thing is that this doesn't work quite in the way you are proposing, but I admit it looks like that. The thing is that $\hat h$ is not a parameter that defines a parametric distribution here. $\hat h$ *modifies* the observed empirical distribution by smoothing it so much that it becomes unimodal, so what you call $F(\bullet,\hat h)$ is a unimodal distribution. But if the observed empirical distribution is in fact clearly not unimodal, observations drawn from $F(\bullet,\hat h)$ will look quite different from the data, and ... (to be continued)
Comment: ...and if you "estimate" $\hat h^*$ from the bootstrapped data, it may look very different from $\hat h$ and the null hypothesis will be rejected. Note that $\hat h^*$ here does *not* estimate the same "parameter" as $\hat h$, because $F(\bullet,h)$ does not only depend on $h$ but also on the data set from which it was computed! This means that the new $F(\bullet,\hat h^*)$ may look very similar to the old $F(\bullet,\hat h)$ even if $\hat h$ and $\hat h^*$ are quite different (and the Fs may be different, if hs are similar).
Comment: If the underlying true distribution is in fact $F(\bullet,\hat h)$ (unimodal!), we would expect $\hat h$ and $\hat h^*$ to be similar, and the bootstrap test wouldn't normally reject. So this is a special construction for this specific problem, having in mind the null hypothesis of interest, unimodality. There is no general principle that says that $F(\bullet,\hat h)$ is a good null hypothesis.
Comment: Yes, I agree this is mentionned in the book for the equality of means test. A bit further in the chapter, they test whether the distribution of a variable is unimodal or multimodal. To do so, they use gaussian kernel with $h$ the width parameter. They obtain $\hat{h}$ from the observations $X$ the minimum h such that the distribution is unimodal. Then, the null hypothesis distribution is chosen to be $\hat{F(\cdot,\hat{h}})$ and for $H_0: h \geq \hat{h}$. So, I understand that they did what I explained above. Am I missing something?


$f+g$ is continuous and $f$ and $g$ are everywhere discontinuous

Question: I know that if $f$ and $g$ are both continuous then $f+g$ is continuous. I also know that there are discontinuous functions whose sum is continuous. However, I want to find two functions that are everywhere discontinuous, yet their sum is continuous. I can only come up with examples such as $f(x) = sgn x$,  $g(x) =-sgn x$, but these only have one discontinuity point.
Comment: Take $f$ any everywhere discontinuous and $g=-f$.
Comment: Try Dirchilet type functions.
Answer: You only need to find a function $f$ that's discontinuous everywhere, since then you can take $g=-f$ and clearly $f+g=0$ will be continous.
Such a function can be found by taking $f(x)=1$ if $x$ is a rational number, and $f(x)=0$ otherwise.
Answer: Take
$$
f(x) = \left\{
        \begin{array}{ll}
            1 & \quad x \in \Bbb Q \\
            0 & \quad x \in \Bbb R \setminus \Bbb Q
        \end{array}
    \right.
$$
and
$$
g(x) = \left\{
        \begin{array}{ll}
           0 & \quad x \in \Bbb Q \\
            1 & \quad x \in \Bbb R \setminus \Bbb Q
        \end{array}
    \right.
$$
Answer: To generate infinitely many examples, take $g = h - f$, where $h$ is a continuous function.


Do the matrices representing Markov chains need to be square?

Question: I assume so — I ask in the context of defining an irreducible set. If a set is non-irreducible, you should be able to find a "smaller" Markov chain matrix nested within a larger one. That "smaller" chain matrix has to be square?
Comment: what do you means by a Markov chain to be a square? That the matrix representing the Markov chain is a square matrix?
Comment: Yes -- thanks for pointing that out.
Answer: The short answer is yes, because we account for the possibility of moving from any state to any state (and write a zero if this transition is not possible). This gives us $n \times n$ elements, which is a square matrix.


typecast BOOL always returns false in iOS 8.1.1

Question: I save & retrive the BOOL value in a dictionary using the following methods.
[userData setObject:@YES forKey:@"IS_AGENT"];

(BOOL) [userData objectForKey:@"IS_AGENT"];

In the above code while retrying the bool value from the dictionary I always get false in the following devices iPhone5s, iPhone5c, iPhone6, iPhone6+ which has iOS 8.1.1 and the same code works fine in iPod Touch which has the same iOS 8.1.1
After googled I came to know that we should not type cast BOOL like this because "BOOL is a char, which is eight bits. If you try to squeeze a value larger than a char through BOOL, the compiler will happily truncate the upper bits, slicing them off."
Now I fixed the issue simply sending boolValue message to a particular object. Check the working code below.
[[userData objectForKey:@"IS_AGENT"] boolValue];

Ok fine.My question is "Why is it happening in a particular devices (iPhone5s,iPhone5c,iPhone5,iPhone6+) only and not in iPod touch which is having the same iOS 8.1.1?"
Comment: Are you sure you got `NO` on the cited iPhones and `YES` on iPod Touch? I would expect you _always_ get `YES` on those iPhones and _sometimes_ `NO` on the iPod Touch.
Comment: I'm getting always NO on the cited Phones only in iOS 8.1.1. But prior iOS versions on the same cited Phones I'm getting the correct value.. And In iPod Touch I always get the correct value.
Comment: @l0gg3r: What's the reason for the (IMHO destructive) edit?
Comment: @DarkDust as we understand our poster never accepts answers, and keeps editing via changing the meaning of question, so I reverted to initial question.
Comment: @l0gg3r: I object to this. 1) The edits to this question did not alter the meaning of the question in any way. 2) Whether he accepts questions or not is an entirely unrelated matter. Please revert your edit.
Comment: @DarkDust Sure. I should have done that. Thanks!
Answer: Here's my speculation what happened before iOS 8.1.1. Maybe Apple changed the way tagged pointers work here, I haven't had an opportunity to test it yet and haven't found any references whether Apple did change something in this system.
You were casting a pointer to a BOOL. Since we're talking about little endian systems here, a pointer like 0x12345678 casted to char yields 0x78, when you cast to BOOL you get 1 since the byte is not 0. On 64-bit iOS systems with 64-bit binaries, the runtime uses tagged pointers.
For tagged pointers, the first byte (which are the least significant digits, we're little endian) will always have the last bit set to mark it as a tagged pointer. If you cast that byte to BOOL you'll always get 1/YES.
But for non-tagged pointers, due to aligning, the last four bits are always 0. And by chance it can happen that the other bits of that are are 0 too (in my tests I've only seen multiples of 0x20 so every 8th object would have 0x00). When you cast that to a BOOL you'll get YES most of the time and sometimes NO.
As you already found out, the correct way to query the value is [[userData objectForKey:@"IS_AGENT"] boolValue] or simply [userData[@"IS_AGENT"] boolValue] if your deployment target is iOS >= 6.
Comment: And one more thing, In iOS 8.2 Beta I'm getting the correct value in all the above-mentioned devices. i.e. (BOOL) [userData objectForKey:@"IS_AGENT"]; method gives me the proper value which I saved..
Answer: First of all you are using setValue:forKey: and valueForKey: that are KVC methods,
This is strong mistake.
You need to use setObject:forKey: and objectForKey: to write/read objects from NSMutableDictionary.
Also you are casting NSNumber to BOOL, which will have unexpected behavior.
Instead of that code, use
    [userData setObject:@YES forKey:@"IS_AGENT"];
    BOOL isAgent = [[userData objectForKey:@"IS_AGENT"] boolValue];

Comment: Sorry, I used objectForKey only not valueForKey, I corrected it in my question... Also, I fixed the issue that is not my question... This issue is only coming in a particular devices not all the iOS 8.1.1 devices.. So It is better to know the reason...
Comment: what? that' a pointer to NSNumber, you are casting NSNumber instance pointer to char which is undefined
Comment: Yes, you are correct. Then It should return NO in all the devices, right? In iPod touch, it is working fine. I get the proper value that what I saved.
Comment: if the architecture is 32 bit then it will return YES always, on 64 bit architecture it will return NO
Comment: even if you will set @NO it will return YES on architecture 32bit
Comment: @l0gg3r: Can you please explain why this should always be YES on 32-bit (I think: sometimes NO) and always NO on 64-bit (I think: always YES)? See my answer, did I miss something?
Comment: even i dont understand the logic , why @NO  will return YES on architecture 32bit ?
Comment: @pawan because you're casting NSNumber pointer (memory address) to BOOL
Comment: @l0gg3r iPhone5c is 32bit only, but In that device also It always returns NO
Comment: @Jirune you are doing same thing as `NSObject *obj = [NSObject new]; NSLog(@"%d", (BOOL)obj);` what will this output? some shit probably. instead of guessing what it will output pointer cast into BOOL, just use `boolValue` and your code will start working. Did you try my provided example?
Comment: @Jirune initially your question was another, so create a new question and ask it there, `When I cast object to BOOL, I get random value`


How to rotate a sinusoid on the axis

Question: I'm programming a game in 2D. What I achieved is to move a sprite on the basis of a custom path.
The path may be represented mathematically as: y = sin (x) ..So the movement is a wave.
I wanna to rotate this wave in such way so the movement is not horizontal but vertical or with some custom angle respect the origin.
I'm a litte bit weak with math. Sorry. Anyone can help.
My code is this
for (int i=0; i<300; i++) {
  coordinatesX[i] = i;
  coordinatesY[i] = (float) (50 * Math.sin(coordinatesX[i]));
}

createpath (coordinatesX, coordinatesY);
...

Comment: to rotate it 90 degrees counter-clockwise just exchange X and Y values
Answer: Well, your object is represented with some starting coordinates (x,y)^t. In order to rotate that in 2D space, you would use the rotation matrix
R = [ cos(a) -sin(a)]
    [ sin(a) cos(a) ]

Since you also want to perform a translation T (move along the sine wave), you can make up an affine transformation by extending you 2D coordinates to 3D homogenous coordinates. Assume your translation will be (tx,ty) and your angle of rotation (in radians) is a, the transformation matrix will be
T = [ cos(a) -sin(a) tx
      sin(a) cos(a)  ty
       0       0     1 ]

When you transform your original (x,y) point to (x,y,1) a simple
 T * (x,y,1)^t

will do the trick.
You can go back from homogenous to cartesian coordinates by dividing all elements by the last one (i.e. you loose one dimension). Since in this simple case they are always 1 you can simply drop the last coordinate and are back in 2D.
Edit:
Mulitplying T and (x,y,1)^t yields:
T*(x,y,1)^t = [ cos(a) -sin(a) tx ] [ x ]
              [ sin(a) cos(a)  ty ]*[ y ]  =
              [   0      0      1 ] [ 1 ]

             = [ cos(a)*x - sin(a)*y + tx ]
               [ sin(a)*x + cos(a)*y + ty ] =
               [           1              ]

             = (cos(a)*x - sin(a)*y + tx, sin(a)*x + cos(a)*y + ty, 1)^t

Comment: 'sin(a) cos(a)' means sin(a) multiplied by cos(a) ?
Comment: No, T is a matrix with three rows and three columns. Have a look here: http://en.wikipedia.org/wiki/Matrix_multiplication
Comment: I edited my answer to include the maths (note ^t means "transpose")

How to rotate a sinusoid on the axis


Is it possible to combine a Bootstrap theme with an existing custom Drupal 6 theme?

Question: Is it possible to combine a Bootstrap theme with an existing custom Drupal 6 theme?
I want to use Grayscale in my Drupal 6 website. But I already have an existing custom theme and I am not sure if I can simply use it. Or whether the styling will interfere with other stylesheets in Drupal.
Answer: There is the Bootstrap base theme for D7 (and even an upcoming D8 version), which bridges the gap between Drupal and the Bootstrap Framework, with also quite a few (contributed) sub-themes available for it already.
So if your question was about D7, then it seems such combination that you're looking for should be possible. As you may know already, the Bootstrap base is not available for D6. But maybe you can use the D7-version of it as inspiration to somehow make such combination also work for D6. Ultimately even come up with a backport of the Bootstrap base theme for D6?
Note: as mentioned by Molot's in the comment to this answer, there is "some" D6 backport of it available on github ... Via Drupal.org there doesn't seem to be any D6 versions, however there "is" some D5 version of it also ... Maybe upgrading the D5 version to D6 is another approach to consider? Using Browse code repository only results in D7/D8 versions though ...
Comment: Backport [is on github](https://github.com/sdmunoz/Drupal-6-Theme-Bootstrap) - I don't know it's quality, I just see it's there.
Comment: I wasn't aware of that, thanks for the hint @Mołot !!!


drupal image upload programmaticaly

Question: i am using drupal 7 . and i am trying to upload image progrmatically .. here is my code
$image = file_get_contents('http://drupal.org/files/issues/druplicon_2.png'); // string
$file = file_save_data($image, 'public://druplicon.png',FILE_EXISTS_REPLACE);
print_r($file); exit

and here is my output
stdClass Object
(
    [fid] => 4
    [uri] => public://druplicon.png
    [filename] => druplicon.png
    [filemime] => image/png
    [uid] => 1
    [status] => 1
    [timestamp] => 1378142542
    [filesize] => 0
)

now here filesize is 0 . as file doesnot contain any data.  i am working on local m/c.  here is no issue of permission.
Does anybody tell me how to solve this
Thanks
Comment: I agree with @Daniel Verite answer. In addition please remember that `drupal.org` is using `https` protocol for some time now. You can test your code using another source, e.g: `$image_uri = system_retrieve_file('http://ipsumimage.appspot.com/140x100.png'); $image = image_load($image_uri); drupal_set_message('' . print_r($image, true) . '');`
Comment: @Jack-PL: i tried above code. this is not working .
Comment: I tested this code and it is working. I wrote the answer with results.
Answer: The ability for file_get_contents() to fetch contents from an HTTP server depends on the allow_url_fopen parameter in php.ini
If it's not set, $image will be false and saving this leads to an empty file.
You may use Drupal's system_retrieve_file() instead. It's a wrapper on top of drupal_http_request() that comes with its own HTTP client implementation.
Example:
$file = system_retrieve_file('http://drupal.org/files/issues/druplicon_2.png',
                             'public://druplicon.png', false,  FILE_EXISTS_REPLACE);

Comment: @Daneil : allow_url_fopen=on.  still filesize=0. Can u give me example code of system_retrieve_file
Comment: @Rajat: OK. But you should test `$image` after the load  to know whether it's this or the save that fails. There are other reasons why an http request could fail,  like a network security policy.
Comment: Can u tell me how to test it. i m newbie in drupal
Comment: HTTP error -1695770671 occurred when trying to fetch http://drupal.org/files/issues/druplicon_2.png
Comment: @Rajat: so it looks like your server is not allowed to issue external http requests (although the error code is not very informative).
Comment: yeah. i am checking this on local m/c.
Answer: $image_uri = system_retrieve_file('http://ipsumimage.appspot.com/140x100.png');
$image = image_load($image_uri);
drupal_set_message('
' . print_r($image, true) . '
');

result
stdClass Object
(
    [source] => public://140x100.png
    [info] => Array
        (
            [width] => 140
            [height] => 100
            [extension] => png
            [mime_type] => image/png
            [file_size] => 2534
        )

    [toolkit] => gd
    [resource] => Resource id #443
)

Do not use use drupal.org images for testing.
If you will use drupal.org path, then you can get the following errors:
using http protocol
HTTP error -5 occurred when trying to fetch http://drupal.org/files/issues/druplicon_2.png

using https protocol
HTTP error 0 occurred when trying to fetch https://drupal.org/files/issues/druplicon_2.png.

drupal image upload programmaticaly


Nginx Server Block Config

Question: I'm having a problem with my nginx configuration. I have several virtual hosts on the same server. However, I'm attempting to add another virtual host, this one being an SSL website. The website returned for the domain is not the correct one.
If I change from listen 443 to listen 80, the correct website is restored. I cannot figure out why this is happening. Any help would be greatly appreciated, thanks.
Below is the configuration for the vhost in question.
vhost-config
server {
    listen 443 ssl default_server;
    server_name *.website.com;

    root /var/www/vhost/www.website.com/public_html/httpdocs/;
    index index.php;

    ssl_certificate /etc/nginx/ssl/www.website.com/website.com.crt;
    ssl_certificate_key /etc/nginx/ssl/www.website.com/website.com.key;

    location / {
            try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_param  REQUEST_URI    $request_uri;
            fastcgi_param  DOCUMENT_URI   $document_uri;
            fastcgi_param  DOCUMENT_ROOT  $document_root;
            fastcgi_param  REMOTE_ADDR    $remote_addr;
            fastcgi_param  REMOTE_PORT    $remote_port;
            fastcgi_param  SERVER_ADDR    $server_addr;
            fastcgi_param  SERVER_PORT    $server_port;
            fastcgi_param  SERVER_NAME    $server_name;
            fastcgi_param  QUERY_STRING   $query_string;
            fastcgi_param  REQUEST_METHOD $request_method;
            fastcgi_param  CONTENT_TYPE   $content_type;
            fastcgi_param  CONTENT_LENGTH $content_length;
            fastcgi_hide_header X-Powered-By;
    }

}

nginx.config
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
worker_connections 768;
# multi_accept on;
}

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##

#include /etc/nginx/naxsi_core.rules;

##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##

#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

##
# Size Limit
##

client_max_body_size 2M;
}

Comment: Have you checked the error log?
Comment: Do you have a default vhost also listening for SSL ? What do you mean by "the website returned for the domain is not the correct one" ? Could you clarify with actual observed behaviour ? Is there anything else running on port 443 on the machine ?
Comment: There is no default vhost listening on SSL. The website that's returned is the last of the websites in the /etc/nginx/sites-enabled/* folder in Ascii order. Nothing else running on port 443.
Comment: What's the overall count of server names handled by included configurations ?
Comment: 14 overall server names handled. If I change the listening port to 80 in the server directive, the website loads just fine.
Comment: Do you test this behaviour with curl or something not using cached response headers ? What version of nginx are you running ? Put your log in debug mode and post it with the curl request & response.
Answer: Do you have more then one virtual host with SSL enabled? I think they both are the same IP and you are trying to check using something that does not support SNI, so domain name is not used to choose correct virtual host.
So please post the whole configuration file and logs. Maybe answer is there.
Comment: Glueon - Thanks for your reply. There actually isn't any other website using 443/SSL. I've also posted the configuration for nginx that each and every vhost use. Thanks.
Comment: Where are the log files and other virtual host configurations?
Comment: Still i see only one server {} directive. Where does the content of the wrong domain come from? I think there are some more virtual hosts declared under /etc/nginx/conf.d/*.conf and /etc/nginx/sites-enabled/*
Comment: Glueon - There are other vhosts declared, and all are listening on 80. However, anytime one of the vhost configurations are corrupt, the page the default page loads which in this case is the last server name in ascii order in the /etc/nginx/sites-available.

Nginx Server Block Config


Wildcard module declaration - Typescript 3.5

Question: I can declare a module in TS:
declare module '*.svg' {
    const content: string
    export default content
}

then i can import it: import svg from './src/file.svg, no errors would be thrown.
But if i use import svg from './src/file.svg?query, then i would get an error: Cannot find module ./src/file.svg?query
Is there a possibility to treat ./src/file.svg?query as ./src/file.svg?
Comment: Have you tried `declare module "*.svg*"`?
Comment: Pattern '*.svg*' can have at most one '*' character.
Comment: hmm TypeScript docs would define that as declare module "*?query" { ... }, typescriptlang.org/docs/handbook/… . Really not optimal, but there doesn't seem to be another solution.
Comment: @ford04 so i have to declare 2 modules: one without `?query` and another with `?query` ?
Comment: Yeah, seems so, I do not know another way. TypeScript docs do it like this - here the (now complete) link: https://www.typescriptlang.org/docs/handbook/modules.html#wildcard-module-declarations
Comment: Thanks, i've read this before. Also `?query` won't work, compiler still complains.
Answer: It's possible with a custom TypeScript plugin, here's an example of a plugin I built for a different purpose. It essentially strips ?client and ?server from the imports so that Typescript sees it without the queries. A similar change can be made for your use case.
Answer: There is no proper solution as far I can tell. The workaround that I'm using is to simply use:
declare module '*'
Of course this would treat everything as a valid module, which may be problematic with any type checking done by the IDE.


Validação de datas no model Asp Net Core?

Question: Tenho um modelo e queria validar a data final (que deve ser igual ou maior a data inicial)
public class MyModel
{
    [Key]
    public int ModelId { get; set; }

    [Display(Name = "Início")]
    [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
    public DateTime? StartDate { get; set; }

    [Display(Name = "Fim")]
    [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
    public DateTime? FinishDate { get; set; }

}

Alguma ideia de como validar um campo com base em outro em um modelo?
Comment: você esta usando dataannotations?
Comment: Estou usando sim
Comment: @WallaceReis foi colocado as duas versões a ASPNetCore mudou somente a classe de interface o resto a implementação é igual a anterior.
Answer: Versão anterior ao ASPNET CORE
Deve ser criado um classe Customizada que herda da classe abstrata ValidationAttribute para validação do servidor e a Interface IClientValidatable para validações cliente, o exemplo básico para validar se uma data é maior ou igual a outra é:
Crie uma classe e herde e implemente respectivamente ValidationAttribute e IClientValidatable:
public class DateTimeCompareAttribute :
        ValidationAttribute, IClientValidatable

{
    public string NameCompare { get; set; }
    public DateTimeCompareAttribute(string nameCompare)
    {
        NameCompare = nameCompare;
    }
    protected override ValidationResult IsValid(object value,
                                                ValidationContext validationContext)
    {
        if (validationContext.ObjectInstance != null)
        {
            Type _t = validationContext.ObjectInstance.GetType();
            PropertyInfo _d = _t.GetProperty(NameCompare);
            if (_d != null)
            {
                DateTime _dt1 = (DateTime)value;
                DateTime _dt0 = (DateTime)_d
                     .GetValue(validationContext.ObjectInstance, null);
                if (_dt1 != null &&
                    _dt0 != null &&
                    _dt0 <= _dt1)
                {
                    return ValidationResult.Success;
                }
            }
        }
        return new ValidationResult("Final Date is less than Initial Data");
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(
                                                  ModelMetadata metadata,
                                                  ControllerContext context)
    {
        ModelClientValidationRule rules = new ModelClientValidationRule
        {
            ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
            ValidationType = "datetimecompare"
        };

        rules.ValidationParameters.Add("param", NameCompare);
        yield return rules;
    }
}

Após a elaboração dessa classe tem que configurar na classe que vai ser validata mediante esse atributo da seguinte forma:
public class MyModel
{
    [Key]
    public int ModelId { get; set; }

    [Display(Name = "Início")]
    [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
    public DateTime? StartDate { get; set; }

    [Display(Name = "Fim")]
    [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
    //configuração para verificar se a data é maior ou igual a que foi dita no parâmetro
    [DateTimeCompare("StartDate")]
    public DateTime? FinishDate { get; set; }

}

essa configuração já valida os dados no servidor, mas, para ficar uma validação completa é legal adicione um trecho javascript (jquery) para também ter a validação cliente, exemplo:
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

}

com essas configurações vai validar a cliente que é o javascript e a do servidor.

Versão para ASPNET CORE
Para ASPNET CORE tem uma mudança na classe de Interface que a responsável em fazer a validação no lado cliente é a Interface IClientModelValidator, mas, as mudanças são minimas, observe a classe DateTimeCompareAttribute:
public class DateTimeCompareAttribute :
        ValidationAttribute, IClientModelValidator
{
    public string NameCompare { get; set; }
    public DateTimeCompareAttribute(string nameCompare)
    {
        NameCompare = nameCompare;
    }
    protected override ValidationResult IsValid(object value,
                                                ValidationContext validationContext)
    {
        if (validationContext.ObjectInstance != null)
        {
            Type _t = validationContext.ObjectInstance.GetType();
            PropertyInfo _d = _t.GetProperty(NameCompare);
            if (_d != null)
            {
                DateTime _dt1 = (DateTime)value;
                DateTime _dt0 = (DateTime)_d
                     .GetValue(validationContext.ObjectInstance, null);
                if (_dt1 != null &&
                    _dt0 != null &&
                    _dt0 <= _dt1)
                {
                    return ValidationResult.Success;
                }
            }
        }
        return new ValidationResult("Final Date is less than Initial Data");
    }

    public void AddValidation(ClientModelValidationContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }
        context.Attributes.Add("data-val-datetimecompare", ErrorMessageString);
        context.Attributes.Add("data-val-datetimecompare-param", NameCompare);
    }
}

Após a criação dessa classe o resto continue seguindo o exemplo para versão anterior ao ASPNET CORE, porque, é da mesma forma, só lembrando os passo:

decorando o FinishDate com o DateTimeCompare
e adicionando o javascript (jquery).

Referencias:

Classe ValidationAttribute
Interface IclientValidatable
Interface IClientModelValidator
client-side validation in custom validation attribute - asp.net mvc 4 - SOEn
How to create Custom Data Annotation Validators - SOEn
Custom Unobtrusive jQuery Validation With DataAnnotations in MVC 3
Criando uma Validação Customizada para o CPF – Custom Validation Attribute
Introduction to ASP.NET Core
Comment: É isso ai mesmo obrigado @virgilio qual a grande necessidade de uma validação no cliente?
Comment: @WallaceReis você obriga a informação ir para o servidor mais formatada possivel, e isso pode ser economia de banda já visto que seu form já tem uma pre - validação... !!! e não precisa ficar enviando várias vezes para conferir. é uma boa prática ter, mas, o essencial é a do servidor essa é garantia de eficiencia


Ear pain when using on-ear headphones

Question: I have a set of Bowers and Wilkins P3 headphones that I've had for about 2 months now and they are pretty comfortable but sometimes I get a very sharp pain in my right ear. I have experimented and found that the pain will happen even if there is no music playing, so it isn't an issue of volume.
Is this a common issue with on-ear headphones? Is there something I can do to alleviate the pain, or should I look into a different style of headphones?
Comment: The original question was ripe for closure, but I have had a similar problem with over-ear headphones so I've edited to bring it on-topic
Answer: I don't know exactly where your pain is located and how much these headphones apply pressure on your ears, but I used to experience some pain (in the pinna) due to the pressure of my headphones when I wore them for too long.
Now I'm used to it and don't feel any pain at all even after an entire day wearing them.
Comment: http://en.wikipedia.org/wiki/Outer_ear
Comment: It starts along the auricle and radiates down inside the ear canal. The helix also tends to be quite painful. So if it's a pressure thing my ears will eventually just toughen up? I assumed that was the case as I've been using in-ear buds for maybe 10 years and this is my first ever on-ear set.
Comment: The most painful area in my case was the helix, but I didn't feel any pain inside the canal.
Comment: @AaronLang, please do edit the current question with some specifics about where the pain is occuring

Ear pain when using on-ear headphones


Futuro do Javascript, como saber das possíveis features/novidades da linguagem?

Question: É um fato que o Javascript tem crescido muito nos últimos anos e tem se tornado cada vez maior, mas qual seria os caminhos para se manter antenado em relação as novas features?
Temos o MDN
Quais são os locais oficiais de propostas de features novas?
Como saber o cronograma de aceite/exclusão de uma feature nova?
Apesar de não ser uma pergunta técnica, ela faz sentido pois a comunidade precisa de um mapa.
Comment: Talvez isto https://www.ecma-international.org/news/index.html e isto https://ecmascript-daily.github.io/ ... estou votando pra fechar, pq mesmo q a pergunta seja interessante, não é bem o lugar para se perguntar, lembrando que o você pode usar o chat oficial do site https://chat.stackexchange.com/rooms/11910/estouro-de-pilha...
Comment: ... Ainda sim preciso dizer, com ou sem novidades, muita coisa do ES6 em diante, em sintaxe, me parece firulas, e poderia escrever facilmente o mesmo em ES5, muita coisa é pura moda. Agora sobre as APIs, é preciso entender que mesmo existindo na especificação (ECMAScript) não significa que os navegadores irão suportar de pronta, é bem provavel que muitas implementações levem tempo, e tem que observar se é "Draft" (rascunho), pq nem tudo q esta em ideia ainda significa que será aprovado no ECMA.
Comment: Eu geralmente não negativo, só quando a pergunta é um erro de digitação ou é SPAM, agora votar pra fechar eu votei, e eu inclusive respondi nos comentários, não vejo resposta além do que eu já comentei, não entendo o que você espera, a pergunta não é um duvida com algoritimos, linguagem e nem softwares, então não é escopo, mas vou formular uma resposta antes que venha alguém e responda qualquer ou copie os comentários e responda, pq infelizmente (apesar de poucos) isso as vezes ocorre.
Answer: Primeiro é preciso entender que não é porque uma função, método, API ou sintaxe novas são definidas que isso vai existir magicamente em todos navegadores, definir algo só diz como algo deve ser, quem defini nem cria a implementação interna, ou seja não codificam nada, no entanto não pense mal deles, é graça a eles (que logo vou citar quem são) que diferenças entre navegadores foram sendo diminuídas.
JavaScript e EcmaScript
Quase tudo que precisamos falar disto eu expliquei https://pt.stackoverflow.com/a/80001/3635, leia antes por favor, então vou dar uma breve resumida:

A Netscape (empresa), na época com o navegador de mesmo nome criou um dialeto para tornar o navegador mais dinâmico que teve 3 nomes, o ultimo JavaScript (nada relacionado com Java)
A Microsoft para não deixar seu navegador pra trás criou um equivalente chamado JScript

Resumindo cada empresa tinha seu dialeto/linguagem, parecidos, mas não iguais, então depois veio a ECMAScript um dialeto em 1997, ou seja as linguagens de ambos navegadores e outros navegadores aderiram ao ECMAScript, mas claro ainda sim cada um (e novos) implementaram suas próprias funcionalidades, APIs, etc.
Ok, então apesar de chamarmos tudo de JavaScript, não é necessariamente isto, só é popular chamar, talvez como chamar esponja de aço por uma marca achando que aquele é nome de todas.
Como saber o que é novidade
Tendo entendido quem e o quê define os "JavaScripts" de cada navegador (ou "motor", por diferentes navegadores podem usar o mesmo motor, o caso do novo Edge, usará Chromium) agora podemos ir para o ponto principal, não é porque a ECMA lançou algo novo no ECMAScript, do qual você pode acompanhar em:

https://www.ecma-international.org/news/index.html

Que significará que um navegador irá de pronto imediato, a ECMA só define, quem desenvolve os motores dos navegadores terá que transformar as normas em código funcionais e ainda talvez tenha que adaptar isto para diferentes sistemas operacionais como:

Mobiles
MacOS
Diferentes Linux
Windows

Ou seja, lançou no ECMA não significa que você poderá sair usando, então para saber quando um navegador disponibilizará a nova funcionalidade é só acompanhando os changelogs
Changelogs dos principais navegadores

https://www.mozilla.org/en-US/firefox/releases/
https://developer.microsoft.com/en-us/microsoft-edge/platform/changelog/
https://chromereleases.googleblog.com/

PS: Eu diria que não só JavaScript, mas qualquer linguagem de programação, quer ver novidades? Então acompanhe o changelog nos site oficiais, isso seria mais que suficiente para uma resposta, só o JavaScript tive que estender porque ele é um pouco mais complicado de se entender.
DevTools de navegadores baseados em Chromium
No entanto para sua felicidade, se usa Chrome e você é desenvolvedor então com certeza você usa o DevTools, apertando F12 geralmente, irá notar embaixo um "What's news", nele contêm mudanças interessante, nem todas haver com JavaScript, muitas haver com o DevTools e até com CSS, mas as vezes aparece algo com JavaScript, mas depende do que foi lançado, exemplo:

No Opera, Edge (novo), Vivaldi, Bravo, etc, que são baseados em Chromium, todos irão apresentar o mesmo, mas claro que nem todos vão acompanhar o Chrome ou Chromium em tempo, no entanto posteriormente vão passar exatamente pelo mesmo


Contar quantas colunas há em um arquivo CSV com C++

Question: Estou fazendo um projeto de uma urna eletrônica, e para isso preciso ler um arquivo csv onde há as informações sobre cada candidato.
Como esse arquivo csv tem muitas informações que não são relevantes, eu decidir utilizar somente determinadas colunas do arquivo csv, por exemplo: NM_CANDIDATO, NM_PARTIDO, ...
A solução que pensei foi iniciar um contador para salvar o "índice" das colunas desejadas, porém não consigo determinar o final da primeira linha, e o índice continua sendo incrementado com todos os dados.
"DT_GERACAO"; "SG_PARTIDO"  ;  "HH_GERACAO"
"03/09/2018"; "DC"          ;   "08:01:43"
"03/09/2018"; "MDB"         ;   "08:01:43"
"03/09/2018"; "PODE"        ;   "08:01:43"

Nesse exemplo, somente a coluna SG_PARTIDO me interessa. Assim, um contador i é inicializado i=1 e durante o getline() da primeira linha é incrementado. Quando uma coluna desejada é encontrada, a posição dessa coluna é salva, para que quando o contador for inicializado i=1 na próxima linha alguma ação seja realizada na coluna desejada.
O código que escrevi é esse abaixo:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream file("presidentes.csv");

    if(!file.is_open())
    {
        cout<<"Erro! Não foi possível abrir esse arquvio"<<'\n';
    }

    string buffer;
    int i=1 p_SG_PARTIDO = 0;

    while(!file.eof())
    {
        getline(file, buffer, ';');

        if(buffer == "SG_PARTIDO" || i == p_SG_PARTIDO)
        {
            p_SG_PARTIDO = i;
            cout << buffer;
        }

        i++;

        if(buffer == "\n") i=1;
    }

    file.close();
    return 0;

}

Essa condição do buffer nunca é verdadeira. Desconfio que o motivo seja uma dupla aspas, ""SG_PARTIDO"". Quando eu removi o primeiro e o ultimo carácter, antes da comparação, essa condição se torna verdadeira, mas continuo com o problema de não saber quando a primeira linha acaba.
O código que retiro o carácter é esse abaixo:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream file("presidentes.csv");

    if(!file.is_open())
    {
        cout<<"Erro! Não foi possível abrir esse arquvio"<<'\n';
    }

    string buffer;
    int i=1;
    int p_ds_cargo = 0;

    while(!file.eof())
    {
        getline(file, buffer, ';');

        if(buffer[0]=='"') // remover """"
        {
            buffer.erase(0,1);
            buffer.erase(buffer.size() - 1);
        }

        if(buffer == "DT_GERACAO")
        {
            p_ds_cargo = i;
            cout << buffer << endl;
        }

        if(buffer == "\n") i = 1;

        i++;
    }

    file.close();
    return 0;
}

Eu agradeço desde já se alguém souber uma maneira mais fácil de ler somente colunas específicas em um arquivo csv.
O link para o csv que estou usando é esse:
https://gitlab.com/oofga/eps/eps_2018_2/ep1/raw/master/data/consulta_cand_2018_BR.csv?inline=false
Answer: Problemas
O arquivo csv tem todos os conteudos dentro de aspas duplas(") logo a comparação que tem nunca irá funcionar:
if(buffer == "SG_PARTIDO"

Contornar esse problema passa por comparar com as aspas:
if(buffer == "\"SG_PARTIDO\""

Ou remover as aspas do valor lido antes de comparar:
if(buffer.substr(1, buffer.size() - 2) == "SG_PARTIDO"

No entanto o teste de mudança de linha também não funciona:
if(buffer == "\n") i=1;

Pois a leitura com getline é sempre feita até ao ; logo nunca irá ler apenas a quebra de linha.
Outra Abordagem
Sugiro outra abordagem ao problema, que acaba por ser mais robusta e permitir-lhe também obter dados de múltiplas colunas, algo que ia ficar mais complicado da forma que estava a fazer.
A ideia é:

Ler cada linha normalmente com getline prevalecendo o delimitador normal \n
Obtem cada coluna de cada linha através de getline mudando o delimitador para ;
Armazena cada linha num vector<string> e todas as linhas num vector<vector<string> >

Leitura
Existem muitas maneiras de tentar ler um csv numa matriz, mas optei por uma que considero simples e que lhe permite obter a informação que pretende.
Implementação:
int main()
{
    //...
    string buffer;
    vector<vector<string> > linhas; //vetor de vetor para toads as linhas

    while(!file.eof())
    {
        getline(file, buffer); //ler cada linha
        stringstream ss(buffer); //colocar a linha lida num stringstream

        vector<string> linha; //iniciar o vetor da linha
        while (getline(ss, buffer, ';')) { //ler cada coluna
            linha.push_back(buffer); //adicionar ao vetor da linha
        }

        linhas.push_back(linha);
    }
    //...

Note que fiz uso tanto de vector como stringstream e por isso precisei de dois includes adicionais:
#include <vector>
#include <sstream>

Outra possibilidade aqui é adicionar apenas os textos já sem as aspas, que irá facilitar mais à frente quando precisar de mostrar e comparar, trocando linha.push_back(buffer); por linha.push_back(buffer.substr(1, buffer.size() - 2);, mas no resto da resposta assumo que foram adicionados com as aspas.
Utilização de uma coluna
Uma forma ingénua e não muito eficiente ainda que simples de obter apenas informação para todas as linhas referentes à coluna SG_PARTIDO é:
for (size_t i = 0; i < linhas.size(); ++i){
    for (size_t j = 0; j < linhas[i].size(); ++j){
        //se na primeira linha desta coluna tem SG_PARTIDO
        if (linhas[0][j] == "\"SG_PARTIDO\""){
            cout << linhas[i][j].substr(1, linhas[i][j].size() - 2); //mostrar sem as "
        }
    }
    cout << endl;
}

Naturalmente isto assume que a primeira linha tem os cabeçalhos do csv. Utilizei o substr para mostrar apenas o conteúdo excluindo as aspas duplas.
Exemplo de teste na minha máquina:

Utilização de múltiplas colunas
Se tiver interessado em várias colunas pode construir um vetor com os indices das colunas que está interessado e depois apenas iterar sobre esses:
vector<int> colunasRelevantes;
for (size_t i = 0; i < linhas[0].size(); ++i){
    string nomeCol = linhas[0][i].substr(1, linhas[0][i].size() - 2);
    if (nomeCol == "SG_PARTIDO" || nomeCol == "NM_CANDIDATO" || nomeCol == "NM_PARTIDO"){
        colunasRelevantes.push_back(i);
    }
}

for (size_t i = 0; i < linhas.size(); ++i){
    for (size_t j = 0; j < colunasRelevantes.size(); ++j){
        int coluna = colunasRelevantes[j];
        string texto = linhas[i][coluna].substr(1, linhas[i][coluna].size() - 2);
        cout << texto << "\t";
    }
    cout << endl;
}

É importante mencionar que tive que remover a ultima linha em branco do arquivo para não dar erro a aceder a colunas que não existem.
Em C++11 estes loops ficam mais simples, mas não o fiz inicialmente para não estar já a mostrar sintaxe que possa ser nova. Ainda assim vou deixar na mesma para ficar como referência:
//Esta parte fica igual
vector<int> colunasRelevantes;
for (size_t i = 0; i < linhas[0].size(); ++i){
    string nomeCol = linhas[0][i].substr(1, linhas[0][i].size() - 2);
    if (nomeCol == "SG_PARTIDO" || nomeCol == "NM_CANDIDATO" || nomeCol == "NM_PARTIDO"){
        colunasRelevantes.push_back(i);
    }
}

//Aqui c++ enhanced for loop
for (auto linha : linhas){
    for (auto coluna: colunasRelevantes){
        string texto = linha[coluna].substr(1, linha[coluna].size() - 2);
        cout << texto << "\t\t\t";
    }
    cout << endl;
}

Se precisar de utilizar muitas colunas então fica mais fácil de utilizar um vetor para esses nomes e construir os indices com base num duplo for:
vector<string> nomesColunasRelevantes = {"SG_PARTIDO", "NM_CANDIDATO", "NM_PARTIDO"};
vector<int> colunasRelevantes;
for (size_t i = 0; i < linhas[0].size(); ++i){
    string nomeCol = linhas[0][i].substr(1, linhas[0][i].size() - 2);
    for (string nome : nomesColunasRelevantes){
        if (nome == nomeCol){
            colunasRelevantes.push_back(i);
        }
    }
}

Contar colunas
Agora fica igualmente fácil de responder à questão que tem no titulo da pergunta:

Contar quantas colunas há em um arquivo CSV com C++

Basta aceder ao size() de qualquer uma das linhas:
cout << linhas[0].size();

Que dá 58 para o arquivo apresentado.
Comment: Gostei muito dessa nova maneira de ler as colunas relevantes!! Muitíssimo obrigado, você abriu minha mente!!
Comment: @DurvalCarvalho Sem problema estamos cá para ajudar :)


R Increasing Variable Based on Previous Occurrences

Question: I have a data frame of restaurant inspections ordered by date. For each observation, I want to add two additional variables to note how many total inspections this restaurant has had, and how many times they have failed. I would like to avoid using a for loop but I'm not sure how else to do this. Essentially, I currently have a data frame consisting of the first three columns of the data frame below and I would like to add the last two columns.
Initial data frame
    Restaurant_ID    Date         Result
    1                01/02/2011   Pass
    2                02/05/2011   Pass
    3                04/07/2011   Fail
    1                09/05/2011   Fail
    2                03/13/2012   Pass
    1                08/25/2012   Fail
    2                09/25/2012   Pass
    3                01/05/2013   Pass

Desired output 1
Restaurant_ID    Date         Result   total_inspect  failed_inspect
1                01/02/2011   Pass     1              0
2                02/05/2011   Pass     1              0
3                04/07/2011   Fail     1              1
1                09/05/2011   Fail     2              1
2                03/13/2012   Pass     2              0
1                08/25/2012   Fail     3              2
2                09/25/2012   Pass     3              0
3                01/05/2013   Pass     2              1

EDIT: I've realized that I actually want the last two columns to reflect the number of total and failed inspections prior to the current observation. So what I actually want is
Desired output 2
    Restaurant_ID    Date         Result   past_inspect  past_failed_inspect
    1                01/02/2011   Pass     0              0
    2                02/05/2011   Pass     0              0
    3                04/07/2011   Fail     0              0
    1                09/05/2011   Fail     1              0
    2                03/13/2012   Pass     1              0
    1                08/25/2012   Fail     2              1
    2                09/25/2012   Pass     2              0
    3                01/05/2013   Pass     1              1

Answer: This solution uses functions from the package tidyverse and lubridate.
# Create the example data frame
dt1 <- read.csv(text = "Restaurant_ID,Date,Result
1,01/02/2011,Pass
2,02/05/2011,Pass
3,04/07/2011,Fail
1,09/05/2011,Fail
2,03/13/2012,Pass
1,08/25/2012,Fail
2,09/25/2012,Pass
               3,01/05/2013,Pass",
               stringsAsFactors = FALSE)

# Load packages
library(tidyverse)
library(lubridate)

dt2 <- dt1 %>%
  # Convert the Date column to Date class
  mutate(Date = mdy(Date)) %>%
  # Sort the data frame based on Restaurant_ID and Date
  arrange(Restaurant_ID, Date) %>%
  # group the data by each restaurant ID
  group_by(Restaurant_ID) %>%
  # Create a column showing total_inspect
  mutate(total_inspect = 1:n()) %>%
  # Create a column showing fail_result, fail is 1, pass is 0
  mutate(fail_result = ifelse(Result == "Fail", 1, 0)) %>%
  # Calculate the cumulative sum of fail_result
  mutate(failed_inspect = cumsum(fail_result)) %>%
  # Remove fail_result
  select(-fail_result) %>%
  # Sort the data frame by Date
  arrange(Date)

Edit: Calculate the past inspection and fail count
dt3 <- dt2 %>%
  mutate(past_inspect = ifelse(total_inspect == 0, 0, total_inspect - 1)) %>%
  mutate(past_failed_inspect = ifelse(Result == "Fail" & failed_inspect != 0,
                                      failed_inspect - 1,
                                      failed_inspect)) %>%
  select(-total_inspect, -failed_inspect)

Comment: @person10559 Please see my update. `dt3` is the `Desired output 2`.
Comment: Thank you this is brilliant! I didn't know about the tidyverse package before so I appreciate you pointing me in that direction. I was hoping you could help me with the edit I just added. I would like the total and failed inspections to reflect the inspections prior to the current observation. At first I thought I could just subtract 1 from both columns, but that doesn't work for past_failed_inspect, as seen in the last row where failed_inspect and past_failed_inspect are the same for restaurant 3.
Comment: This has been incredibly helpful, thank you so much!


Reducing Matrix For General Solution

Question:
for a general solution:
$$ \begin{bmatrix} x\\y\\z \end{bmatrix} = \begin{bmatrix} 2s - 3t\\s\\t \end{bmatrix} $$, where s, t are arbitrary parameters.
Why is the general solution as stated above with two arbitrary parameters? I assume that it is because having three arbitrary parameters does not tell us anything interesting or further than what we already know going into the problem. Whereas with two arbitrary parameters we can now understand some additional relationship across the solution? Is there another general solution that can be found?
Comment: Is your first matrix equation really what you wanted to say?
Comment: Yeah I have written it correctly...perhaps I have done something incorrect in the reduction? Or should I not reduce the first matrix any further? technically it is a system of equations...I was just lazy and wrote it as a matrix and vector
Comment: So a non-zero three-by-three matrix really does equal a zero column vector?
Comment: yes that is as per the book that i am reading ( Linear Algebra 1 pg 83) Ma Siu Lun, Victor Tan and Ng Kah Loon
Comment: I believe you mean $$\begin{bmatrix}1&-2&3\\2&-4&6\\3&-6&9\end{bmatrix}\begin{bmatrix}x\\y\\z\end{bmatrix}=\begin{bmatrix}0\\0\\0\end{bmatrix}$$
Comment: I have added an image of the example...perhaps i have translated it incorrectly...apologies...I am struggling to make sense of it all still
Comment: No problem. The system in your picture would actually have a corresponding matrix equation as I have written it in my previous comment, rather than what you have in the body of your question. The addition of the vector $\begin{bmatrix}x\\y\\z\end{bmatrix}$ is actually quite important.
Comment: ahh okay...sorry and thanks for clarifying
Answer: Notice that $$A=\begin{bmatrix}1&-2&3\\2&-4&6\\3&-6&9\end{bmatrix}$$ is not invertible. This is because the second and third rows are multiples of the first, so by adding $-2$ times the first row to the second row, and by adding $-3$ times the first row to the third row we have the matrix $$\begin{bmatrix}1&-2&3\\0&0&0\\0&0&0\end{bmatrix}$$which has determinant $0$ obviously. Hence, $A$ has determinant $0$ and is therefore not invertible. Therefore, we cannot row reduce $A$ to the identity matrix. That is, we cannot reduce the matrix in question to $$\begin{bmatrix}1&0&0\\0&1&0\\0&0&1\end{bmatrix}$$
From the above argument we see that $A$ has rank $1$ and nullity $2$. This is why there are two "arbitrary parameters", or free variables, in the solution system (because the nullity, the dimension of the kernel, is $2$).
Comment: Yes. This is because the nullity is the dimension of the kernel, i.e. the dimension of the solution space to $A\vec x=\vec 0$. So the dimension corresponds to how many free variables we have.
Comment: Wonderful...this makes sense finally :)
Comment: so is the # of free variables always equal to the nullity?
Comment: Thanks a million this was super helpful!
Comment: Excellent! Happy to help. Any further questions just let me know.
Answer: You have a two-parametric solution because your linear system is in fact, one simultaneous equations with three unknowns.   The other two are just scalar multiples of the first and don't impart any new constraints on the variables' freedom.
$$\begin{array}{l:l}{x-2y+3z = 0 \\ 2x-4y+6z=0 \\ 3x-6y+9z=0}&{x-2y+3z = 0 \\ 2(x-2y+3z)=0 \\ 3(x-2y+3z)=0}\end{array}$$
So all you can say is $x=2y-3z$
... or, if you will, that $x=2s-3t$ when $y\gets s, z\gets t$
$$\begin{bmatrix}x\\y\\z\end{bmatrix}=s\begin{bmatrix}2\\1\\0\end{bmatrix}+t\begin{bmatrix}-3\\0\\1\end{bmatrix}$$
Comment: Thanks Graham....this is very clear...appreciate the answer !!

Reducing Matrix For General Solution


How to return values that are NOT in a list, compared to other values

Question: I'm in a production environment, trying to find a list of invoices that do not contain a specific item.
Example:
Invoice |  Item
    1 | Item A
    1 | Item B
      |
    2 | Item A
    2 | Item C
    2 | Item D
      |
    3 | Item B
    3 | Item D
      |
    4 | Item A
    4 | Item C
    4 | Item F

I'm looking for a query that will return all invoice numbers that do not contain Item B.
I could use NOT IN query, but I feel like that would return invoice numbers that do have Item B associated, but not in that row.
Any help is appreciated!
For the example, I expect the result to return Invoice numbers 2 and 4.
Comment: SO is not a code writing service: https://stackoverflow.com/help/on-topic
Answer: One simple approach uses aggregation:
SELECT Invoice
FROM yourTable
GROUP BY Invoice
HAVING COUNT(CASE WHEN Item = 'Item B' THEN 1 END) = 0;

Another approach would make use of a subquery of blacklisted items:
SELECT Invoice
FROM yourTable t1
WHERE NOT EXISTS (SELECT 1 FROM yourTable t2
                  WHERE t2.Invoice = t1.Invoice AND t2.Item = 'Item B');

Comment: Thank you, this helped!
Answer: With NOT EXISTS:
select t.*
from tablename t
where not exists (
  select 1 from tablename
  where invoice = t.invoice and item = 'B'
)

If you want only the invoice numbers:
select distinct t.invoice
from tablename t
where not exists (
  select 1 from tablename
  where invoice = t.invoice and item = 'B'
)

Answer: Try This:
select t1.*
from tblItem t1
where not exists
(select *
                  from tblItem t2
                  where t1.Invoice = t2.Invoice
                    and Item = 'Item B');

Answer: Just another option by avoiding NOT IN-
SELECT A.*
FROM your_table A
LEFT JOIN(
     SELECT * FROM your_table
     WHERE item= 'Item B'
)B ON A.Invoice = B.Invoice
WHERE B.Invoice IS NULL


Virtual Hosting FTP, FTPS and SFTP

Question: I have a need to setup an FTP server for many users to upload and download files.
The users are not consistent in their chosen protocol, some use FTP, some FTPS, some SFTP.
I would like to setup a server to provide this preferably using some Virtual Hosting arrangement that does not involve setting up system accounts for individual users. While the software for FTPS and FTP to enable this is readily available SFTP (the subsystem of SSH) does not appear to have many equivalents.
I have a single RedHat EL machine provisioned for this purpose.
Any suggestions?
Answer: In my experience, you are forced to use PAM to authenticate users using SSH. You may want to look into pam_pwdfile if you wish to keep things simple for storing users and create a /etc/pam.d/ssh file accordingly.
You probably want to create SFTP-only chrooted accesses for your users, OpenSSHv5 allows you to do so very simply with ChrootDirectory (quick example here).
Comment: likely can then auth any ftp against the same file, ty!

Virtual Hosting FTP, FTPS and SFTP


IOS: write a string in a file txt from an array

Question: I have an array composed by other array:
example my array with two arrays inside:
myArray = [(element1, element2, element3)],[(element4,element5,element6)]
this is only an example to show that myArray have two arrays (these elements are string)
now I want write in a txt file these elements in this way:
<code> element1#element2#element3;element4#element5#element6;
</code>
what is the code to create this string to write in txt file?
Answer: <code>    NSMutableString *printString = [NSMutableString stringWithString:@""];
    for(i=0;i<[myArray count];i++)
    {
        for (NSString element in [myArray objectAtIndex:i])
        {
            [printString appendString:[NSString stringWithFormat:@"%@#",element] ];
        }
        [printString appendString:@";"];
    }

    //CREATE FILE

    NSError *error;

    // Create file manager
    //NSFileManager *fileMgr = [NSFileManager defaultManager];

    NSString *documentsDirectory = [NSHomeDirectory()
                                    stringByAppendingPathComponent:@"Documents"];

    NSString *filePath = [documentsDirectory
                          stringByAppendingPathComponent:@"fileArray.txt"];

    NSLog(@"string to write:%@",printString);
    // Write to the file
    [printString writeToFile:filePath atomically:YES
            encoding:NSUTF8StringEncoding error:&error];
</code>


Calculate first principal component direction and scores

Question: Given that x1 =  (9, 9, −18)^T and x2 = (18, 9, 9)^T with eigendecomposition of its sample covariance matrix Σ = cov(X)

How do I calculate the first two principal component direction and the principal component scores of x1 and x2 corresponding to the first two principal component direction
Answer: The columns in the first matrix of the expansion corresponds to principal components of yours, corresponding to eigenvalues in the diagonal, in the same order. Note that they're unit norm, i.e. direction vectors. So, the first two largest eigenvectors, i.e. principal components, are the third and first ones; and the scores of $x_i$ are the dot products of $x_i$ with these components.
Comment: I'm having trouble following this, because you seem to be asserting that the $x_i$ are eigenvectors of $\Sigma$--but they are not.
Comment: If i understand this correctly, first principal component direction is 4 and second principal component direction is 2, the scores are (4 2)^T ⋅ (9, 9, −18) ⋅  (4 2) and (4 2)^T ⋅ (18, 9, 9) ⋅  (4 2) respectively?
Comment: Principal components are vectors with same dimension as your data vectors. First PC is [1/3,2/3, -2/3] for example, which is the third column of the first matrix. @whuber, I corrected a terrible verbal mistake of mine, but I don't seem to understand if it addresses your comment.
Comment: It does, thank you: I had hoped to see the term "dot product" appear somewhere in the answer!
Comment: Ok so first PC direction is dot product of the following, (9, 9, −18) ^T ⋅ (1/3 2/3 -2/3) ⋅  (9, 9, −18)  @whuber. Sorry, I am really struggling to understand the application.
Comment: Direction is a vector, score is a scalar. I've already told the first PC direction in my last comment. For the score of $x_i$ on the 1-st PC: you have $x_i ⋅ pc_1$, where ⋅ denotes the dot product.


Probability Dice Game Follow Up

Question: Original Question:
Paul, Dave and Sarah are rolling a fair six sided die. Paul will go first, always followed by Dave, who is always followed by Sarah, who is always followed by Paul, and so on... What is the probability that Sarah will be the first one to roll a six?
Here's my work, I found the following formula in s, the number of sequences Sarah did not roll a six, terms
(5/6 * 5/6 * 5/6)^s * (5/6 * 5/6 *1/6)
(5^3/6^3)^s * (5^2/6^3)
(5^3s/6^3s) * (5^2/6^3)
$\dfrac{5^{(3s+2)}}{6^{(3s+3)}}$ = $\dfrac{5^{(3s+2)}}{6^{(3)(s+1)}}$
$\dfrac{5^{(3s+2)}}{216^{(s+1)}}$
And then we would use the summation formula for
$\displaystyle \sum_{s=0}^{\infty} \frac{5^{3s+2}}{216^{s+1}}$.
Would this be an acceptable answer or do I need to find an exact value
Comment: try to find the probability of P and D not rolling a 6. Then use a simple geometric distribution
Answer: It is the relative probability that you will have to calculate.
Answer
Probability that Sarah will be the first one to roll the winning number = $$\frac{(5/6)*(5/6)*(1/6)}{(1/6)+(5/6)*(1/6)+((5/6)*(5/6)*(1/6)}$$ $$=\frac{25}{91}$$
Answer: Your summation is correct.  You can evaluate it by writing is as
$$
\frac{25}{216} \sum_{s=0}^{\infty} \frac{5^{3s}}{216^s} =
\frac{25}{216} \sum_{s=0}^{\infty} \left(\frac{125}{216}\right)^s =
\left(\frac{25}{216}\right) \frac{1}{1-\frac{125}{216}}=\frac{25}{91}.
$$
Answer: $\displaystyle \sum_{s=0}^{\infty} \frac{5^{3s+2}}{216^{s+1}}$ can be simplified into $\displaystyle \sum_{s=0}^{\infty} (\frac{125}{216})^{s} \frac{25}{216}$, which can be futher simplified too.

Probability Dice Game Follow Up


How is the sample mean an unbiased estimator of the population mean via deeplearningbook.org?

Question: So I know that the sample mean is a unbiased estimator of the population mean.  Just wondering how the author gets from 5.33 to 5.34 in the below.
How do you get from $\mathbb{E}[\mu_m]$ to just $\mu$ not $\hat{\mu}$
Answer: We are told that $x^{(i)} \sim N(\mu, \sigma^2)$ from equation $5.29$.

$$p(x^{(i)}; \mu, \sigma^2) = \frac1{\sqrt{2\pi\sigma^2}}\exp\left( -\frac12 \frac{(x^{(i)}-\mu)^2}{\sigma^2}\right)\tag{5.29}$$

Hence $\mathbb{E}(X_i)=\mu.$
Note that $\mu_m$ is a statistics, not a parameter, where $\mu_m = \frac1m \sum_{i=1}^mx^{(i)}$.
Comment: I don't get how we know the true $\mu$ if all we have access to is the sample data.  In 5.33, we have $E[x^i]$ which is the expected value of our sample xi.  That should equal the sample mean, not the population mean.  Not sure how he is able to plug the population mean into 5.34.
Comment: You do not need to know the value of $\mu$ in order to write down notation $\mu$.  If you are told that $X \sim N(\mu, \sigma^2)$. Then $E[X]=\mu$. The sample mean $\mu_m$ is an approximation of $\mu$. The proof has illustrated that $E[\mu_m]=\mu$ using the linearity of expected value.
Answer: It's an assumption of his model.
Look at the previous page, Eq 5.29. He assumes that $x^{(i)}\sim\mathcal N(\mu,\sigma^2)$, i.i.d. Gaussian. By definition, then $E[x^{(i)}]=\mu$.


Probability of Consecutive rainfall days?

Question: I have a daily rainfall data measured in (cm), i have put some threshold and derived a categorical variable whether it is a rainy day or a dry day.
From this data i want to calculate Probability of a future day given last n-days, e.g.
what is the probability of a day being a rainy day given last 3 days were rainy day? there can be four instances -

P(rainy day| previous 3 rainy days) ?
P(rainy day| previous 3 dry days) ?
P(dry day  | previous 3 rainy days) ?
P(dry day  | previous 3 dry days) ?

Thanks
Comment: This is confusing: what would instances (2) and (4) have to do with the question, given that they concern the situation where the previous three days were dry rather than rainy?
Answer: Use conditional probability here. For instance if you want to calculate a  rainy day given 3 rainy days find ratio of P(3rainy days and 4th rainy day) to P(3rainy days). For numerator count all the cases of 4 consecutive days such that all four are rainy. For denominator count all cases such that 3consecutive days are rainy. Hope this helps you


ImportError: No module named redis_cache

Question: I am using Django=1.5.4 and I have installed redis properly in ubuntu. I installed django-redis. But I get this error. I put 'redis_cache' in my django settings. But I got this error
ImportError: No module named redis_cache

What to do?
Comment: whats's the output of `python2 -c "import redis_cache"`
Answer: You are supposed to add django_redis instead of redis_cache.
The documentation says:

if you are coming from django-redis < 3.8.x, you are probably using
  redis_cache. Since django-redis 3.8.x, redis_cache module is
  deprecated in favor to django_redis. The redis_cache module will be
  removed in 3.9.x versions.
Answer: Redis cache backend for Django
Run python setup.py install to install, or place django_redis on your Python path.
You can also install it with: pip install django-redis
Comment: Thanks! It works for me on python 3.6 and django 1.11
Answer: Use following command as available at this:
pip install redis-simple-cache

Answer: you are missing django_redis module run below command to add the module:
pip install django-redis

Answer: I was using docker compose + poetry to manage dependencies, and the solution for me to get rid of this Redis error was:

On Docker, delete all images and containers associated with the project
On VSCode, run the command Python: Clear cache and reload windows
Run docker compose up again

It seems like it was a cache issue, at least in my case.
Maybe there's a more surgical way of doing this, but these steps certainly are helpful when you've already double checked all dependencies and project settings.


How to access the blob storage file using link created by a logic app workflow. create SAS URL by path URL not working

Question: I am not able to use the generated link from this action to access the file I have uploaded to the blob storage.
I am able to only access the URL generated from the UI only. When I match the two URLs I can notice that the one generated from the logic app is missing half of the part.
Answer: After reproducing issue from my side got expected as results.
By removing  Web Url from the body of send email action i got the blob storage file using SAS link .
Web url does not include total path for the file. Hence use shared access signature which will include url of the file and signature also

Created logicapp workflow as shown below

In Get blob content action blob is taken as shown in below

In Create SAS URI by path action blob path should be List of files path .

In send an email action i have taken only Shared access signature and then i get complete URL to mail and i can download file from using that link as shown in below outlook images.
Comment: Thanks for the workflow. I got the link to the file now which works. One of the mistakes I was doing was I was filling in group flow Identifier as well in create SAS URI by path block.


Constructing a pivot-based confidence interval

Question: So I've been working on a problem in my probability class on which I have become stuck. It involves X1 X2 ... Xn ~ Poisson(lambda)
1 - We were instructed to show that X_bar was sufficient.
Sol'n: I did this using factorization, and by finding T(x) = Sum(x_i)/n, which came from the exponent position above my lambda from the likelihood/pdf. It could've also been achieved via the exponential family method.
2 - We were then instructed to show that X_bar^2 is approximately normal with mean \lambda^2 and a variance depending on \lambda
Sol'n: This was achieved using the central limit theorem and the delta method, which ended up giving me N(0, 4*lambda^3)
In step 3, however, we are asked "to display an approximate pivot based on the result in part #2, and then produce a 95% confidence interval for lambda^2"
We were given a hint, namely that
sqrt(n)(x_bar^2 - lambda^2) / sqrt (lambda * 4 lambda^2) ~ N(0,1)

I have, up until this point, no concept of what to do with a pivot or with this given info...I am looking for a number of degrees of freedom for a poisson table, which I think is wrong, but I've clearly got an issue with understanding what is being asked of me. If anyone could give me a nudge in the right direction, I'd be really grateful.
Answer: As opposed to a statistic, a pivotal statistic (or pivotal quantity) can depend on both the sample and parameters, but its distribution does not depend on any parameters. A good example is the standardized mean from a normal distribution:
$\frac{\sqrt n(\bar{X}-\mu)}{\sigma} \sim  N(0,1)$
This quantity depends on the sample and parameters, but its distribution does not depend on parameters so it is a pivotal quantity. When you are asked to construct a CI based on pivotal quantity, just use the critical values of the distribution of the pivotal quantity. So for the example I just gave (and using the approximate critical values for the standard normal), the 95% CI based on the pivotal quantity is:
$-2\leq\frac{\sqrt n(\bar{X}-\mu)}{\sigma}\leq2$
In your example, you just need to construct this interval based on the approximate distribution of the pivotal quantity and then solve for $\lambda^2$
Comment: thanks that was more insightful than my professor's been all year hahaha thank you so much for taking the time to help me!


What happens when bowler deliberately delays while bowling?

Question: I searched for Law to deal with the situation where the bowler deliberately pauses or delays while bowling but found nothing. Is there a law that I did not found, if there isn't then what does umpire do in this situation?
I am asking for situation when bowler may use this tactic to distract the batsman.
Answer: This would be covered by Law 42.9 (Fair and unfair play, Time wasting by the fielding side):

If either umpire considers that the progress of an over is unnecessarily slow, or time is being wasted in any other way by the captain of the fielding side or by any other fielder, at the first instance the umpire concerned shall [ ... ] warn the captain of the fielding side, indicating that this is a first and final warning.
[ ... ]
If either umpire considers that there is any further waste of time in that innings by any fielder, he shall [ ... ] direct the captain of the fielding side to suspend the bowler forthwith. The bowler thus suspended shall not be allowed to bowl again in that innings.

The incident would also be reported to the Governing Body responsible for the match.
Comment: So it may be called as dead ball in bowler parts.
Comment: Yes, one of the bits I've skipped there is "if the ball is in play, call and signal Dead ball."


What can I validly conclude about a variable that becomes significant and switches sign when other variables are included in the model?

Question: I have a dataset dat where each row represents a soil sample, with independent variables chemical measurements a, b and c, and a dependent variable soil erosion rate value.
Here is a printout of the data:
      a    b     c value
1  1.68 1.84  5.33  1.00
2  1.85 1.75  5.52  1.42
3  1.64 1.48  5.12  1.08
4  2.13 1.68  5.77  1.07
5  2.07 1.68  8.65  2.18
6  1.84 1.74  9.06  2.16
7  2.10 1.97  8.72  1.89
8  2.15 1.91  8.48  2.00
9  2.12 1.20  0.70  1.51
10 1.94 1.19  0.39  2.45
11 2.30 1.38  0.32  1.58
12 2.08 1.35  0.63  1.32
13 2.05 1.65  0.97  1.37
14 1.73 1.65  1.06  1.71
15 2.40 1.86  1.07  1.30
16 2.16 1.95  0.94  1.38
17 2.14 1.18 10.61  3.69
18 2.33 1.38 10.70  3.33
19 2.31 1.28 10.96  3.09
20 2.26 1.08 10.42  2.84
21 2.24 1.74 19.69  3.49
22 2.21 1.45 19.64  4.04
23 2.07 1.96 19.79  3.51
24 1.66 2.07 19.51  3.39
25 1.67 0.83  1.95  1.17
26 1.54 0.69  1.93  1.52
27 1.52 1.02  1.76  0.92
28 1.25 0.88  2.11  0.97
29 2.11 1.99  4.55  2.25
30 2.29 1.50  4.46  1.41
31 2.48 1.65  4.63  2.19
32 2.18 1.51  4.43  2.16

And here is reproducible code to create it in R:
dat = structure(list(a = c(1.68, 1.85, 1.64, 2.13, 2.07, 1.84, 2.1,
    2.15, 2.12, 1.94, 2.3, 2.08, 2.05, 1.73, 2.4, 2.16, 2.14, 2.33,
    2.31, 2.26, 2.24, 2.21, 2.07, 1.66, 1.67, 1.54, 1.52, 1.25, 2.11,
    2.29, 2.48, 2.18), b = c(1.84, 1.75, 1.48, 1.68, 1.68, 1.74,
    1.97, 1.91, 1.2, 1.19, 1.38, 1.35, 1.65, 1.65, 1.86, 1.95, 1.18,
    1.38, 1.28, 1.08, 1.74, 1.45, 1.96, 2.07, 0.83, 0.69, 1.02, 0.88,
    1.99, 1.5, 1.65, 1.51), c = c(5.33, 5.52, 5.12, 5.77, 8.65, 9.06,
    8.72, 8.48, 0.7, 0.39, 0.32, 0.63, 0.97, 1.06, 1.07, 0.94, 10.61,
    10.7, 10.96, 10.42, 19.69, 19.64, 19.79, 19.51, 1.95, 1.93, 1.76,
    2.11, 4.55, 4.46, 4.63, 4.43), value = c(1, 1.42, 1.08, 1.07,
    2.18, 2.16, 1.89, 2, 1.51, 2.45, 1.58, 1.32, 1.37, 1.71, 1.3,
    1.38, 3.69, 3.33, 3.09, 2.84, 3.49, 4.04, 3.51, 3.39, 1.17, 1.52,
    0.92, 0.97, 2.25, 1.41, 2.19, 2.16)), row.names = c(NA, -32L), class = "data.frame")

The variables a, b and c seem to be uncorrelated which I thought means it's OK to include them in the same model:
cor(dat[, c("a", "b", "c")])
##           a         b         c
## a 1.0000000 0.3323974 0.1891983
## b 0.3323974 1.0000000 0.3353348
## c 0.1891983 0.3353348 1.0000000

My goal is to evaluate the effects of a, b and c on value. According to lm, it seems like all three effects are significant:
fit = lm(value ~ a + b + c, dat)
summary(fit)
## Call:
## lm(formula = value ~ a + b + c, data = dat)
##
## Residuals:
##      Min       1Q   Median       3Q      Max
## -0.88732 -0.22240 -0.05573  0.12671  1.07648
##
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  0.08922    0.56288   0.159  0.87519
## a            1.01036    0.28126   3.592  0.00124 **
## b           -0.60997    0.23447  -2.601  0.01467 *
## c            0.12839    0.01361   9.431 3.45e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4333 on 28 degrees of freedom
## Multiple R-squared:  0.7995,    Adjusted R-squared:  0.7781
## F-statistic: 37.23 on 3 and 28 DF,  p-value: 6.611e-10

However, when evaluating the effect of b on its own, the effect is not significant:
fit = lm(value ~ b, dat)
summary(fit)
## Call:
## lm(formula = value ~ b, data = dat)
##
## Residuals:
##     Min      1Q  Median      3Q     Max
## -1.1638 -0.7137 -0.2697  0.6349  2.0208
##
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   1.4815     0.7027   2.108   0.0435 *
## b             0.3708     0.4510   0.822   0.4174
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9246 on 30 degrees of freedom
## Multiple R-squared:  0.02204,   Adjusted R-squared:  -0.01056
## F-statistic: 0.6761 on 1 and 30 DF,  p-value: 0.4174

A scatterplot of b vs. value also shows there is no association between the two:
plot(value ~ b, dat)
abline(lm(value ~ b, dat))

Moreover, the scatterplot suggests a positive effect (if any), while the model value~a+b+c suggests a (significant) negative effect of b!
My questions are:

What should be the conclusion regarding the effect of b on value?
What other tests or diagnostics should be done to investigate this type of situation?

Nota bene: This thread previously had a different title and was marked as a duplicate.  Those prior, similar questions focus on how this situation might arise. My question on the practical aspect of reporting the results. Namely, in the described situation, would it be correct to conclude that "b negatively affects value", and if so, what other diagnostics or tests (if any) is it necessary to report.
Comment: Hi, Michael. I think in your correlation coefficient matrix. 'b' is weakly correlated to 'a' and 'c'. It's not uncorrelated.
Comment: @Tbone Thanks, I used the correlation matrix mainly to conclude whether to include all three of the independent variables in the same analysis, using the rule of thumb that all possible pair have weak (<0.7) correlations.
Answer: Let me address some preliminaries first, before addressing the main question.  For reference, it will help to read these threads:

Significant predictors become non-significant in multiple logistic regression
How can adding a 2nd IV make the 1st IV significant?
Is there a difference between 'controlling for' and 'ignoring' other variables in multiple regression?
Estimating $b_1x_1+b_2x_2$ instead of $b_1x_1+b_2x_2+b_3x_3$
Intuition behind the names 'partial' and 'marginal' correlations
Basic Simpson's paradox

As those threads discuss, the reason a variable can move from non-significant to significant as more variables are included is that the additional variables are associated with the response and so reduce the error variance, increasing the power of the test of the focal variable.  To come to understand how this phenomenon is playing out in your data, you can use the method @whuber demonstrates in his answer to the second linked thread above.
Likewise, the reason the sign flips is because the variable is correlated with the other variables that are being added.  I gather seeing the sign flip was perplexing, because you had checked for correlations and found they were small to moderate (you thought they "seem to be uncorrelated").  Nonetheless, the phenomenon is due to those correlations.  It's important to recognize that those correlations, and the scatterplot of value x b, are marginal projections (see linked thread #5 above), and relationships can exist between more than two variables oriented in such a way within the full multidimensional space such that they cannot be seen in any of the 2D projections.  The best way to see this is to use visualizations that move beyond 'flatland' via conditioning, pseudo-3D representations, motion, or interactivity (e.g., brushing).  These can all be done conveniently in R (e.g., via ?coplot, ?lattice, ?scatterplot3d, ?rgl, or ?rggobi).  I'm not sure how easy they are in other software, though.  A simple hack is to examine a scatterplot matrix and use color, or different symbols, to distinguish different values of your focal variable.
cols = ifelse(dat$b<median(dat$b), "red", "blue")  # low b values are red, high blue
windows()
  pairs(dat[,c(4,1:3)], col=cols, pch=16)
windows()
  plot(value~c, dat, col=cols, pch=16)
  abline(a=1, b=.15, col="gray")

Now it is possible to see if a value for b is low or high.  The plots with b aren't the ones to look at, instead, look at how the colors play out in the plots that are collapsing over b.  The key plot is the plot of value vs c in the top right corner (row 1, column 4).  To get a better look, we can make a larger version of that scatterplot alone:

Here you can see that there is a strong correlation between value and c, with a band of points moving from lower left to upper right.  Running parallel within that band, we see stripes of mostly red points (lower values of b) along the band's top and mostly blue points (higher values of b) along the band's bottom (separated by a line I arbitrarily drew through the data).  As a result, the relationship between b and value, after controlling for c, is downward sloping.  That gets you your negative coefficient sign in the multiple regression model.

Having now attempted to establish the phenomena at play here, and to understand how they are occurring in these data, let's move to the main question:  What interpretations can be made, given what you've found?
When we see conflicting results (e.g., non-significant vs. significant, or positive vs negative) when modeled different ways (alone or controlling for covariates), or having made different judgment calls (e.g., which measurement of a construct, transformations, outliers, etc.), it is common to ask, 'which is right?'  There isn't necessarily an answer to that.  In many cases, transparency is the best course of action: report the analysis planned a-priori, but then discuss other analyses / results as 'sensitivity analyses'.
In this case, I gather the question is about causality ("the effect of b on value").  There are three things to bear in mind:

This appears to have been an observational study.  Causal inferences are valid primarily due to the design of the study.  That is, you can infer causality because the study is a true experiment due to randomization and independent manipulation of the treatment, or due to exploiting naturally occurring exogeneity.
These effects are due to the correlations amongst variables, and there will likewise be correlations with other variables that are not included in the study.  (Variable b is confounded with a and c, which are 'measured confounders', and is certainly confounded with any number of other variables that are unmeasured in this study.)
Each of these models / correlations is a marginal association, and each of the  plots is a marginal projection.

Point three is really important to understand.  It may help to read linked threads #s 5 and 3 above.  It is entirely possible that both models are correct: there is no (or just a very small positive) association with value when ignoring all other possible variables, and there is a clear negative association with value when ignoring all other variables except a and c (which are being controlled for).  If you were to gather data on additional variables and assess other marginal associations (controlling for d and e, controlling for a and d, for c and e, for all four covariates, etc.), you could get completely different answers and they could also be right.
I don't necessarily think other tests or diagnostics are needed.  The exploratory data visualizations listed above and in @whuber's linked answer can help you with understanding the results of the tests you've already run, though.
Ultimately, if you want to know if b causes value, you need to run a true experiment.  Find plots, and divide them randomly into subplots.  Independently manipulate the levels of the variables of interest (b, but possibly all three) and treat the subplots.  Then wait whatever period of time is appropriate in this context and assess the resulting values.  Good experimental design will make a, b, and c orthogonal, and randomization will make all background variables uncorrelated at the population level.  That will allow for valid causal inferences.
Comment: Thank you very much for the thorough answer, this is more then I could expect! The issue is much more clear to me now.


Add custom payment widget to a node?

Question: I have a widget that allows the user to book and pay on a site. I used:
function capewinefarms_preprocess(&$variables, $hook)

And ended up with something like this:
$variables['book_now_widget'] = '
bla bla bla
';

Which I then output in the template using:
{{ book_now_widget }}

The problem is that instead of seeing:
 bla bla bla

in bold text, i see:

bla bla bla

Because the widget is "self-contained", it needs all the javascript and html and everything to be outputted exactly as is, and not be formatted. It seems twig is somehow doing this.
Any idea as to how to get around this? Or any insight into why this is happening? Or perhaps an even better question might be: how do I do this the most correct way?
I suppose I can do it as a block, but then I would have to attached a JS file, and I can't do this, because it all needs to be self contained as it is also re-used on a mobile app.
Any ideas?
Answer: 1.) What is happening
Twig sanitizes all variables before displaying them. Because it assumes, that the style attribute is from user input and is considered to be unsafe. You can pass most html as #markup, but not the style attribute.
2.) You asked about the most correct way to do this is.
This would be to put all html markup in templates.
3.) How to get around this
If you don't want to build a custom twig template for this, you can use an inline template instead:
$variables['book_now_widget'] = [
        '#type' => 'inline_template',
        '#template' => '
{{ var1 }}
',
        '#context' => [
          'var1' => 'bla bla bla'],
];

Answer: This solved my problem:
{{ book_now_widget|raw }}

Comment: This is unsafe. It may work for you, if you know exactly what you are doing. But you shouldn't post this as an answer.

Add custom payment widget to a node?


Recursion Binary to Decimal

Question: I am using C++ to write a program that uses recursion to convert a user input binary number to a decimal. I've played with this code for hours
(earlier I initialized i with i = binary.length();)
    void bin2dec(string binary, int i)
{
    double decNum=0;

    if (i >= 0)
    {
        if (binary[i] = 0)
        {
            decNum = (decNum + 0);
        }
        else
        {
            decNum = (decNum + pow(2, (i-1)));
        }
        bin2dec(binary, i-1);
    }
    cout << decNum;
}

That is my recursion function. Unfortunately, I am stuck. The program runs but gives me incorrect values. For example, when I plug in 1 as the binary, I expect to get 1 decimal in return. But I get .5 number. Is my calculation wrong or am I using recursion incorrectly?
Upon receiving suggestions, I made the following changes. However, the program still returns an incorrect value.
void bin2dec(string binary, int i)
{
    double decNum=0;
    if (i >= 0)
    {
        if (binary[i] == 1)
        {
            decNum = (decNum + pow(2, i));
        }
        else if (binary[i] == 0)
        {
            decNum = (decNum + 0);
        }
        bin2dec(binary, i - 1);
        cout << decNum;
    }
}

Answer: Assuming you are using small endian, you should use pow(2, i).  With i-1, you are going to have 1 in position 0 in the array, which means you will evaluate pow(2, -1), which is 0.5.
Consider this working example (https://ideone.com/pWVAGP):
int bintodec(string binary, unsigned int i = 0)
{
    int tot = 0;
    if (i < binary.length())
    {
        if (binary[i] == '1')
            tot = pow(2, i);
        else if (binary[i] != '0')
            throw "String is not formatted in binary";
        return tot + bintodec(binary, ++i);
    }
    return tot;
}

Note that it's possible to start at the end of the string and work backwards, but I prefer starting at 0 since I think it is simpler.  To total up your additions, the easiest thing to do is return another call of the function, as I did at the end of the if(i < binary.length() block.  Once you have hit the base case (in this case where i == binary.length()), return a 0, which is added to the total number but doesn't change it.  Once the base case has returned, the others will begin returning their portion of the sum to the one above them, which keeps getting added back until it reaches the bottom of the call stack, which is the original calling function.
If you do not want to return the answer, you could change the function signature to void bintodec(int& tot, string binary, unsigned int i = 0) and keep adding the value to tot rather than returning it, but it requires that your caller gives you an int to modify.
Answer: A slightly improved version of the above algorithm; which does not use the pow() function is:
int btod_r(int n, int p = 0) {
    if(n == 0) return 0;
    if(p == 0) return n%10 + btod_r(n/10, 2);
    return  (n%10) * p +  btod_r(n/10, 2*p);
}

NOTE: the binary input here is an int, with a small modification you can re-write it to accept string instead.
Answer: class trip
{
    int convert2(int N, int i)
    {
        if(N!=0)
        {
            return (N%10)*((int)Math.pow(2,i))+convert2(N/10,i+1);
        }
        else
        {
            return 0;
        }
    }
}

Answer: Solution:
int bin2dec(long long num)
{
   if(num==0) return 0;
   return num%10+2*bin2dec(num/10);
}

Answer: int decimal(int no,int e=0){
    // if number become 0 then return 0
    if(no==0) return 0;

    int last = no%10; //Extracting the last digit of the binary number

    //Checking if the last digit is 1 or 0
    if(last==1){
        //Calculating the value of current bit
        int ex=pow(2,e);
             //adding current bit value to the value of remaining binary number
        return ex + decimal(no/10,e+1);
    } else{
             //Passing the remaining binary number to calculate the value
        return decimal(no/10,e+1);
    }
}

Comment: Isn't this basically the same as the other answers?
Comment: Please add an explanation to your solution, especially regarding how it differs from the other solutions added here. Read [answer] for more guidelines.

Recursion Binary to Decimal


Nginx proxy request to url specified in GET variable

Question: I need Nginx to respond to a request like
https://example.org/proxy/?url=https://othersite.com/path/file.php%%a=test123%%b=321tset

or a similar method, like
https://example.org/proxy/https://othersite.com/path/file.php?a=test123&b=321tset

by proxying the request to
https://othersite.com/path/file.php?a=test123&b=321tset

Is there a way to do this with rewriting or a different rule? Any help would be appreciated. Thank you in advance.
Comment: This is a very bad idea. You are sending open invitation to all malicious actors to use your server as a gateway to attack other websites. Don't do this without proper restrictions on proxy targets.
Comment: You don't know my situation. This is going to be on a local server.
Comment: In that case it is fine. Just wanted to warn in general about the dangers.
Answer: Welcome to SE. Those redirects can be done using if statements and I've not tried it using nginx proxy.
Add the following if blocks to your Nginx server block to redirect them as required
server {
    ...
    ...

    #For https://example.org/proxy/?url=https://othersite.com/path/file.php%%a=test123%%b=321tset

    if ($request_uri ~ ^/proxy/\?url=(.*)$) {
        return 301 $1;
    }

    #For https://example.org/proxy/https://othersite.com/path/file.php?a=test123&b=321tset

    if ($request_uri ~ ^/proxy/(https\:\/\/(.*))$) {
        return 301 $1;
    }
}

Update:
You can use proxy using location blocks instead of using if
location ~ /proxy/\?url=(.*)$ {
    proxy_pass $1;
    proxy_set_header Host $host;
}

location ~ /proxy/https\:\/\/(.*)$ {
    proxy_pass $1;
    proxy_set_header Host $host;
}


How do you load extra javascript files required for wysiwyg api plugin on node/edit pages?

Question: I'm building a plugin using the WYSIWYG API module for Drupal 7 that will add a button to the toolbar of supported editors.
I'm using the hook_wysiwyg_plugin() hook to create a new button on editor toolbar but I need to load a few other javascript files. The hook_wysiwyg_plugin() hook seems to allow you to specify one JS and CSS file.
How can I load extra required javascript files required for the plugin javascript?
I've implemented hook_library() to define a library i.e. js/css files but am unsure how I hook that up to wysiwyg api so that they load on the node/edit screens?
Comment: Voice your request here as well http://drupal.org/node/1039076
Answer: With drupal_add_js:
function yourmodule_preprocess_page(&$vars)
{
  $node = $vars['node'];
  if($node->type == 'yournodetype' AND arg(2) == 'edit')
  {
    drupal_add_js([...]);
  }
}

Comment: I really need it to be tied in with the WYSIWYG API module so that the additional files are only loaded when that module creates a WYSIWG area. Also I need to load a library defined by hook_library().
Answer: It's actually quite simple, although my experience with doing the same thing is through Drupal 6, so there may be some minor differences with my code:
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  // Only modify node_form's.
  // @TODO - Set a condition for all forms you wish to alter for D7 entities.
  if (isset($form['type']['#value']) && $form_id == "{$form['type']['#value']}_node_form") {
    // We use after build to add JS and CSS to prevent issues caused by form caching.
    $form['#after_build'][] = 'MYMODULE_form_alter_after_build';
  }
}

function MYMODULE_form_alter_after_build($form_element, &$form_state) {
  drupal_add_js(...);
}

Again, this code is for D6, but the basic principle should still work.
While it's not tied directly to the Wysiwyg API, it's probably the best option as the Wysiwyg API only supports on JS file per plugin (iirc).
A few wild/untested options for you:

Make a menu callback for the Javascript file defined in the plugin and pass back a somewhat cached javascript file containing the sources of multiple javascript files. (Wysiwyg Fields does do the menu callback for dynamically generated plugins, but only for one javascript file, see the source for ideas).
Use a Javascript dynamic Javascript loading technique similar to http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml

Hope this helps.
Comment: I've not tried it yet but it sounds like that will work. Thanks.
Answer: In theory, all you would need to do is use the right "#text_format" parameter on the #type textarea FAPI element for the wysiwyg editor associated with the Text Format to be automatically included, this way:
function mymodule_menu(){
    return array(
        'mypage' => array(
            'title' => 'A page to test wysiwyg',
            'page callback' => 'drupal_get_form',
            'page arguments' => array('mymodule_page'),
            'access arguments' => array('access content'),
        )
    );
}

function mymodule_page($form, &$form_state) {
    $element = array(
        '#type' => 'textarea',
        '#title' => t('Comment'),
        '#default_value' => 'something',
        '#text_format' => 'full_html',
        '#required' => TRUE,
    );
    $form['mytext'] = $element;
    return $form;
}

However, it doesn't get included unless it's a "node edit" page, so something related to functions "filter_process_format" or "wysiwyg_pre_render_text_format" may be able to force it, but I haven't found the magic combination yet, still reading the source of the Wysiwyg module to find the missing piece.


how to make to position a small image on top of a parent image in css

Question: I have a web page design (it is an image) , I embedded it in an html and to display it and the css code is like this

.imageMain {
  position: relative;
  height: 100vh;
  /* For 100% screen height */
  width: 100vw;
  /* For 100% screen width */
}

img#homeinactive {
  position: relative;
  top: -960px;
  left: 750px;
  width: 40px;
  height: 40px;
  z-index: 999;
}

Now I need to put a small image image on top of that image which will serve as a button that will
redirect a user somewhere. the problem is, only the parent image follows the screen size, and it actually stretches automatically no matter what the size of the screen because of my .imageMain css class.
the small image is being misplace when I change screen size, any idea what to put in the css to make it stay like in the middle top no matter what screen size it will be..take note, this small image is on top of main image..here's my css, it's wrong
here's the html
Comment: can you share your html?
Comment: please create a code snippet so we can help you.. thanks
Comment: Hi , I added the html code at the bottom
Comment: @sasori, You mean like this?? https://codepen.io/Maniraj_Murugan/pen/poJWwzB or like this? https://codepen.io/Maniraj_Murugan/pen/NWqagPE
Answer:

.container {
  position: relative
}
.imageMain {
    height: 100vh; /* For 100% screen height */
    width:  100vw; /* For 100% screen width */
    object-fit: cover;
}

img#homeinactive {
    position: absolute;
    left: 50%;
    top:0;
    transform: translateX(-50%);
    width: 40px;
    height: 40px;
}

Comment: how to auto adjust the "top" property ? ,
Comment: Same way like the left property, top:50% then you add transform: translate(-50%,-50%) instead of translateX
Answer: You can easily achieve this by using flex. Wrap your second image in a div and use justify content: center; and it will always be in center.
Here's an example:
https://jsfiddle.net/psparthsahni/50e9nuzh/
Comment: do i need to use bootstrap for that ?
Comment: No, you don't!!