How do we store a reference to an object in the pointer?

Question: I am thinking of this solution, but not sure if it is correct.
Type* myObjP;

void setSmth(Type& toBePassed) {
  myObjP = *toBePassed;
};

Comment: @Evg I wouldn't .. recommend that.
Comment: @Evg The context of the question is too small to suggest a solution that is guaranteed to be valid.
Comment: Store a reference in a pointer?? That makes no sense.
Answer: Syntactically speaking, you would need to use:
void setSmth(Type& toBePassed) {
  myObjP = &toBePassed; // Addressof operator rather than dereference operator.
}

Word of Caution
When using such a mechanism, you'll have to be very careful when you dereference myObjP. If the object passed to setSmth is not alive, dereferencing myObjP will cause undefined behavior.
There are more robust mechanisms in the language but it's hard to suggest anything without additional context.
Comment: @Chipster, got to respect the OP's feeling on that :)
Comment: Not sure how I became the accepted answer. I like yours better than my own.
Answer: toBePassed is a reference, not a pointer. So you actually need the & operator (address of operator), not the * operator (dereference operator):
myObjP = &toBePassed;

Note: You're not really storing a reference in a pointer. You're just storing a regular pointer to the object that is being referenced.
Answer: You can try it.
you should use & operator for pass address to pointer.
double* myObjP;

void setSmth(double& toBePassed)
{
    myObjP = &toBePassed;
};

int main()
{
    double testValue{8.0};
    setSmth(testValue);

    if (testValue == *myObjP)
    {
        cout << "We store a reference to an object in the pointer\n";
        cout << "testValue ==> " << testValue << "   " << "*myObjP ==> " << *myObjP<<std::endl;
    }

    std::getchar();

}


Speed up web a/b tests with sample size checkpoints

Question: Before starting an a/b test with a control and one experimental route, I can calculate a required sample size based on conversion rate estimates for both routes. I can get a good estimate of the conversion rate of the control by looking at historical data. But the conversion rate of the experimental route is unknown. What I would like to do is calculate a number of different sample sizes based on a variety of sensitivities.
For example, I can calculate sample sizes for a 10%, 15% and 20% sensitivity (increase in conversion from the control) that might look like this:
<code>Sensitivity   Required Sample Size
10%           1,961
15%           871
20%           490
</code>
Some of the reading I've done says that you should calculate a single sample size at the start of the test and always run the test for that long.
Question:

Is there any problem with checking for statistical significance at multiple pre-calculated sample sizes and potentially ending a test early if I've found the results to be statistically significant?

Example:
I originally estimate that the experimental route will outperform the control by 15%. But once I've reached 490 samples I find that the experimental route is actually outperforming the control by 20%, can I end the test and declare that the experimental route boosts conversion by 20%?
Comment: @sashkello the sample size for 20% sensitivity was calculated using a significance level of 95% and a power of 80%. So if a 20% effect is observed at 490 samples, can the test be ended and considered _credible_?
Comment: You can stop your test at any point (nothing wrong with that), but in this case your result is still 20+-20%, taking into account your sensitivity. So even the probability that there is no effect is high. I strongly suggest this read: http://visualwebsiteoptimizer.com/split-testing-blog/how-to-calculate-ab-test-sample-size/ and that blog in general.
Comment: @sashkello: Are you saying that a test stopped at 490 samples because one subject is shown to be outperforming the other by 20% somehow reports a value that is less credible than one that collected 1,961 samples and also showed the same 20% performance improvement?
Comment: @jkndrkn Of course, they will have different precision. What if you have test of 5 samples with 20% (one sample) improvement? It has no credibility whatsoever.
Comment: @sashkello: Assume the significance level of the experiment is 0.95 and the power is 0.8. Say the user of the test is comfortable stopping at 490 samples because the effect is shown to be 0.2. The user knows that with fewer samples the credibility of the test is reduced. Can a credibility score of some kind be assigned that accompanies a sample size? If so, which value could be used?
Comment: 'Credibility' *is* significance & power. Sample size is embedded into them.
Answer: This approach doesn't have the properties you would have if you fixed the sample size ahead of time.
The situation where you look for a particular result while your experiment continues and have some 'stopping rule' (halt your experiment early if a particular situation is achieved) is a version of sequential analysis; see also SPRT.
You have to take care that the properties of your actual decision rules are doing what you want - you can't apply the properties of one situation to another and expect that it will work.
For example, you won't have the power you have calculated at the given sample sizes if you're doing sequential testing; the required sample sizes will be somewhat larger. On the other hand, when your effects are substantial, you'll often end up stopping earlier - meaning smaller sample sizes/faster decisions.

Specifically which properties are affected if one were to terminate the test at, say, 490 samples because 20% improvement over the control is shown?

First, estimates will be biased, but also standard errors, Type I and (as already mentioned) Type II error rates are affected - plus anything any of these will impact.
The SPRT link I gave outlines a general approach that is used with early stopping with hypothesis testing.

Phillip Good does some work with discrete sequential analysis in his book
Permutation, Parametric, and Bootstrap Tests of Hypotheses in section 6.7
Comment: Specifically which properties are affected if one were to terminate the test at, say, 490 samples because 20% improvement over the control is shown?
Comment: See the edit for some details of the kinds of things affected.
Comment: Thanks, is there any way to quantify how bias, Type I, and Type II error rates are affected? It may be desirable to end a test early so long as these parameters don't suffer beyond some acceptable threshold.
Comment: @jkndrkn (My apologies for not @-notifying you before, I hadn't realized you weren't the OP until just now. ) ...
Yes, you can quantify the bias under a given set of assumptions via simulation, for example. But why would you choose to do something biased instead of actually using a proper sequential analysis in the first place?


nginx is slow in parsing php with html content

Question: I don't know why but nginx seems to be quite slow while the browser requests a php with html content ?? [ It's quite weird ]
And this is my php-fpm.ini :
;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;

; All relative paths in this configuration file are relative to PHP's install
; prefix (/usr). This prefix can be dynamicaly changed by using the
; '-p' argument from the command line.

; Include one or more files. If glob(3) exists, it is used to include a bunch of
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
; Relative path can also be used. They will be prefixed by:
;  - the global prefix if it's been set (-p arguement)
;  - /usr otherwise
;include=/etc/php/fpm.d/*.conf

;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;

[global]
; Pid file
; Note: the default prefix is /var
; Default Value: none
pid = run/php-fpm/php-fpm.pid

; Error log file
; Note: the default prefix is /var
; Default Value: log/php-fpm.log
;error_log = log/php-fpm.log

; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice
;log_level = notice

; If this number of child processes exit with SIGSEGV or SIGBUS within the time
; interval set by emergency_restart_interval then FPM will restart. A value
; of '0' means 'Off'.
; Default Value: 0
;emergency_restart_threshold = 0

; Interval of time used by emergency_restart_interval to determine when
; a graceful restart will be initiated.  This can be useful to work around
; accidental corruptions in an accelerator's shared memory.
; Available Units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
;emergency_restart_interval = 0

; Time limit for child processes to wait for a reaction on signals from master.
; Available units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
;process_control_timeout = 0

; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
; Default Value: yes
;daemonize = yes

;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;

; Multiple pools of child processes may be started with different listening
; ports and different management options.  The name of the pool will be
; used in logs and stats. There is no limitation on the number of pools which
; FPM can handle. Your system will tell you anyway :)

; Start a new pool named 'www'.
; the variable $pool can we used in any directive and will be replaced by the
; pool name ('www' here)
[www]

; Per pool prefix
; It only applies on the following directives:
; - 'slowlog'
; - 'listen' (unixsocket)
; - 'chroot'
; - 'chdir'
; - 'php_values'
; - 'php_admin_values'
; When not set, the global prefix (or /usr) applies instead.
; Note: This directive can also be relative to the global prefix.
; Default Value: none
;prefix = /path/to/pools/$pool

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock

; Set listen(2) backlog. A value of '-1' means unlimited.
; Default Value: 128 (-1 on FreeBSD and OpenBSD)
;listen.backlog = -1

; List of ipv4 addresses of FastCGI clients which are allowed to connect.
; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
; must be separated by a comma. If this value is left blank, connections will be
; accepted from any ip address.
; Default Value: any
;listen.allowed_clients = 127.0.0.1

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
;                 mode is set to 0666
listen.owner = http
listen.group = http
listen.mode = 0660

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
user = http
group = http

; Choose how the process manager will control the number of child processes.
; Possible Values:
;   static  - a fixed number (pm.max_children) of child processes;
;   dynamic - the number of child processes are set dynamically based on the
;             following directives:
;             pm.max_children      - the maximum number of children that can
;                                    be alive at the same time.
;             pm.start_servers     - the number of children created on startup.
;             pm.min_spare_servers - the minimum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is less than this
;                                    number then some children will be created.
;             pm.max_spare_servers - the maximum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is greater than this
;                                    number then some children will be killed.
; Note: This value is mandatory.
pm = dynamic

; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes to be created when pm is set to 'dynamic'.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI.
; Note: Used when pm is set to either 'static' or 'dynamic'
; Note: This value is mandatory.
pm.max_children = 50

; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = 20

; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.min_spare_servers = 5

; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.max_spare_servers = 35

; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
pm.max_requests = 500

; The URI to view the FPM status page. If this value is not set, no URI will be
; recognized as a status page. By default, the status page shows the following
; information:
;   accepted conn        - the number of request accepted by the pool;
;   pool                 - the name of the pool;
;   process manager      - static or dynamic;
;   idle processes       - the number of idle processes;
;   active processes     - the number of active processes;
;   total processes      - the number of idle + active processes.
;   max children reached - number of times, the process limit has been reached,
;                          when pm tries to start more children (works only for
;                          pm 'dynamic')
; The values of 'idle processes', 'active processes' and 'total processes' are
; updated each second. The value of 'accepted conn' is updated in real time.
; Example output:
;   accepted conn:        12073
;   pool:                 www
;   process manager:      static
;   idle processes:       35
;   active processes:     65
;   total processes:      100
;   max children reached: 1
; By default the status page output is formatted as text/plain. Passing either
; 'html' or 'json' as a query string will return the corresponding output
; syntax. Example:
;   http://www.foo.bar/status
;   http://www.foo.bar/status?json
;   http://www.foo.bar/status?html
; Note: The value must start with a leading slash (/). The value can be
;       anything, but it may not be a good idea to use the .php extension or it
;       may conflict with a real PHP file.
; Default Value: not set
;pm.status_path = /status

; The ping URI to call the monitoring page of FPM. If this value is not set, no
; URI will be recognized as a ping page. This could be used to test from outside
; that FPM is alive and responding, or to
; - create a graph of FPM availability (rrd or such);
; - remove a server from a group if it is not responding (load balancing);
; - trigger alerts for the operating team (24/7).
; Note: The value must start with a leading slash (/). The value can be
;       anything, but it may not be a good idea to use the .php extension or it
;       may conflict with a real PHP file.
; Default Value: not set
;ping.path = /ping

; This directive may be used to customize the response of a ping request. The
; response is formatted as text/plain with a 200 response code.
; Default Value: pong
;ping.response = pong

; The timeout for serving a single request after which the worker process will
; be killed. This option should be used when the 'max_execution_time' ini option
; does not stop script execution for some reason. A value of '0' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_terminate_timeout = 0

; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_slowlog_timeout = 0

; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
;slowlog = log/$pool.log.slow

; Set open file descriptor rlimit.
; Default Value: system defined value
;rlimit_files = 1024

; Set max core size rlimit.
; Possible Values: 'unlimited' or an integer greater or equal to 0
; Default Value: system defined value
;rlimit_core = 0

; Chroot to this directory at the start. This value must be defined as an
; absolute path. When this value is not set, chroot is not used.
; Note: you can prefix with '$prefix' to chroot to the pool prefix or one
; of its subdirectories. If the pool prefix is not set, the global prefix
; will be used instead.
; Note: chrooting is a great security feature and should be used whenever
;       possible. However, all PHP paths will be relative to the chroot
;       (error_log, sessions.save_path, ...).
; Default Value: not set
;chroot =

; Chdir to this directory at the start.
; Note: relative path can be used.
; Default Value: current directory or / when chroot
;chdir = /srv/http

; Redirect worker stdout and stderr into main error log. If not set, stdout and
; stderr will be redirected to /dev/null according to FastCGI specs.
; Note: on highloaded environement, this can cause some delay in the page
; process time (several ms).
; Default Value: no
;catch_workers_output = yes

; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
; the current environment.
; Default Value: clean env
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp

; Additional php.ini defines, specific to this pool of workers. These settings
; overwrite the values previously defined in the php.ini. The directives are the
; same as the PHP SAPI:
;   php_value/php_flag             - you can set classic ini defines which can
;                                    be overwritten from PHP call 'ini_set'.
;   php_admin_value/php_admin_flag - these directives won't be overwritten by
;                                     PHP call 'ini_set'
; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no.

; Defining 'extension' will load the corresponding shared extension from
; extension_dir. Defining 'disable_functions' or 'disable_classes' will not
; overwrite previously defined php.ini values, but will append the new value
; instead.

; Note: path INI options can be relative and will be expanded with the prefix
; (pool, global or /usr)

; Default Value: nothing is defined by default except the values in php.ini and
;                specified at startup with the -d argument
;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f firstname.lastname@example.com
;php_flag[display_errors] = off
;php_admin_value[error_log] = /var/log/fpm-php.www.log
;php_admin_flag[log_errors] = on
;php_admin_value[memory_limit] = 32M

Thanks for reading this :)
Comment: Nginx neither runs nor parses PHP.
Comment: You should run with request_slowlog_timeout and slowlog set in your php-fpm config to see where your slowdown is. Looks like your are mostly running with default values.
Comment: How long is the php taking to execute? Slow php is usually slow php.
Comment: Thanks, maybe this is php-fpm problem ?
Answer: Thanks for all answers. I've solved the problem myself.
When I use ln -s to link my code folder from a ntfs partition to /srv/http, so it really slows down PHP compiler. When I put the real code to /srv/http , it works damn fast :)


How to translate a "formal" definition of the distributive property?

Question: I have seen sometimes a formal representation of the distributive property over an operator defined as follows:

A function $f$ is said to distribute over the operator $\bigoplus$ if
there is an operator $\bigotimes$ such that  $f(x\bigoplus y)=f(x)\bigotimes f(y)$

When we see a definition like this, how do we "translate" it to a more concrete example?
E.g. multiplication distributes through addition and hence:
$x\cdot (y + z) = x \cdot y + x \cdot z$
So in this case what is the $f$ what is the $\bigotimes$ and what is the $\bigoplus$?
Comment: Where did you see this definition?
Comment: Why the downvote?
Answer: Let $f(t):=x\cdot t$, $\oplus:=+$, $\otimes:=+$.
Then $f(y\oplus z)=f(y)\otimes f(z)$.
Edit
Although I have answered the question as set, I think that the definition given isn't quite the one I'd expect to see. It makes sense, but forces us to treat the $x$'s individually. To get something more uniform I would have preferred a definition like this:

The function $\mu: (x,y)\mapsto \mu(x,y)$ is said to right-distribute over the binary $\alpha:(x,y)\mapsto\alpha(x,y) $ if there exists a  binary $\alpha':(x,y)\mapsto\alpha'(x,y) $ such that
$$
\mu(x,\alpha(y,z))=\alpha'(\mu(x,y),\mu(x,z)).
$$

Then in the example we'd take multiplication for $\mu$, and addition for each of  $\alpha, \alpha'$.
Comment: @Jim in the definition you gave, $\oplus$ and $\otimes$ can be whatever operations you want. There's nothing in the definition that says they can't both be the addition operation on the real numbers, for example.
Comment: JIm, $f$ is as I said, left-multiplying by $x$.
Comment: Both are +. Where's the distributivity?
Comment: @ancientmathematician: I don't understand the answer. My question was specifically how it is translated into the distribution of multiplication over addition
Comment: @littleO: Then what is $f$?
Comment: As @littleO has said. In words "Left multiplication by $x$ distributes over $+$ because there exists an operation (also $+$) such that left multiplying the sum of $y,z$ by $x$ equals the sum of the results of left-multiplying $y$ by $x$ and $z$ by $x$."
Answer: Consider the $\exp$ function, and the ordinary addition and multiplication/. You have
$$\exp(x+y)=\exp(x)\cdot\exp(y).$$
Comment: I was interested specifically in the distribution of multiplication over addition


Function subtyping and Scala - confused

Question: So I'm getting a little confused with function subtyping.  I have the below that I'm messing around with.  I know that for contravariance, that A->Int is a subtype of B->Int which is why I was able to assign AToInt to "testBContra".
But what I thought I could do is pass an A object to "testBContra"...
testBContra(a)
which doesn't work.  Am I misunderstanding what can be passed in?  I thought that I could now pass an A object and have it return an Int.
class S(x: Int) {
  val sInput = x
}

class A(x: Int) extends S(x){
  val aInput = x
}

class B(x: Int) extends A(x){
  val bInput = x
}

def AToInt(h: A): Int = h.aInput

// Checking contravariance

// val testSContra: S=>Int = AtoInt
val testAContra: A=>Int = AToInt
val testBContra: B=>Int = AToInt

val a = new A(2)
val b = new B(5)
val s = new S(10)

testAContra(b)
testBContra(b)

Answer: testBContra has type B => Int, but since A is not a subtype of B you can't evaluate testBContra(a). The contravariance of function types is used when you assign a function A => Int to a value with type B => Int as you do in
val testBContra: B => Int = AToInt

The Function1 trait is contravariant in its parameter type and covariant in its result type. Contravariance means that if A is a supertype of B then a function A => T is a subtype of a function B => T for some result type T. Contravariance reverses direction of the super/sub-typing relationship. This means that if you have a function:
def foo(f: B => Int)

you can call it directly with AtoInt because A => Int is a subtype of B => Int i.e.
foo(AToInt)

without the contravariance of function parameters you would have to wrap AToInt in a function with an argument type of B i.e.
foo((b: B) => AToInt(b))

The covariance of function return types means that if B is a subtype of A then a function T => B is a subtype of T => A for some input type T. In your example, since Int is a subtype of AnyVal and Any, AToInt is a subtype of A => AnyVal and A => Any. So if you had a function
def bar(f: A => AnyVal) = f(new A())

then you can call bar(AToInt).
The two kinds of variance can be combined, so for example AToInt is also a subtype of B => AnyVal since A is a supertype of B and Int is a subtype of AnyVal.
Comment: @cpd1 - You already are assigning a function `A => Int` to one with type `B => Int` when you assign `testBContra`. The difference between `testBContra` and `AToInt` is that you are narrowing the parameter type from `A` to one of its subtypes `B`. That is the reason you can't pass an `A` to `testBContra` even though you can to the underlying method. This isn't that useful on its own but is if you have some function `foo(f: B => Int)` you need to call when you already have a function `A => Int` as you do here. Then you can do `foo(AToInt)` instead of `foo(b: B => AToInt(b))`
Comment: Also it helps to look at what `testBContra` could do. Say it was `val testBContra: B => Int = _.bInput`. Then obviously the type system weren't sound if it allowed you to pass a value `a` of type `A` to it, because `a` does not have method `bInput`.
Comment: Thank you guys.  Sorry if I'm being dense about this.  It could be that I just wrote a poor example to work with but how then could I pass something to testBContra that would be of A => Int?  The function expects an object so when I try to pass it a function it returns an error
Comment: @cpd1 - Why are you assigning a function `A => Int` to a variable of type `B => Int` and then trying to call it with an instance of `A`? Can't you just call it directly? Could you add more detail to your question to show why you need to do this?
Comment: I am a little confused with this stuff so sorry if I'm not being clear but I just want to prove to myself that I can pass something of A=>Int to something of B=>Int.  I thought I could pass the object A but I realize that  I was just testing class subtyping instead of function subtyping.  It just seems to me that I haven't done much that will make testBContra work any differently after assigning the type A=>Int to the function.
Comment: Thanks for following up!  So foo is actually illustrating function subtyping right?  So `foo(AToInt)` works because `A => Int` is actually a subtype of `B=>Int`?  I then tried to set up another function `def foo2(f: S=>Int): Int = 1` and then `foo2(AToInt)` but this won't work because `A=>Int` is not a subtype to `S=>Int`?
Comment: Thanks!  I just tried it with covariance as well and that seems to confirm what I was thinking.  This helps me a bit so thank you
Comment: @cpd1 - It's not `foo` itself which illustrates function subtyping but rather calling `foo` with a function `A => Int` which is a subtype of `B => Int`. I've added a more detail explanation.

Function subtyping and Scala - confused


Checkout form not getting saved

Question: I'm trying to develop an e-commerce site with Django. So I'm at this point where, users can add items to their cart, proceed to checkout but for some reason, my checkout form is not being saved.
I made sure that I have registered my models, and ran migrations, but every time I fill out my form and go to check in my admin panel, it says: 0 user addresses. What is the problem?
My views.py:
@login_required()
def checkout(request):
    address_form = UserAddressForm(request.POST or None)
    if address_form.is_valid():
        new_address = address_form.save(commit= False)
        new_address.user = request.user
        new_address.save()
        return HttpResponseRedirect('/')
    else:
        raise Http404
    context = {"address_form": address_form}
    template = "orders/checkout.html"
    return render(request, template, context)

My checkout.html:
<form method="POST" action="{% url 'ajax_add_user_address' %}?redirect=checkout">
    {% csrf_token %}
    <fieldset class="form-group">
        {{ address_form|crispy }}
    </fieldset>

        <input type="submit" class="btn btn-outline-dark" value="Place Order"/>

</form>

My urls.py:
from orders import views as orders_views
path('checkout/', orders_views.checkout, name='checkout'),

Comment: Not entirely related to your question, but you should always do a redirect after a form submission. In other words, you should have a `redirect(...)` within your `if` block.
Comment: Does `address_form.is_valid()` return True? Please check.
Comment: Okay I am adding them(edit)
Comment: Yea now it says Http4040
Comment: But why is my form not valid?
Comment: print form.errors
Comment: It says: Not Found: /checkout/
Comment: Looks like you need to go back to basics. Learn about forms here: https://tutorial.djangogirls.org/en/django_forms/. There's an example of a complete view that uses a form near the end of the article.
Comment: Essentially this can be marked as a duplicate of your next question: [Django checkout not accessible: Page not found (404)](https://stackoverflow.com/questions/61339977/django-checkout-not-accessible-page-not-found-404). In truth that one was a wilful duplicate of this one, but I am reversing the duplicate linkage, as that one has an accepted answer.
Comment: This question appears in a sequence of 11 questions from the same author on the same, or nearly the same topic. Many of these are copy-paste duplicate of each other, and will serve to waste the time of helpful readers who have not checked how many times the question has been re-asked.
Answer: First of all, the login_required decorator is not a function to be called. So, remove the parentheses first, then try to re-run your code. Also, the request.user is an object, not a string or any other variable data type. So, if you want your user's username you should write request.user.username.
Furthermore, try to add two different function views or one class-based view, to handle separately the GET and the POST request. The GET request will render the form to the user and will render the template. The POST request will redirect the user back to the homepage if the form submitted successfully. Last but not least, remember that a POST request does not have query parameters. So, you cannot use ?redirect=checkout in your URL.
Update
If you do not want URL consolidation, then your urls.py file should be something like this:
from [project_name].orders import views as orders_views

url_patterns = [
    path('checkout/', orders_views.checkout, name='checkout'),
]

Reference: https://www.webforefront.com/django/consolidatedjangourls.html
Comment: But my form.errors is still saying : Not Found: /checkout/
Comment: Try to post your `views.py` file, because if it cannot find a URL that is probably due to an error in the `views.py` or `urls.py` file.
Comment: I have posted my views.py file
Comment: I have also posted my urls.py @georgekrax
Comment: I cannot even render my checkout form and this form is not invoked from AJAX
Comment: Did it solve your problem?

Checkout form not getting saved


Force IE9 into browser compatibility view

Question: I am aware of how to force IE into viewing a page in document compatibility, but what about browser compatibility view?
I have a project that only works when IE9 is in browser compatibility view for various reasons, yet I can't find any resources on how to force IE9 to do so.
Everyone only talks about forcing the document compatibility, e.g. using
<meta http-equiv="X-UA-Compatible" content="IE=8" />

But that doesn't work in my case, because it only sets the document view. Is there a way to set the browser view, too?
PS: To make it abundantly clear, this is the setting I need to enforce:
Answer: See MSDN  documentation.
You should use emulateIE7 since compatibility mode is kind of IE7:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">

Edit
If the above does not work, the other options are really poor.
Quoting from the link above:

Microsoft has not placed the domain name of the site on a blacklist. (blacklist linked in original article is very old and not being updated, you can find a local copy on your computer)
An intranet admin has not placed the site on a blacklist.
The user has not pressed the Compatibility View button (duh)
The site is not in the intranet zone
The page is not framed by a Compatibility Mode page.

The above are, AFAIK, the only causes for compatibility mode.
Unless you are prepared to contact Microsoft directly to have your website added to the global blacklist, there isn't much you can do (unless you want to wrap your site in a frame that originates from a blacklisted domain).
Comment: I tried all of the document compatability modes, but that has no impact on the issues the page has. It actually only works when the browser mode is explicitly set to compatability view.
Comment: @FlorianPeschka I added all the known factors triggering compatibility mode: they are definitely not easily attainable and I don't think anybody would actually want to use them on purpose, but they do definitely work.
Comment: Okay. I hoped I could avoid having to debug the application, but in that case, I guess that's the only choice. Thanks for the effort though.
Comment: The MSDN documentation on that page has changed. http://msdn.microsoft.com/en-us/library/jj676915(v=vs.85).aspx May be more applicable now.
Answer: I've tried the various methods suggested by the MSDN article at http://msdn.microsoft.com/en-us/library/jj676915(v=vs.85).aspx regarding forcing IE9 to emulate a previous version with the X-UA-Compatible header in order to display correctly.
In my case, the test is playing an MP4 video file in an IE9 browser which is using the default standard mode rather than the compatibility mode.  Other websites have said that Internet Explorer is only configured to play MP4 files (rather than ogg and webm video files), so I assumed that would be the safest file format to test.
I had tested the view of the webpage using IE9 emulating the IE8 standard and the IE7 standard views of that page, and in both cases, the old standard views worked for playing the MP4 video file.  And the IE9 compatibility mode also played that web page.  But I forced the IE9 to revert back to the standard view in order to test a code that would work for playing the video for those who had not configured their own IE9 browser to use compatibility mode.
The methods suggested by that MSDN article referenced earlier seem to be directed towards this issue, but in testing each of those methods, none of them fixed the problem of an IE9 browser playing an MP4 video file.
The solution to my problem came from this Stack Overflow ticket:  HTML5 Video not working only in IE9
I actually had to add the following lines to the .htaccess file on the website:
AddType video/ogg .ogv
AddType video/mp4 .mp4
AddType video/mp4 .mov
AddType video/webm .webm

Then, I also needed to make sure that the first line of code used this:
<!DOCTYPE html>

... rather than this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

I did not need to use any of the "previous version" code such as this:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8">

On my test page, I also used the video-js code, which I had assumed would take care of the IE9 standard problem (as it had for other web browsers).  Its own code seems to have also output the following X-UA-Compatible codes:
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />

However, these codes alone did not solve the problem.  Ultimately, I believe it was due to the addition of the code to the .htaccess file.
Answer: I have one solution for all (7,8,9) ie Browsers

use this java script in head tag.
Comment: These links may not remain valid, as Google Code - especially direct links therein - is not guaranteed to remain usable by usual methods after January of 2016, as per its 'retirement plan' detailed here: https://code.google.com/p/support/wiki/ReadOnlyTransition


Hierarchical data structure in firebase with geospatial/geohash searching; am I doing this right?

Question: Intent:
Create a catalog of aerial photography. Records to be created by AngularJS forms, but retrieved by a map viewer like leaflet.
Goals:
Retrieve information multiple ways:  by Flight, Collection, Image by ID, and Image by Geography
Data Structure:
<code>Collection: {
    meta: '',
    Flights: {
        meta: '',
        Aerials: {
            meta:'',
            geopoint: [lat, lon],
            geopoly: [[x1,y1], [x2,y2], [x3,y3], [x4,y4]]
        }
    }
}
</code>
Firebase (attempt at denormalizing) Structure:

/Collections/CID
/Collections_Flights/CID
/Collections_Images/CID
/Flights/FID
/Images/IID

(I referenced this stackoverflow)
Questions:

If there are 1 Million Images does the denormalization look adequate? (each flight will have about 80 images, each collection will average 100 flights... if it matters)
Should I use a GeoHash?, if so does the GeoHash become the "Image ID (IID)" for the firebase reference /Images/UID? (Or should I make another reference ex: /Images_Geo/)
Must I use a GeoHash like this example? (On most mapping servers I can pass in a bounding box of the user's current view and the server will return all the items in that location.  Not sure how to go about this using Firebase.)
Answer: If Collection_Flights and Collections_Images only contain ids, and if you always retrieve them at the same time you fetch Collections/$CID, then you may not need to denormalize those indexes.
The main purpose of denormalizing here would be to make it faster to fetch a list of Collections or to grab Collections/$CID without having to wait for images and flight lists to load. So again, if those are always used in parallel, probably additional complexity for no gain.
The split on Flights/ and Images/, referenced from an index, is an excellent choice.
One million images of 10k each would be about 10GB of data; an important consideration. Assuming the images aren't changing at real-time speeds, you'd probably want to optimize by finding an extremely cheap storage facility (S3, CDN, etc) for those images and just storing the URLs in Firebase, rather than storing them as data and paying real-time rates for bandwidth and storage of these bulky static assets.
Whether you should use GeoHash is a huge topic and quite debatable; there are plenty of alternatives as you've already pointed out. I don't think anyone can tell you the answer to that huge implementation decision in a Q&A format (maybe as a chapter in a textbook or a discussion thread on an appropriate mailing list).
Comment: Kato, thanks a lot!  Collections would be retrieved as a list so I will keep them denormalized but your explanation/way of thinking helps me properly conceptualize the database design/structure.
Comment: Regarding the geoHash, I am leaning towards using ArcGIS server or GeoServer to serve up a some GeoJSON that contains a thumbnail url and a firebase UID. Though I will do some more reading before implementation.  Thanks again for your answer/time.


connecting slick 3.1.1 to the database

Question: I have the following code and I'm trying to connect to the MySQL database without success.
cat Database.scala
package com.github.odnanref.EmailFilter

import slick.driver.MySQLDriver._
import slick.driver.MySQLDriver.backend.Database

/**
  * Created by andref on 12/05/16.
  */
class Database {
  val url = "jdbc:mysql://localhost/playdb"
  val db = Database.forURL(url, driver = "com.mysql.jdbc.Driver")

  override def finalize() {
    db.close()

    super.finalize()
  }
}

cat EmailMessageTable.scala
package com.github.odnanref.EmailFilter

import java.sql.Timestamp

import slick.driver.JdbcProfile
import slick.driver.MySQLDriver.api._

import scala.concurrent.Future

class EmailMessageTable(tag: Tag) extends Table[EmailMessage](tag, "email_message") {

  def id      = column[Option[Long]]("id", O.AutoInc, O.PrimaryKey)
  def email   = column[String]("email")
  def subject = column[String]("subject")
  def body    = column[String]("body")
  def datain  = column[Timestamp]("datain")
  def email_id= column[Long]("email_id")

  def * = (id, email, subject, body, datain, email_id) <> ((EmailMessage.apply _).tupled, EmailMessage.unapply)
  def ? = (id.get.?, email.?, subject.?, body.?, datain.?).shaped.<>({ r =>; _1.map(_ =>
    EmailMessage.tupled((_1, _2.get, _3.get, _4.get, _5.get))) }, (_: Any) =>
    throw new Exception("Inserting into ? projection not supported."))
}

I can't initialize the database and execute search query's or insert statements based on this code I try to do
val db = new Database()
db.db.run(TableQuery[EmailMessageTable] += EmailMessage(...) )
And it says, it doesn't know the method +=

Also I get this error:
Database.scala:4: imported `Database' is permanently hidden by definition of class Database in package EmailFilter
[warn] import slick.driver.MySQLDriver.backend.Database

What am I doing wrong?
Post EDIT>
package com.github.odnanref.EmailFilter

import java.sql.Timestamp

case class EmailMessage(
  id: Option[Long],
  email: String,
  subject:String,
  body:String,
  datain: Timestamp,
  email_id: Long
)

Answer: You are importing a class named Database inside a file that defines another class with the same name. You can:

rename your Database class:
class MyDatabase {
 val url = ...
 val db = ...
 ...
}

rename imported class:
import slick.driver.MySQLDriver.backend.{Database => SlickDB}
...
val db = SlickDB.forURL(url, driver = "com.mysql.jdbc.Driver")

avoid importing Database explicitly:
import slick.driver.MySQLDriver.backend
...
val db = backend.Database.forURL(url, driver = "com.mysql.jdbc.Driver")

Comment: Awsome, that solved an issue, what about the +=
value += is not a member of slick.lifted.TableQuery[com.github.odnanref.EmailFilter.EmailMessageTable]
Comment: Could you edit your question and post your `EmailMessage` case class definition?
Comment: It looks like the file that contains `db.run` invocation is missing an import:
`import slick.driver.MySQLDriver.api._`
Comment: Thanks, that was it. Forgot that I was calling the run method outside the other object.

connecting slick 3.1.1 to the database


Melhor forma de dar binding em objetos de 1 para N - asp.net MVC

Question: Gostaria de saber qual a melhor forma de dar o Binding de dados no objeto FuncaoGrupo do meu Controller. Cada item da lista Funcoes irá gerar 4 Checkbox na View.
Tenho a seguinte classe:
public class GrupoDeUsuario
{
    public GrupoDeUsuario()
    {
        Funcoes = new HashSet<FuncaoGrupo>();
    }

    [Key]
    public int GrupoDeUsuarioId { get; set; }

    [MaxLength(length: 100, ErrorMessage = "Descrição do grupo deve ser somente 100 caracteres")]
    [Required(ErrorMessage = "Descrição do Grupo de Usuário deve ser informada")]
    public string Descricao { get; set; }

    public ICollection<FuncaoGrupo> Funcoes { get; set; }

}

Obs: Consegui fazer mas foi utilizando FormCollection, porém gostaria de saber se existe uma maneira melhor ou correta de se fazer isso.
Answer: A melhor forma é passando o grupo de usuário novamente para a Action POST:
[HttpPost]
public ActionResult Salvar(GrupoDeUsuario grupoDeUsuario)
{
    /* Lógica de negócios aqui */
}

Possivelmente seu problema de binding está relacionado ao fato de que o ModelBinder não envia os valores de cada função ligada ao grupo de usuário de volta para o Controller. Pra que isso aconteça, é preciso ter duas coisas no seu form:

Cada registro de função precisa ser identificado com um índice;
Os campos de cada função devem ser indexados pelo valor contido neste campo de índice.

Em resumo, o índice de uma função é um <input type="hidden"> em qualquer lugar do formulário cujo name é Funcoes.index e valor um inteiro ou Guid. Para simplificar, eu vou usar inteiros:
<input type="hidden" name="Funcoes.index" value="1" />

Agora, os campos checkbox. Você não disse seus nomes, então vou chamá-los de Campo1, Campo2, Campo3, Campo4. Ficaria assim:
<input type="hidden" name="Funcoes.index" id="Funcoes_index" value="1" />
<input type="checkbox" name="Funcoes[1].Campo1" id="Funcoes_1_Campo1" value />
<input type="checkbox" name="Funcoes[1].Campo2" id="Funcoes_1_Campo2" value />
<input type="checkbox" name="Funcoes[1].Campo3" id="Funcoes_1_Campo3" value />
<input type="checkbox" name="Funcoes[1].Campo4" id="Funcoes_1_Campo4" value />

Apenas isto já é suficiente para que a Action reconheça os valores ao enviar o formulário. Só que isto é um tanto prolixo de fazer, então é melhor usar uma ferramenta pra isso. No caso, o nome dela é BeginCollectionItem. Já respondi várias e várias vezes sobre ela. O que ela faz é gerar este campo de índice e indexar os campos pra você, mas ela funciona bem usando Razor. Ficaria assim:
@foreach (var funcao in Model.Funcoes)
{
    using (Html.BeginCollectionItem("Funcoes"))
    {
        @Html.CheckBoxFor(_ => funcao.Campo1)
        @Html.CheckBoxFor(_ => funcao.Campo2)
        @Html.CheckBoxFor(_ => funcao.Campo3)
        @Html.CheckBoxFor(_ => funcao.Campo4)
    }
}

Answer: Você pode usar um EditorFor para sua coleção de Funcoes, sem ter que utilizar o FormCollection. Você consegue fazer isso com "for", com foreach nao vai funcionar, já aviso. Sua View seria mais ou menos assim:
@model SeuProjeto.GrupoDeUsuario

@Html.EditorFor(model => model.Descricao)

@for(int i = 0; i < model.Funcoes.Count; i++)
{
  @Html.EditorFor(model => model.Funcoes[i].DescricaoFuncao)
}

Seu controller seria mais ou menos assim:
public ActionResult Index()
{
   GrupoDeUsuario grupo = dbContext.GrupoDeUsuario.Include(g => g.Funcoes).FirstOrDefault();

   return View(grupo);
}

[HttpPost]
public ActionResult Index(GrupoDeUsuario grupo)
{
  // aqui voce tem o grupo, com suas funcoes preenchidas, sem usar form collection
}


Get the RadioButton Value on click using jquery and disabling, enabling text boxes accordingly, Asp.net mvc

Question: I have two radio button
input  type = radio name = "Level" value = "P"
input  type = radio name = "Level" value = "S"
I have two textboxes:
input type = "text" name = "inpu11" disabled = "disabled"
input type = "text" name = "inpu12" disabled = "disabled"
My question is,
How Can I identify to which button I've selected ?
How can I retrieve the selected radio Id ?
I need to enable  the first textbox if the selected value is "P" and disabled if 'S'
Comment: Are you looking to do this with javascript on the client side or with .net on the server side? I see you mention jquery in the title but also asp.net so just want to make sure I understand.
Answer: You asked 3 questions. I'll answer the third question first. You can do this with Javascript/JQuery fairly easily.
View.aspx:
<form action='/MyController/MyAction' method='post'>

        <input type='radio' name='selectRadios' value='P' />
        <input type='text' name='firstTextBox' value='P' />

        <input type='radio' name='selectRadios' value='S' />
        <input type='text' name='secondTextBox' value='S' />

    <input type='submit' value='Submit Values to ActionMethod' />
</form>

I think your first and second questions will get answered pretty easily by MVC's modelbinder.
MyController.cs (action method only):
//argument names match form element names from view.aspx
public ActionResult MyAction(string selectRadios, string firstTextBox, string secondTextBox)
{
    //do something with your form values
}


Finding average using central limit theorem.

Question: I'm have a lot of trouble trying to wrap my head around how exactly to use the central limit theorem in this question.
Image a gambling casino consisting of 100 busy roulette tables.  Suppose that each table brings in an
average hourly profit of $50 with a standard deviation of 25.
(a)  What  is  the  average  hourly  profit  that  the  entire  casino  brings  in?   What  is  the  standard
deviation?
(b)  Find the probability that the casino will make a profit less than $4500 on any given hour.
I know in this situation I am supposed to CLT to figure out the average profit, but I don't understand how to use the CLT on a continuous case like this.
Answer: So we have a hundred tables $X_1, \dots, X_{100}$. Because we have more than lets say $30$ tables, or stochastic processes, we can use the CLT and we know that $\frac{1}{100}(X_1 + \dots + X_{100}) \sim N(50,\frac{25^2}{100})$. All these tables, or $X_i$'s are stochastic with $E(X_i) = 50$ and $V(X_i) = 25^2$.
To calculate the average hourly profit of the entire casino we have to look at $E(X_1 + \dots + X_{100}) = E(X_1) + \dots + E(X_{100}) = 100*50 = 5000$. Because the operator $E$ is linear. For the standard deviation of the whole casino we'd have to look at
$$
V(X_1 +  \dots + X_{100}) = V(X_1) + \dots + V(X_{100
})
$$
Since each table is independent of the other tables. So we get:
$$
V(X_1) + \dots + V(X_{100}) = 100*25^2
$$
So the standard deviation is $10*25 = 250$.
So now we know $X_1 + \dots + X_{100} \sim N(5000,250)$.
For part b) you have to calculate $P(X_1 + \dots + X_{100} \leq 4500).$ Now try to use $X_1 + \dots + X_{100} \sim N(5000,250)$ to solve this.
Answer: Hint: By the CLT, the distribution of the profit for whole casino follows a normal law of mean $5000\$/$hour and standard deviation $250\$/$hour. $4500$ is $2\sigma$ from the mean.


Show @class field in SQL result set

Question: When using OrientDB console, i want to show the @class field in the SQL result set (especially when working with the traverse command). How do i do this? Could not find anything in the documentation. When using the Java API this is possible.
Answer: i've tried with 1.7.x and yes is is not printed @class
With 2.x it is there
Which version of OrientDB do you have?
If you want to display @class you can also do something like this
select *, @class as class from V
Answer: This was fixed 9 days ago and is now in the latest release https://github.com/orientechnologies/orientdb/issues/2881

Show @class field in SQL result set


How to convert time to percentage? (Description inside)

Question: I'm doing one daily working shedule project windows phone. In that initially I'll set the maximum hour or minutes to a variable, assume i.e., 100%. if the time decreases then the percentage should also decrease.
for example assume we give 10hours for 100%, if the hours reduced to 5 the percentage should be 50%.
somebody tell me how to do this.
Comment: You described the process well, what's the problem? Just implement it.
Comment: If you have problems of this level, you should really consider taking a math class. I mean, a 5th grade elementary school math class. There is no such thing as too late. BTW the answer for `somebody tell me how to do this` is: start coding, and don't stop till its done!
Comment: Can you show what you have tried?
Comment: -1 for this is very basic math. [Hint](http://en.wikipedia.org/wiki/Rule_of_three_%28mathematics%29#Rule_of_Three)
Answer: A simple function should suffice.Do you can try this please ?
    //Example initial time to 1 houre == 100%
    TimeSpan InitialTime = new TimeSpan(1, 0, 0);

    private double getPercentOnTime(TimeSpan currentTime,TimeSpan timeToRemove)
    {

        //Convert all to minutes
        double currentTime_minute = currentTime.TotalMinutes;
        double timeToRemove_minute = timeToRemove.TotalMinutes;
        double InitialTime_minute = InitialTime.TotalMinutes;

        //Calcul the additional time to remove from InitialTime
        double totaltimeToRemove = currentTime_minute + timeToRemove_minute;

        //calcul the new percent
        double percent = (InitialTime_minute / 100.0) * totaltimeToRemove;

        return percent;
    }

Answer: TimeSpan totalTime;
TimeSpan actualTime

actualTime.TotalMiliseconds / totalTime.TotalMiliseconds * 100

Answer: Somtehing like that:
var tenHours = TimeSpan.FromHours(10); // 100%
var testHours = TimeSpan.FromHours(3); // 30%
var percent = ((float) testHours.Ticks/tenHours.Ticks)*100f;

Answer: You can format a double value (0.0 - 1.0) as a percentage using the "P" formatter.
(0.5).ToString("P0"); // "50%"

Comment: I'm not the downvoter, but I guess it's because the question wasn't about formatting, but about computing percentage. Your answer assumes the OP already knows how to compute (which he apparently doesn't). I'm upvoting, because I never knew about the `double.ToString(string format)`
Answer: Simple proportion. if 10 hours is 100% 5 hours is how much...
5/10*100


copying an arraylist containing an arraylist

Question: I have two classes, say Class Foo , and Class Boo.Foo class contains an arraylist of Boo class. I need to create a deep copy of Foo class, and I'm creating arraylist of Foo itself. How can I accomplish this.
        Class Foo{
        ArrayList<Boo> boovar = new ArrayList<Boo>();
        }

        Class Boo{
        int x;
        void add_x(int y)
        {
            x=y;
        }
        }

         ArrayList<Foo> foovar = new ArrayList<Foo>();

    ..adding elements to foovar.

now I want to create an ArrayList foovar1 of type Foo, such that it is a deep copy of the objects in foovar. ie, if I modify a boo object in foovar, like deleting x or adding elements, it should not be reflected when i access foovar1.
I know deep copying has been beaten to death,  but I really couldn't find articles telling me how to do this.  Could someone show me how with a code?
EDIT:
Im trying to create a copy constructor within Foo, like one of the comments mentioned, but I'm really at a loss on how to copy the Boo arraylist to another through the constructor in Foo.
Not to mention how I'm going to loop the instances of Foo in the arraylist to create copies of that as well. Any help would be really appreciated!
Comment: I think you would have to use loops, and copy constructor in `Foo` class to create a deep copy of your List. Because, you not only need a deep copy of your List, but also deep copy of `Foo` instances in your List.
Comment: did you try searching "java deep copy" here on SO? you will get many posts. some pointing to libraries while others have info and tips. If after that you still have a specific question, you can ask it. check this one it might be relevant to your question: http://stackoverflow.com/questions/3291830/deep-copy-and-arraylist-java
Comment: Declare your variable as the abstract `List`, not the concrete. It's good practice.
Comment: Format your code correctly and put that final `foovar` somewhere it would compile.  It is currently outside of a class definition.
Comment: @ A.J - well, I did. and most people suggested copy contructor/ serialization. I'm not sure how I should structure the copy constructor inside Foo class, since I would need to copy all the elements in Boo.  And, also,since Foo itself is an arraylist, I need to copy all instances of Foo in the arraylist as well. Seems complex, and I'm not sure how to do it.
Comment: @CoryKendall I was just providing an example class to explain the problem.. It wasn't code.. just an example that you could use to explain your solution
Answer: First, you need to make a copy Constructor in your Boo class:
Class Boo{
  int x;

  //copy constructor
  public Boo (Boo sourceBoo){
    x = sourceBoo.x;
  }

}

with the above, you can make a copy constructor in Foo class that copies the array of Boos contained in it:
Class Foo{
  ArrayList<Boo> boovar = new ArrayList<Boo>();

  //copy constructor
  public Foo(Foo sourceFoo){
    for (Boo aBoo:sourceFoo.boovar){
      boovar.add(new Boo(aBoo));
    }
  }

}

now, you can loop over your array of Foo(s) and copy them one by one into a new array. you can define a method to do that:
public ArrayList<Foo> deepCopyFooList(ArrayList<Foo> sourceArray){
   ArrayList<Foo> targetList = new ArrayList<Foo>();
   for (Foo aFoo:sourceArray){
     targetList.add(new Foo(aFoo));
   }

   return targetList;
}

so to copy your array you would:
ArrayList<Foo> foovar = new ArrayList<Foo>();
ArrayList<Foo> foovarCopy = deepCopyFooList(foovar);

Comment: Thank you so much! That not only helped me understand, it also worked without a hitch!! If I could, I would give it more than one upvote!!
Answer: EDIT:
List<Boo> newCopy = new ArrayList<Boo>(boovar);
newCopy.addAll(boovar);

..provided the elements of boovar are immutable. If not, you should utilize the clone() method on Boo class.
Comment: both ways make a shallow copy and not a deep copy as required in the question.


JMSListener - dynamic selector

Question: I currently have a JMSListener as shown below. It uses a selector of a value in a properties file. This works fine.
 @JmsListener(destination = "myQueueDest",
     selector = MyHeaders.SELECTOR_KEY + " = '${myapp.selector_val}'")
 private void consumeData(MyCustomObj mycustomObj) { }

I have a need now to use a dynamic selector with a value in memory, rather than the spring property. Is there a way to use JMSListener (or some other listener mechnaism) to do a selection off the ActiveMQ queue?
Update:
It may be possible to assign an ID to my @JMSListener, and then retrieve it from my JmsListenerEndpointRegistry bean. Get the listener container by ID, cast it to DefaultMessageListenerContainer, and call setMessageSelector(), although I'm not entirely sure if this will work yet.
This requires setting my DefaultJmsListenerContainerFactory bean to have the cache level of CACHE_SESSION.
But this doesn't seem to work, as the listener picks up all messages, regardless of what I set the message selector to be.
Answer: As stated in one of the comments:

the javadoc for setMessageSelector says it can be set at runtime. http://docs.spring.io/spring-framework/docs/2.5.x/api/org/springframework/jms/listener/AbstractMessageListenerContainer.html#setMessageSelector(java.lang.String)

This example explains how to setup at startup but doing it dynamically should be possible with a few more tricks:
@EnableJms
@Configuration
public class JmsConfiguration {

    @Value("${my.int.param:100}")
    private int config;

    @Bean
    public MessageConverter messageConverter() {
        final MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setTargetType(MessageType.TEXT);
        converter.setTypeIdPropertyName("_type");
        return converter;
    }

    @Bean
    public JmsListenerContainerFactory<?> specialQueueListenerFactory() {
        final String selector = "foo > " + config;
        final DefaultJmsListenerContainerFactory factory = new CustomJmsListenerContainerFactory(selector);
        factory.setMessageConverter(messageConverter());
        return factory;
    }
}

And the CustomJmsListenerContainerFactory
public class CustomJmsListenerContainerFactory extends DefaultJmsListenerContainerFactory {

    private final String selector;

    public CustomJmsListenerContainerFactory(String jmsSelector) {
        this.selector = jmsSelector;
    }

    @Override
    public DefaultMessageListenerContainer createListenerContainer(JmsListenerEndpoint endpoint) {
        final DefaultMessageListenerContainer instance = super.createListenerContainer(endpoint);
        instance.setMessageSelector(selector);
        return instance;
    }
}

Answer: JMS specification says the selection string must be provided while creating a consumer. So the answer is NO. Consumer must be closed and recreated with a different selection string to receive messages that match a different selection criteria.
If using JMS API is not a must for your project, then you could explore using Active MQ's native APIs. I am sure the API will have a way to specify a different selection string every time a receive is called. IBM MQ's native API provides such a functionality.
Comment: the javadoc for setMessageSelector says it can be set at runtime.

http://docs.spring.io/spring-framework/docs/2.5.x/api/org/springframework/jms/listener/AbstractMessageListenerContainer.html#setMessageSelector(java.lang.String)
Comment: How exactly do I close and recreate a new consumer? is there an example somewhere of creating a listener completely programatically without using @JmsListener? Thanks.
Comment: Please lookup Spring documentation. My knowledge of Spring is very limited.

JMSListener - dynamic selector


what's the difference between these two notations?

Question: I am using this book
http://www.librariamuzicala.ro/cs-docs/12069-1382689258.pdf
and just on page 3, the said:

I am using this virtual piano
http://virtualpiano.net
so what is the difference in this picture

between the ones with the line above and ones without ? (the time ? )
Please use simple words since as you can see I am still in the first page of this book and my theoretical knowledge of music is bad (that's why I am learning and asking).
Answer: The difference is that while tapping out the beats, you're supposed to sing a note—any note—on the beats that have the line over them, and not sing on the beats that don't. You're supposed to sing a single note, and that note is supposed to last the length of the beats that have the line above them.
In other words, you tap or clap out a steady beat, and for exercise (a), you sing a note lasting five beats, then don't sing for five beats, then sing another note lasting five beats, then don't sing for five beats, etc.
For exercise (b), you sing a note for the first four beats, then don't sing for four beats, then sing for four beats, then don't sing for four beats. etc.
And so on. For these exercise, at least, you don't need an instrument of any kind, including the virtual piano. These exercises are about training your ear and sense of rhythm; the actual notes aren't important.
Answer: Sorry to disagree with Alex, but the alternative seems like a better bet for a complete beginner, as in play/sing/tap 5 separate notes, then rest for the next 5.'Sing the tone only on the strokes'. That would be why there are 5 stems shown. The same idea works for each example. In fact, you could also use Alex's idea for a change.
In fact, the instructions are ambiguous - possibly on purpose, but who knows.
Comment: Yours is a fine idea, but if you download the PDF, the instructions aren't ambiguous. In the clip the OP posted, the instructions mention "the tone", which is defined previously in Exercise 2 as a single long tone lasting the duration of the connected beats.
Comment: Thanks, Alex. Unfortunately, not enough information wasn't given. If that info. is there, why the question?
Comment: You got me; maybe the OP just wanted to be sure. Anyway, it's a very interesting book, and I really enjoyed reading the introduction.
Comment: @Tim from the user name and profile, OP's first language is probably not English. It might not be obvious to an Italian speaker whether "tone" meant "tono", "tonalità", or "timbro" (though an English speaking musician probably doesn't need an Italian dictionary to guess the different meanings of those three words!)
Comment: @alephzero - all the more reason for having an accurate profile on this site. It gives us more basis for an intelligent answer.


Cross-correlation of two autocorrelated signals (removing autocorrelation with ARIMA)

Question: I want to get the cross-correlation of two time series x and y in R.
I have calculated an ARIMA model, and I can get the mod1$residuals from signal x. These residuals almost have no autocorrelation, so that's great.
xts <- ts(x,start=1,frequency=12) #convert to a time series
library(fpp)  #load forecasting package
mod1 <- auto.arima(xts)

I now did the same procedure on signal y.
My question is: is this correct? Or should I somehow deduct the mod1 (based on x) from y to de-trend it?
ccf(mod1$residuals, mod2$residuals)

Secondly, I am confused about the order of operations. Should I prewhiten the data before calculating the model?
I found this code:
prewhiten(x, y, x.model = ar.res,ylab="CCF", ...)

Should I estimate the mod1 first and then supply it to the function prewhiten? And are x and y the two time series? Many thanks!
Comment: Related to [this](http://stats.stackexchange.com/questions/191131/testing-significance-of-cross-correlated-series/).
Comment: I don't fully understand the answer you point to. Do I remove the arima model from both time series? And do I remove the same model or make two models?
Comment: I know I did not manage to provide a clarification there; I posted the link so that other users could see the background of the question.
Comment: Regarding terminology, you "estimate" rather than "calculate" model `mod1` or ARIMA model. It could be nice to have this fixed in both your question and your answer.
Answer: Prewhitening seems like a good way to help interpret ccf. (see here)
Prewhitening basically estimates a model (such as ARIMA) on data1, then the residuals data2 and this model are used in a ccf function with data1 residuals. In the case of this question:
 print(prewhiten(y, model=mod1))

will display the ccf graph


Problem with two normally distributed random variables

Question: Suppose bridges can tolerate mass without structural damage following $N(200000,20000^2)$ and that car weights follow $N(1300,130^2)$. Unit is kg for both random variables. I'm trying to find out the amount of cars needed on the bridge for causing structural damage with probability greater than $0.1$.
I calculated $\frac{X-200000}{20000}<1.645\implies X<232 900$, where $1.645$ is the $0.95$ decile (I don't know if decile is the correct term here) of normal distribution and so if the cars weigh more than $232900kg$ or less than $200000kg-32900kg=167100kg$, the bridge will suffer structural damage with a probability of $0.1$. This seems flawed, because cars weighing less means that one car, two cars etc. might cause structural damage with a $5\%$ probability.
Then I tried to calculate amount of cars, the probability for previously calculated car weights set to $1$. In other words, how many cars are needed to guarantee, that their total weight exeeds $232900kg$. But I can't seem to cross the bridge from a single (standardised) random variable to the sum. If the sum of $n$ car weights follows $(200000\cdot n,20000^2\cdot\sqrt{n})$, then how to standardise it and solve for $n$? I found that $Z_n=\frac{S_n-\mu\cdot n}{\sqrt{n}\cdot\sigma}$, where $S_n$ is the sum of $n$ car weights. But from that I got $n$ next to zero.
My approach might be completely wrong so any help and corrections are appreciated. This is a new kind of problem for me.
edit: corrected an error
Comment: How do you arrive at your conclusion "so if the cars weigh more than ... or less than"? The latter part of it is inconsistent with your calculation.
Comment: I suppose normal distribution is symmetric. So if the other direction is 32900kg, then also the other is? ... oh, now I see the mistake. I didn't calculate anything with the lower part, though. I was just trying to find the number of cars to outweigh the upper part.
Comment: Should the first equation be$$\dfrac{X-200000}{20000}$$
Comment: Corrected that one, too.
Answer: A complete solution requires some familiar basic steps.

Describe the problem in statistical language.

The solution will require finding a quantile of a random variable.  Because this variable turns out to have a Normal distribution, at a minimum we will need to compute its expectation and variance (its parameters).

Now that we know the parameters of this distribution, we can compute any probabilities we wish.  They will depend on the number of cars, giving us an equation relating the number of cars and the failure probability.

Solve the equation.

Before we go any further, let's do some reality checks.  If we were being so crude as to ignore all uncertainty, we would estimate the bridge could hold about $200000/1300 \approx 154$ cars. That number of cars intuitively ought to correspond to a failure probability near $0.5.$  Thus, a probability of $0.1$ will be reached with fewer cars.  The insight afforded by these probability models concerns how many fewer.  That is, at what point should we seriously start worrying about the bridge?  After 10 cars?  100 cars?  153 cars?

I read the question as follows:

Suppose the total loading a bridge can tolerate is a random variable $X$ with a Normal$(\mu,\sigma^2)$ distribution.  Suppose further that there is a line of cars whose weights are random variables $Y_i,$ $i=1,2,3,\ldots,$ with a common Normal($\nu,\tau^2)$ distribution.  Assume $X$ and all the $Y_i$ are independent.  Drive the cars one by one onto the bridge so that after car $n$ is placed, the total weight is $S_n = Y_1 + Y_2 + \cdots + Y_n.$  What is the smallest $n$ for which the chance of bridge failure (computed before driving any cars onto the bridge!) exceeds $p = 0.1$?

To obtain an answer, we ought to find the chance of bridge failure in terms of $n.$ This is readily obtained by expressing the event in terms of the random variables as
$$\Pr(\text{failure with } n \text{ cars}) = \Pr(S_n \gt X) = \Pr(X - S_n \lt 0),$$
thus reducing the calculations to a single random variable $X-S_n.$
The expectation is
$$E[X - S_n] = E[X] - E[Y_1] - E[Y_2] - \cdots - E[Y_n] = \mu - n\nu$$
and, because the variables are independent, their variances add:
$$\operatorname{Var}(X - S_n) = \operatorname{Var}(X) + \operatorname{Var}(Y_1) + \cdots + \operatorname{Var}(Y_n) = \sigma^2 + n\tau^2.$$
Moreover, because all these distributions are Normal, $X-S_n$ has a Normal distribution -- and we now know its parameters.  Thus,
$$X - S_n\ \sim\ \operatorname{Normal}(\mu - n\nu, \sigma^2 + n\tau^2).$$
Let $\Phi$ be the standard normal distribution function (CDF).  Upon standardizing $X-S_n$ and recalling that normal distributions are continuous, we obtain

$$\Pr(X - S_n) \lt 0 = \Pr(X - S_n \le 0) = \Phi\left(\frac{\mu - n\nu}{\sqrt{\sigma^2 + n\tau^2}}\right).$$

A systematic search, perhaps aided by a plot of this probability, will determine the solution.

Just plot the horizontal line at level $p,$ find where it crosses the plot, and read down to find $n$ (here written $n^{*}(p)$).
If you would like a formula for $n,$ you will need to invert $\Phi$ using its quantile function $\Phi^{-1}:$
$$\Phi^{-1}(p) = \frac{\mu - n\nu}{\sqrt{\sigma^2 + n\tau^2}}.$$
Squaring both sides and clearing the denominator gives a quadratic equation for $n,$
$$0 = (\nu^2)n^2 - \left(2\mu\nu + \Phi^{-1}(p)^2\tau^2\right) n  + \mu^2 - \Phi^{-1}(p)^2 \sigma^2.$$
Solve it any way you please, such as with the Quadratic Formula.
Such a quadratic equation has two solutions, equally spaced around $\mu/\nu = 200000/1300 = 153.85.$  They will correspond to $p$ and $1-p$ because $\Phi^{-1}(p)^2 = \Phi^{-1}(1-p)^2.$  Pick the smaller solution when $p \lt 1-p$ and otherwise pick the larger solution.  You will get a fractional value $n^*.$  Thus, the bridge has a chance $p$ of tolerating any whole number of (random) cars less than or equal to $n^*,$ but putting any more cars than that on the bridge will make the risk of failure exceed $p.$
For the data in the question, the higher value of $n^{*}$ is $173.6.$ This means the chance of bridge failure will first exceed $1-0.1 = 0.9$ when $n=174$ cars are driven onto the bridge.  I leave it to you to find the answer to the question, which is the lower value of $n^{*}.$
Comment: @Maximilian I did not save it, but `curve(1 - pnorm((mu - n * nu) / sqrt(sigma^2 + n * tau^2)), 100, 200, xname = "n")` ought to get you started.
Comment: Thank you very much, this looks like a wonderfully educational answer. I'll accept it after I have had the time to go through it.
Comment: Can I please explore the `r` implementation? thanks.
Comment: @Whuber: many thanks, can you also help with tau parameter? Not quite sure how to obtain this. Also not getting the right outcomes, from the `optim()`.
Comment: @Max I'm sorry, but I don't understand what you mean by "help with."  $\tau$ is one of the numbers stated in the problem.
Comment: @whuber: I'm sorry, I overlooked it, all good. Apologies for wasting your time.


How to solve the Index error in Pytorch when I use "for x, y in Dataloader"

Question: I am using K-Fold method to train a Classifier. And use the KFold moudule of sklearn.
FK_split = KFold(n_splits=4, shuffle = True, random_state=0)

for epoch in range(num_epoch):
    train_loss = 0.0
    Acc_valid = 0.0

    for train_idx, valid_idx in FK_split.split(torch_trainDataset):
        train_sampler = SubsetRandomSampler(train_idx)
        valid_sampler = SubsetRandomSampler(valid_idx)
        train_dataloder = DataLoader(torch_trainDataset, batch_size=1, sampler=train_sampler)
        valid_dataloder = DataLoader(torch_testDataset, batch_size=1, sampler=valid_sampler)

        train_loss += train(model, train_dataloder, lossfunc, optimizer, train_loss)
        _, acc_valid = test(model, valid_dataloder, optimizer)

and the train function, test function and acc function are defined as follows,
def train(model, data_train, lossfunc, optimizer, train_loss):
    for x, y in data_train:
        optimizer.zero_grad()
        output = model(x)
        loss = lossfunc(output, y)
        loss.backward()
        optimizer.step()
        train_loss += loss.item()*x.size(0)
    return train_loss

def get_acc(outputs, labels):
    """caculate acc"""
    _, predict = torch.max(outputs.data, 1)
    correct_num = (labels == predict).sum().item()
    return predict, correct_num

def test(model, data_test,  optimizer):
    Predict = []
    Acc = 0.0
    for x, y in data_train:
        outputs = model(x)
        predict, acc = get_acc(outputs, y)
        Predict.append(predict.tolist())
        Acc += acc
    return Predict, Acc

**However, the IndexError occurs in test process while the same method in train process works. Could you guys help me solve this problem? I attach IndexError information below. **
Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In [38], line 34
     30     valid_dataloder = DataLoader(torch_testDataset, batch_size=1, sampler=valid_sampler)
     32     train_loss += train(model, train_dataloder, lossfunc, optimizer, train_loss)
---> 34     _, acc_valid = test(model, valid_dataloder, optimizer)
     35     Acc_valid += acc_valid
     37 Acc_valid = Acc_valid / len(valid_dataloder)

Cell In [30], line 20, in test(model, data_test, optimizer)
     18 Predict = []
     19 Acc = 0.0
---> 20 for i, data in enumerate(data_test, 0):
     21     x, y = data
     23     outputs = model(x)

File c:\Users\Ryan\anaconda3\envs\d2l\lib\site-packages\torch\utils\data\dataloader.py:681, in _BaseDataLoaderIter.__next__(self)
    678 if self._sampler_iter is None:
    679     # TODO(https://github.com/pytorch/pytorch/issues/76750)
    680     self._reset()  # type: ignore[call-arg]
--> 681 data = self._next_data()
    682 self._num_yielded += 1
    683 if self._dataset_kind == _DatasetKind.Iterable and \
    684         self._IterableDataset_len_called is not None and \
    685         self._num_yielded > self._IterableDataset_len_called:
...
File c:\Users\Ryan\anaconda3\envs\d2l\lib\site-packages\torch\utils\data\dataset.py:188, in <genexpr>(.0)
    187 def __getitem__(self, index):
--> 188     return tuple(tensor[index] for tensor in self.tensors)

IndexError: index 106 is out of bounds for dimension 0 with size 27

Thank you so much if you can offer me help.
Comment: It seems you're iterating over `data_train` in the `test` function. Try fixing that maybe?
Answer: you must use same sampler and data set.
now you are using torch_testDatase with valid_sampler.
use test_sampler  instead of valid_sampler.


Can you add privilege level command to allow a certain user to add new users for an ASA?

Question: I thought I could get this done by doing the following:
privilege cmd level 6 mode exec command user

But under a user with that priv level the user can only do the following
user newuser

They can't assign a password and priv level.

Maybe there is a better / another way to do this?
Comment: Could you explain the reason for wanting this? TACACS+ or RADIUS may be better options.
Comment: I was trying to think of the fastest way to give a client the ability to do this (to manage vpn users). I found out we have enough time to implement a RADIUS service so that will be the path.
Answer: In exec mode, user expands to user-alert; this isn't what you want.
You're probably looking for something more along these lines:
privilege cmd level 6 mode configure command username

And then possibly some allowances for config-username mode:
privilege cmd level 6 mode username command service-type
privilege cmd level 6 mode username command group-lock
...

But that's worthless as anything other than a thought exercise.  If I'm a restricted level 6 user, and I'm allowed to run the username command in configure mode, then I can create myself a level 15 user; it's just a waste of effort to put a completely ineffective restriction in place.  Adam's exactly right, TACACS+ should be used for this.
Comment: Agh well that explains it! Good to know at least.


Binning Calibrated probability scores for business use

Question: Context:
We have a model that outputs calibrated probability scores for a binary classification problem (events/nonevents). There is a general business requirement that we bin these outputs further to make the predictions easier for business usage. For example, it is encouraged that we overlay the probability scores with scores of 1 to 5, where 5 will represent prob. scores of say 10%-100%, score 4 will do the same for scores of say 5%-10%...etc. And say one arrives at the bin edge 10-100% because the area contains 90% of the minority class (events) in the test sample and also is 2% of the entire population, and similarly one arrives at bin edge 5-10% because the area contains an additional 5% of the minority class (events) in the test sample which is also 25% of the total population, so by reviewing bins 4 and 5 (5% to 100%  prob. scores) one reviews only 27% of the population but captures 95% of the events.
The question is:
This binning process is extremely qualitative and subject to criticism (why 5 bins, why should we believe 90% of the event population will remain in the 10%-100% in production setting...), does anyone know of a robust method to post-process calibrated probabilities into bins for decision making, without breaking this fundamental rule of machine learning "In general, with a multistep modeling procedure, cross-validation must be applied to the entire sequence of modeling steps" ESL II."?
Any resources and links would be appreciated.
Comment: Yeah I have dealt with this before as well. Often I do it via constraints, e.g. business wants to prioritize N events in a queue, e.g. top 1000 are bin 1, next 1000 are bin 2, etc. (To test calibration later on make sure to cache the original probability prediction.)
Answer: If you need to bin the predictions for business reasons, you probably want to do something like treating the bins differently, e.g. assigning them to different marketing campaigns. In such a case, you have in mind optimizing some objective (money, clicks, subscriptions, churn, etc). What you need to do is find such bins that are optimal given the objective. This is an optimization problem. Any other binning strategy would be arbitrary.
Comment: @PaulNoah you give us almost any details so it is impossible to give detailed, problem-specific, answer. How this could be done and validated depends on the details of your specific problem. If you use some arbitrary binning you're also not doing anything to validate it or make it optimal, not even locally.
Comment: I hear you. But I can't ignore the fact that we would be optimizing on one dataset and proceeding to use the results (bin thresholds) on outside samples. You can't really use the outcomes that have been created from a generating process that wasn't designed for it, that is why we do cross validation for model development. Cross validation ensures that the model is tuned  such that it will work on out sample data, optimization on a sample data does not ensure that. I wonder if one can do the type of optimization you are mentioning with a cross validations mechanism.
Comment: do you have any other thoughts on the subject that does not require optimization? Broad ideas will be fine. At this point I will be very happy camper if you got anything else, even if they are just links. Optimization is not a feasible option for the type of problem I am trying to solve.
Answer: To follow up on what @Tim said, Let me give some specific examples.
Optimizing for marginal revenue of investment: cut at the first edge that accounts for X% of revenue increase. X is decided by the amount required to increase head count in our department in next quarter, cause HR has asked for support evidence. We will make a case to hire new people to support customers in this first bin with the expected revenue it will bring.
Optimizing for CTR: CTR is usually so skewedly distributed that a small percentile accounts for most data points. So a summary of the large group can inform decision on most of the customers. Here we are trading off complexity of the summary and the analysis coverage of the customers. If you wanna cover more customers in your summary, your conclusion and recommendation is harder to communicate to the business sector. Why 95% but not 98%? Cause the extra 3% looks different enough to complicate the conclusion without adding much to the recommendations.


Using multiple TF-IDF matrices on a classification task

Question: I have a dataframe with a certain number of columns of type text. Let's say column A has the name (random names that cannot be used as keys but resemble sentences) and B has its description(a bigger chunk of text). Whilst training, I would want a higher weightage for the column A than column B. Using a single TF-IDF matrix will make it quite homogenous i.e words from A and B will get mixed up and be considered identical in terms of the importance. What could be a possible solution to implement this. I was thinking of multiple TF-IDF matrices.
Edit: After some brain storming, it looks like multiple tf-idf is not an appropriate solution as the vocabulary set will be different. Any other approach would be helpful.
Answer: You can't necessarily do that, and it's not recommended even if you find a way/hack to do it. One hack would be to ensemble multiple models, weighted - with one feature per model. This is not a tested approach, so proceed with caution.
Do Recursive Feature Elimination (RFE) to see how it affects your accuracy + precision + recall. Also, rank your features using random forests benchmarks to be able to see which feature gives your model more variance.


Is it possible to run APPCMD from Server 2003?

Question: In our transitory migration situation, I have the need to run APPCMD from a Windows Server 2003 machine, but of course targeting an IIS7 instance on a remote Windows Server 2008.
Is it possible?
(I am NOT trying to run IIS7 on Windows Server 2003)
Nick
Answer: appcmd doesn't have a way to remotely control any machine (even between 2 W2k8 machines) so the answer to your question is no.
Just out of curiosity I tried copying appcmd.exe from a W2k8 box to a W2k3 box, and I get an error that the executable "is not a valid Win32 application".
Answer: I solved it with PsExec on the Windows Server 2003 machine.
Answer: You can try running this command on the prompt:
<code>c:\windows\system32\cscript.exe c:\windows\system32\iisapp.vbs
</code>


Bayesian regression - prior dist for variables

Question: In a multiple Bayesian linear regression model, do all variables (dependent and predictors) get prior distributions?
If so, can one mix non-informative and substantive priors in the model?
Thanks!
Answer:
In a multiple Bayesian linear regression model, do all variables
  (dependent and predictors) get prior distributions?

All parameters get a prior. Random variables/data might get a prior if you're modeling missing/latent/hidden data.

If so, can one mix non-informative and substantive priors in the
  model?

Based on this information alone, I don't see anything wrong with this approach.


Case Deletion Diagnostics

Question: I have NO idea how to approach this problem. I don't see any connection between the corollary and the formula we need to prove. Does anyone have any hints?
Corrolary: If $\mathbf{A}$ and $\mathbf{B}$ are nonsingular matrices of dimensions $m\times m$ and $n\times n$, respectively, and $\mathbf{C}$ and $\mathbf{D}^\intercal$ are arbitrary $m\times n$ matrices, and $\mathbf{A+CBD}$ is nonsingular, then $$ (\mathbf{A+CBD})^{-1} = \mathbf{A}^{-1} - \mathbf{A}^{-1}\mathbf{C}(\mathbf{B}^{-1}+\mathbf{D}\mathbf{A}^{-1}\mathbf{C})^{-1}\mathbf{DA}^{-1}$$
Use the preceding corollary to prove the following updating formula which is very useful for case deletion diagnostics in regression analysis $$ (\mathbf{X}_{-i}^\intercal\mathbf{X}_{-i})^{-1} = (\mathbf{X}^\intercal\mathbf{X})^{-1} + \dfrac{(\mathbf{X}\intercal\mathbf{X})^{-1}\mathbf{x}_i\mathbf{x}_i^\intercal(\mathbf{X}\intercal\mathbf{X})^{-1}}{1-h_{ii}}$$ where $$\mathbf{X} = \begin{pmatrix} \mathbf{X}_{-i} \\ \mathbf{x}_i^\intercal\end{pmatrix} \qquad h_{ii}=x_i^\intercal(\mathbf{X}\intercal\mathbf{X})^{-1}\mathbf{x}_i$$ You may assume that the inverses of $\mathbf{X}^\intercal\mathbf{X}$ and $\mathbf{X}_{-i}^\intercal\mathbf{X}_{-i}$ exist.
Answer: I got it:
$$\mathbf{X}^\intercal\mathbf{X} = \mathbf{X}_{-i}^\intercal\mathbf{X}_{-i} + \mathbf{x}_i\mathbf{x}_i^\intercal$$ Therefore $$\mathbf{X}_{-i}^\intercal\mathbf{X}_{-i} =\mathbf{X}^\intercal\mathbf{X} - \mathbf{x}_i\mathbf{x}_i^\intercal = \mathbf{A}+\mathbf{BCD}$$ where $\mathbf{A} =  \mathbf{X}^\intercal\mathbf{X}$, $\mathbf{B} = -\mathbf{x}_i$, $\mathbf{C}=1$, and $\mathbf{D} = \mathbf{x}_i^\intercal$. Plug and chug gives us $$\begin{align*}
(\mathbf{X}_{-i}^\intercal\mathbf{X}_{-i})^{-1} &= (\mathbf{X}^\intercal\mathbf{X})^{-1} + (\mathbf{X}^\intercal\mathbf{X})^{-1}\mathbf{x}_i(1-\mathbf{x}_i^\intercal(\mathbf{X}^\intercal\mathbf{X})^{-1}\mathbf{x}_i)^{-1}\mathbf{x}_i^\intercal(\mathbf{X}^\intercal\mathbf{X})^{-1}\\
&=  (\mathbf{X}^\intercal\mathbf{X})^{-1} + (\mathbf{X}^\intercal\mathbf{X})^{-1}\mathbf{x}_i(1-h_{ii})^{-1}\mathbf{x}_i^\intercal(\mathbf{X}^\intercal\mathbf{X})^{-1}\\
&= (\mathbf{X}^\intercal\mathbf{X})^{-1} + \dfrac{(\mathbf{X}^\intercal\mathbf{X})^{-1}\mathbf{x}_i\mathbf{x}_i^\intercal(\mathbf{X}^\intercal\mathbf{X})^{-1}}{1-h_{ii}}
\end{align*}$$

Case Deletion Diagnostics


Views will display entities the user does not have "view" access permission

Question: I have a custom entity type that can be viewed by certain people only. This works just fine, when people go to "mysite.com/custom-entity/8" some can see the info, some get "access denied" error.
The problem is I created a view to display a list of those entities and all of them are shown to all users, even if certain users shouldn't have "view" access permission.
LE: Caching is set to "None"
Any ideas on how to fix this? Thanks.
Comment: Gotcha. When you create a custom entity in Drupal you can define a permission for viewing the entity which will be managed through the standard configuration system. The benefit to that is exactly what I have outlined in my answer below: you can tie that permission to other operations on the site that you cannot affect within an access control handler.
Comment: This sounds like it could be a caching issue. Does this happen without caching enabled for your view?
Comment: Caching is set to "None"
Comment: Is the access on the custom entity controlled by a configurable permission?
Comment: Access is controlled by a custom access control handler just like any other Drupal entity.
Comment: I updated my answer :)
Comment: It has nothing to do with view access handler, all users should have access to the view but the view results should be different per user. Eg: UserX should see a list of entities like e1, e2, e3 and UserY should see only e1 and e2.
Answer: Option A
Create a custom access handler for your view.
Drupal 8 - Views Custom Access with a Plugin is a great guide. It seems like your scenario may fit the use case outlined in the beginning:
"... prohibit the display of a Drupal 8 View dependent upon some custom rules that don't fit easily with Drupal's permissions model ..."
Pro: You will get exactly the control features you want.
Con: A lot of custom code.
Option B
If you want permission for viewing the entity to be directly tied to the view access, there is an "Access" section on the view configuration form. Select "Permission" and then select the permission for viewing the entity you have created.
Pro: Easy to set up.
Con: Your use case must fit into Drupal's permissions system.


SQL Number rows of table in function (should be easy)

Question: I'd like to number the rows of a table returned by a query. More specifically my function randomizes the rows of a table. I want an extra column which counts from 1-n AFTER the randomization is done. Here is my current code for the randomize function:
create or replace function random_surnames(num integer)
returns table(name character(30)) as
$$
begin
    RETURN QUERY
    select public."Surname".surname
    from public."Surname"
    order by random()
    Limit num;
end;
$$
language 'plpgsql' volatile;`

It should be simple enough, I just can't seem to be able to find a solution online maybe because I'm not using the correct keywords.
Answer: This can be achieved using ROW_NUMBER and a subquery, here is an example of converting your query to do that:
select *, ROW_NUMBER() OVER () FROM
(select public."Surname".surname
from public."Surname"
order by random()
Limit num) subq;

Comment: Beat me by seconds... :) +1
Comment: Works like a charm! Thanks a lot, I was confused for over 1 hour on this! Now it makes sense. @JorgeCampos thanks for the edit as well


Move Menu minipanels DOM location

Question: I am trying to create a Menu-minipanel which will appear directly below the horizontal header menu, and push the content below down.
I have set the menu positioning to static so that it sits within the page structure, but that renders it at the bottom of the page when triggered (which is where it is put by default, and usually repositioned from there outside of the page structure).
Among the Menu mini-panels options there is a field labelled:

"Allows control over where within the page's DOM structure the
  minipanel will be inserted. This defaults to $(document.body), i.e. at
  the end of the page's content."

but I have absolutely no idea how to manipulate this, and cannot find any documentation which makes this clear.
DOM is something I simply don't have a clue about, and I feel like I am so close to the answer, but my own knowledge seems to have run out!
Any guidance would be so appreciated!
Comment: Looks like I cant answer my own question as a relatively new user, but through a bit of experimentation I found that the reference to the default value of '$(document.body)' was what confused me - it turns out that just by adding a standard css selector to that field moved the menu.

Its always when one asks for help that inspiration strikes!

Thanks for reading Alex!
Comment: Thats basically saying it gives you the option to place the panel where you want to. Have you event tried it?
Answer: So, though a bit of experimentation I found that the language

"Allows control over where within the page's DOM structure the
  minipanel will be inserted. This defaults to $(document.body), i.e. at
  the end of the page's content."

Was what confused me - it turns out that just by adding a standard css selector to that field moved the menu.

Move Menu minipanels DOM location


For $x, y \in U$ given, how can I find a $C^\infty$ diffeomorphism $F: U\to U$ satisfying $F(x)=y$?

Question: The problem is:

Given $x, y\in U\subset \Bbb{R}^n$, where $U$ is open and connected, find a $C^\infty$ diffeomorphism $F: U \to U$ such that $F(x)=y$.

This is a problem in basic Real Analysis. I am looking for a solution without using the concept of flow, like this one. (Maybe using bump function? Or something easier?)
Comment: Let $r$ be such that $B_r(y) \subset U \cap (U + y-x)$ and let $\phi:\mathbb R^n \to \mathbb R$ be a smooth function compactly supported function on $B_r(y)$ that is one at $y$. Then $F(a)=\phi(a+y-x)(a+y-x)$ should do the job.
Answer: Here's an idea: In one variable, suppose $x_1,x_2\in (a,b).$ Then there exists a diffeormorphism of $\mathbb R$ onto $\mathbb R$ such that $f(x_1) = x_2$ and $f(x) = x$ in an open set containing $(-\infty,a]\cup [b,\infty).$ Thus the diffeomorphism $f$ is the identity outside of a compact subset of $(a,b),$ $f$ maps $(a,b)$ onto $(a,b),$ and $f(x_1) = f(x_2).$
Suppose for simplicity we're now in $\mathbb R^2.$ Choose an open square $S=(a,b)\times (c,d)$ with $\bar {S}\subset U.$ Let $(x_1,y_1),(x_2,y_2)\in S.$ Choose two maps $f,g$ like the above with respect to the intervals $(a,b),(c,d),$ such that $f(x_1)=x_2,g(y_1) = y_2.$ The map $(x,y) \to (f(x),g(y))$ is then a diffeormorphism of $\mathbb R^2$ onto $\mathbb R^2$ that maps $S$ onto $S,$ is the identity off of a compact subset of $S,$ such that $(x_1,y_1) \to (x_2,y_2).$
Thus we can move points around in any open square whose closure in contained in $U.$ Finitely many iterations of such maps, based on the connectedness of $U,$ should then give the desired result.


Como devo atribuir strings no C#?

Question: Até aonde sei, System.String no .NET (não sei se em outras plataformas o tipo string também é assim) é imutável, ou seja, se eu fizer:
string a = "texto";
a = "outro texto";

Por detrás dos panos, ao atribuir outro texto em a, ele vai criar outra instancia de a. É isso mesmo ou estou falando algo equivocado?
Existem algumas partes do código aqui na empresa com o seguinte cenário:
private void Cadastrar(string Valor, string Valor2, string Valor3)
{
    string msg = "";
    try
    {
        Entidade obj = new Entidade();
        DAL objDal = new DAL();

        msg = "Campo 1";
        obj.Campo1 = Valor;

        msg = "Campo 2";
        obj.Campo2 = Valo2;

        msg = "Campo 3";
        obj.Campo3 = Valor3;

        objDal.Cadastrar(obj);

    }
    catch(Exception ex)
    {
        string errmsg = "Erro ao cadastrar o campo " + msg;
        MessageBox.Show(errmsg);
    }
}

Eu imagino que a atribuição das strings deveria ser a menor das minhas preocupações nesse cenário, mas existe alguma forma de eu conseguir melhorar as atribuições dessas strings ou isso é frescura da minha parte?
A minha preocupação é que isso acontece em vários lugares e creio que isso poderá ser um peso desnecessário na aplicação e que pode ser corrigido ou melhorado de alguma forma.
Answer:
Por detrás dos panos, ao atribuir outro texto em a, ele vai criar outra instância de a. É isso mesmo ou estou falando algo equivocado?

Não exatamente, neste exemplo está "criando" outra instância, mas nem sempre isso é verdade, atribuir um valor a uma variável e criar instância são coisas distintas. Embora essa instância específica já esteja no executável, então criar também não é bem o termo. Literais do tipo string já estão todos criados quando inicia a execução da aplicação, uma instância só é criada em nova área de memória quando o dado vem externamente ou é construído em tempo de execução.

Eu imagino que a atribuição das strings deveria ser a menor das minhas preocupações nesse cenário

Não sei, eu me preocupo bastante, e entendo todo funcionamento da memória do .NET para tomar as decisões certas.

mas existe alguma forma de eu conseguir melhorar as atribuições dessas strings ou isso é frescura da minha parte?

Vale meu primeiro comentário. Pode ter um jeito melhor de fazer esse mecanismo que criou como um todo (inclusive porque é uso errado de exceção, mas não vem ao caso aqui), mas feito desta forma não tem como reduzir a quantidade de instâncias de texto existentes. Exceto talvez a concatenação de strings dentro de exceção, mas é como falei, esse erro só existe porque o uso de exceção está errado.
Quando concatena duas strings está criando uma terceira, então prefira uma interpolação, o ganho pode não ser grande, mas pode valer a pena.
Não afeta nada no  que está perguntando, mas poderia afetar outro caso, por que criou uma variável  que só é usada uma vez? Em geral isso é um equívoco.

A minha preocupação é que isso acontece em vários lugares.

Isso pode ser um problema, tudo o que acontece em vários lugares tender a ser o mecanismo errado e provavelmente viola o DRY.
Comment: Neste caso, zero, ou seria negativo.
Comment: Pelo que entendi, utilizar StringBuilder não iria ajudar muito, certo? O problema é muito maior, ou seja, no conceito e não simplesmente eu trocar A por B.

Como devo atribuir strings no C#?


Just realized IEnumerable is immutable, what are other most commonly used immutable generic interfaces? MSDN doesn't say

Question: Just realized IEnumerable is immutable, what are other commonly used immutable generic interfaces? MSDN doesn't say.
Or any good online resources?
Comment: Is that a useful thing to know there are probably loads that you will never use, isn't that a bit like asking "is there a list of classes implement `IEnumerable`"
Comment: How would you know that an implementation of IEnumerable is immutable? Immutability is not part of the definition.
Answer: Apart from the fact that it doesn't make a whole lot of sense to refer to interfaces as being immutable, IEnumerable<T> isn't, for example:
List<string> myList = new List<string>();
myList.Add("Test"); // We just changed the state of myList, hence its not immutable

An example of an object that is immutable is String.
It doesn't make sense for an interface to be immutable as almost by definition an immutable object needs to be sealed, which an interface obviously cannot be.
Comment: but `IEnumerable myList = new List(); myList.Add("Test");` is an error... because `IEnumerable` is immutable.