Motivation behind definition of homologous cycles

Question: Two cycles are said to be homologous if their difference is a boundary (usual meanings implied).
What is the motivation behind this definition or the intuitive meaning it carries?
I am looking of something along the line of definition of homotopic curves, that they are curves which can be continuously deformed into one another.
Or may be if such a simple explanation isn't possible, what developments led to this particular definition, and what geometric properties of the topological space and homeomorphisms does it state.
Diagrams will be exceptionally helpful if possible.
Comment: This is an atypical reference, but pages 110-111 of this [arXiv paper](http://arxiv.org/pdf/0908.1395v1.pdf) have a nice intuitive discussion. (The whole chapter is pretty good.)
Comment: Some of the answers here http://mathoverflow.net/questions/640/what-is-cohomology-and-how-does-a-beginner-gain-intuition-about-it look helpful.
Comment: This Wikipedia page on [Pochhammer contours](http://en.wikipedia.org/wiki/Pochhammer_contour) is also surprisingly relevant: it gives an explicit example of a contour which is homologous to zero but not homotopic to zero. (That means that you can't deform the contour into a point, but it still would integrate to zero because it has zero net winding around every point.)
Comment: I'd start by getting a sense for what a cobordism between manifolds means.  Then look at the definition of singular bordism and bordisms between singular maps.  Next, think of a singular chain as a degenerate singular bordism.  That's my preference.  I think of homology between cycles as a triangulated version of bordism between cycles.
Answer: As mentioned by @RyanBudney, homology between cycles is like a PL version of cobordism between manifolds inside an ambient space. Cobordism just like homotopy, but continuous deformation of one submanifold to another instead of just loops.
(note : I have added hover texts with the pictures, please place your mouse point over the pictures to see the corresponding texts explaining them)
$M, N$ be closed $n$-manifolds. $W$ be a $(n+1)$-manifold such that $\partial W = M \sqcup N$. Then $W$ is said to be a (unoriented) $\it{cobordism}$ between $M$ and $N$, and $M$ is said to be $\it{cobordant}$ to $N$. One can easily verify that cobordism is an equivalence relation: reflexivity ($M \sqcup M$ bounds $M \times I$) and symmetry is obvious, and transitivity can be seen by gluing cobordisms along boundaries.

Define $\Omega_n^{un}$ to be the collection of all cobordism classes of manifolds $[M]_{bor}$ with group operation $[M]_{bor} + [N]_{bor} = [M \sqcup N]_{bor}$. The identity under this group operation is the cobordism class $[\emptyset]_{bor}$ of the empty manifold. $\Omega_n^{un}$ is called the $n$-th unoriented cobordism group.
If $X$ is a simplicial/delta-complex, $\xi$ a simplicial $n$-cycle. Then $\xi$ can be thought, roughly, as a subcomplex of $X$ (by realizing each simplex in the formal linear combination as a simplex of the subcomplex, with products $n \cdot \Delta^i$ thought as an $i$-simplex thickened by a factor of $n$) with no boundary (i.e., no "open edges"). $\xi'$ be another $n$-cycle, realized as a subcomplex-ish thing.
If $\xi$ and $\xi'$ are homologous, the we know there is a $(n+1)$-chain $\sigma$ such that $\partial \sigma = \xi - \xi'$. But this $\sigma$ can also be realized as an $(n+1)$-dimensional subcomplex(-ish) of $X$ but with a few open edges this time such that boundary of the subcomplex is precisely $\xi - \xi'$. This reminds us of bordism, doesn't it?

Indeed, more formally, if we work in the singular context, then an $n$-cycle $\xi$ can be realized as a map $\xi : K_\xi \to X$ as follows: realize $\xi$ as the formal linear combination $\sum_i \varepsilon_i e^i$ where $e^i$ are singular $n$-simplices and $\epsilon_i$ are $\pm 1$ (repeatation is allowed in the sum). Glue $n$-simplices appropriately according to how pair of singular $(n-1)$-simplices cancel in $\partial \xi = 0$ (there is a little subtlety here, but let's ignore that for now). We get the delta complex $K_\xi$ and the map $K_\xi \to X$ is obtained from sending each simplex to $X$ by the singular simplices $e^i : \Delta^i \to X$.
Thus, singular $n$-cycles are merely maps $\Delta \to X$ from delta complexes. Given two such maps $\xi : \Delta^n \to X$ an $\xi' : \Delta'^n \to X$, we see $\xi$ and $\xi'$ are homologous if $\xi$ and $\xi'$ extend to a map $\zeta : \mathbf{\Delta}^{n+1} \to X$ where $\partial \mathbf{\Delta}^{n+1} = \Delta^n \sqcup \Delta'^n$, i.e., $\zeta|_{\Delta^n} = \xi$ and $\zeta|_{\Delta'^n} = \xi'$. One can similarly check that this is an equivalence relation, and the equivalence classes of maps from certain delta complexes to $X$ forms precisely the $n$th singular homology group.
So homology between cycles in $X$ is merely (sort-of-)cobordism between simplicial complexes inside the ambient space $X$.


How can I include .pdf files when I publish but not in a search and replace in visual studio?

Question: More specifically my problem is if I include a pdf in my project and set its build action to content so it gets copied to the server when I publish, is there a way to exclude it from visual studios search and replace feature.  Would a post build action be a solution to this?  I just don't want anybody to do a global search and replace in my project and mess up the pdfs...
I know I just said a mouth full so let me know if you need clarification.  Thanks!!!
Answer: If you look in the section headed "Find options" in the find dialog, there is an option titled  "Look at these file types". If you qualify the type as *.cs, it will only look in files with that extension. You can also enter multiple file extensions. As long as you don't include pdf files in that filter, it will not apply to them. An alternative is to define a folder set to search in by clicking the ... next to the folder combo. Put your PDF files into a sub-directory of your project and exclude that folder from the folder set.
Additionally, as you have said, not including them in the project and using a post-build event to copy them to the output would also work.
I would qualify though that you are trying to solve a problem that shouldn't really be a problem. The onus is on the developer doing a find and replace to take care in doing so, and to not replace the contents of files they don't intend to change in the first place. Find an replace can be a dangerous tool.


Changing file name on upload

Question: I'm trying to automatically add prefixes to uploaded file names so they can later be processed by non-drupal scripts. I want to do that while the user is uploading files during creation/editing of a node.
To do that I am using hook_file_validate($field) where I check if the file is correct and then using $field->destination I define a new file name.
Surprisingly my issue lies in getting context to generate the proper file name (nid of the node,nid of the parent group, or info from other fields). When I upload a file, only part of the website is reloaded by javascript and hook_file_validate is triggered before hook_form_alter . Whene it is triggered I have no access to any data that would tell me where am I.
So far I have tried:
$_REQUEST['destination']

while defined (not a typo) when the whole form is loaded, it is undefined when the file field of the form is reloaded
$_GET['nid']

again - undefined
arg(0); arg(1); arg(2);

returns file ajax field_quest_file
which is not very usefull
I can access uid but it is not enough for me to create unique file names.
Comment: Files can be uploaded to nodes that have not yet been created so there won't necessarily be an nid at all, at least not at the point the file is uploaded. What about adding an additional submit handler to the form and doing the renaming in there once you know the node has been created?
Comment: Felix, the node belongs to a group so as a naming convention I want to use group_id+user_id+filename.I keep your option in mind but it will make my life complicated as I will get files with the same names which will be renamed by drupal, which will lead to issues with processing by server side scripts. I want the files to verified, processed and plotted while the user uploads them and before he submits the whole node.
Answer: Following on from my comment suggesting you do this in an additional submit handler something like this should work:
Add the new submit handler:
function misc_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'YOUR_CONTENT_TYPE_node_form') {
    $form['actions']['submit']['#submit'][] = 'rename_files';
  }
}

And then in the new submit handler:
function rename_files($form, &$form_state) {

  $node = $form_state['node'];

  global $user;

  // Loop over the uploaded files. Be sure to re-name field_files to the name of
  // your file field.
  foreach($node->field_files[LANGUAGE_NONE] as $item) {

    // Load the file object
    $file = file_load($item['fid']);

    // To set the destination first remove the filename from the stream wrapper
    // URI.
    $destination = str_replace($file->filename, '', $file->uri);

    // Modify this line to the name of your group field.
    $group = $node->field_group[LANGUAGE_NONE][0]['value'];

    // And then append the group, uid and put the filename back on the end.
    $destination .= $group.$user->uid.$file->filename;

    // And finally rename the file.
    file_move($file, $destination);
  }

}

Comment: @PawelG The URI will already have been passed through `file_create_filename()` to make sure it's unique, so `filename` and the filename part of `uri` will both be the same
Comment: I see one issue here. If drupal renames the file (so there are no 2 files with the same name) will the $file->filename change or will the change only be for $file->uri. Your script assumes that both will be renamed. As the original file name is important for me (for verification and plotting), if only uri gets renamed your code might actually do the trick for me.
Comment: Exactly like Clive said. There is also a third argument that can be passed into [file_move](https://api.drupal.org/api/drupal/includes!file.inc/function/file_move/7) that controls what happens when the destination file already exists.
Answer: Instead of renaming the files I used a different approach and change their destination folder.
I do it from the level of hook_form_alter where I have full access to POST/GET arguments and the values in other fields of the form.
function myModule_form_alter(&$form,&$form_state,$form_id){
    switch($form_id){
        case 'nodetype_node_form':
            //prepare whatever prefix you want
            //in my case gid/uid/
            $dest_folder = _make_destination_folder($form);
            //findout which file will be added next
            //the form is rebuild each time a file is uploaded
            $delta = form['field_files'][LANGUAGE_NONE]['#file_upload_delta'];
            //instead of renaming files just change the destination folder
            form['field_files'][LANGUAGE_NONE][$delta]['#upload_location'] = 'private://'.$dest_folder;
            break;
    }
}

This way files end up in an unique, non random and predictable place before the node is even saved.
A bonus of this approach is that I will have the files sorted in folders based on group and user

Changing file name on upload


Using Kotlin I want to programmatically change an icon in the bottom_nav_menu from an activity but am unable to see the menu item

Question: I am using Android Studio (Kotlin). In my res/menu folder I have a file called bottom_nav_menu.
<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/miPlayers"
        android:icon="@drawable/ic_person"
        android:title="Players" />
    <item
        android:id="@+id/miPigInfo"
        android:icon="@drawable/ic_pig_svgrepo_com"
        android:title="Pigs" />
    <item
        android:id="@+id/textinput_placeholder"
        android:title="Won" />
    <item
        android:id="@+id/miAbout"
        android:icon="@drawable/ic_info_white"
        android:title="About" />
    <item
        android:id="@+id/miSettings"
        android:icon="@drawable/ic_target_score_500"
        android:title="Target" />
</menu>

I want to access and change the icon programmatically in the last item miSettings from my MainActivity.kt. I am using viewBinding but if it is easier then I could use viewById.
Any help would be greatly appreciated.
Answer: just do like this
replace this id (R.id.miSettings) with your item id you want to change
bottom_nav_menu.menu.findItem(R.id.miSettings).setIcon(R.drawable.your_drawable_name);


In AR(1), why is $X_i|X_{i-1}=x_{i-1}\sim N(\alpha x_{i-1},\sigma^2)$?

Question: The AR(1) model starting with $X_0=0$ is
$$X_i=\alpha X_{i-1}+\epsilon_i, \ i=1,...,n, \ -1<\alpha<1$$
where $\epsilon_i\sim N(0,\sigma^2)$ are independent error terms.
Why then is $$X_i|X_{i-1}=x_{i-1}\sim N(\alpha x_{i-1},\sigma^2)?$$
Comment: Do you mean $X_i|(X_{i-1}=x_{i-1})\sim N(\alpha x_{i-1},\sigma^2)$ (mind the parentheses)?
Comment: @RichardHardy My reference gives it exactly as I've written.
Comment: I see. I was quite confused to see it, but once I read the answer of Francis, it seems that is the case. I do not say that parentheses there are necessary, but they may help visually. I think this is related to your comment under Francis answer, isn't it?
Answer: $X_i$ conditioned on $X_{i-1}=x_{i-1}$ implies $$X_i=\alpha x_{i-1}+\epsilon_{i},$$ i.e. a normal random variable $\epsilon_i$ adding a constant $\alpha x_{i-1}$. So $X_i\mid X_{i-1}=x_{i-1}$ is still a normal random variable, such that $$\operatorname{E}[X_i\mid X_{i-1}=x_{i-1}]=\operatorname{E}[\alpha x_{i-1}+\epsilon_{i}]=\alpha x_{i-1}+\operatorname{E}[\epsilon_i]=\alpha x_{i-1},$$ and $$\operatorname{Var}(X_i\mid X_{i-1}=x_{i-1})=\operatorname{Var}(\alpha x_{i-1}+\epsilon_{i})=\operatorname{Var}(\epsilon_{i})=\sigma^2.$$
Comment: And why is $X_i|X_{i-1}=x_{i-1}$?
Comment: @mavavilj: Notation $X_i\mid X_{i-1}=x_{i-1}$ means "distribution of $X_i$ given $X_{i-1}=x_{i-1}$", see Greenparker's answer as well.
Answer: I think the notation is confusing you here.
$X_i |X_{i-1} = x_{i-1} \sim N(\alpha x_{i-1}, \sigma^2)$ is another way of writing $X_i |(X_{i-1} = x_{i-1}) \sim N(\alpha x_{i-1}, \sigma^2)$. The equality is not meant to give a value for $X_{i}|X_{i-1}$, but meant to define a conditional distribution for when $X_{i-1}$ takes the realization $x_{i-1}$.
The notation $X_i |X_{i-1} = x_{i-1} \sim N(\alpha x_{i-1}, \sigma^2)$ means that conditional on the random variable $X_{i-1}$ taking value $x_{i-1}$, the distribution of $X_{i}$ is a normal distribution with mean $\alpha x_{i-1}$ and variance $\sigma^2$.


Running functional tests on a non-English site

Question: When creating functional tests (extending BrowserTestBase) for a D8 site, how would I go about changing the default language of the site being temporarily created by BrowserTestBase?
I'm trying to run tests on a site in a non-English language, and I'd like to avoid confusion by using English terms etc. in test cases.
As far as I see, Drupal/Core/Test/FunctionalTestSetupTrait.php sets the default langcode to be 'en' (in the installParameters method) and there's no option to specify custom installation parameters. Besides hacking this manually, is there a more elegant, "proper" way of changing this behavior?
If that's not possible, then would it be possible to change the site's language after it's created, maybe in the setUp method of the test class?
Answer: I think there are a few ways. The simplest might be to override the installParameters() method to specify your desired language:
protected function installParameters() {
  // This will come back with the langcode set to `en`.
  $parameters = parent::installParameters();

  // Update to desired langcode:
  $parameters['parameters']['langcode'] = 'es';

  return $parameters;
}

I didn't find any tests in core that did it this way, but there are a few that change it during setup via the UI. (This example from LanguagePathMonolingualTest)
// Make French the default language.
$edit = [
  'site_default_language' => 'fr',
];
$this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));


Determining if the set $\{e^{-x} : x\geq 0\}$ is compact or not.

Question: Decide if the set $\{e^{-x} : x\geq 0\}$ is compact or not. If not compact give a counter example.
Reasoning:
So it is known that if a set is compact it is closed and bounded. The two possible trouble points of this set are the endpoints. If $x = 0$ then $e^{-x} = 1$ this would serve as an upper bound and would also highlight that this limit point is  in the set. I'm having trouble with reconciling the other possible point $0$.
So $0$ is definitely a lower bound for this set, but I'm not sure whether I can conclude that it is in the set. The reason for this is that a set is considered closed if it contains all of its limit points. There is definitely a sequence converging to $0$, and I "feel" that $0$ "should" be considered in the set, but I'm having an issue with dealing with the limit operator on the sequence: $$\lim_{x \rightarrow \infty} e^{-x} = 0$$
Does the fact that I am taking the limit still allow me to arrive at the conclusion that $0 \in \{e^{-x} : x\geq 0\}$?
Comment: There is no such $x$.
Answer: $0$ is a limit point of the set but exponential is always positive so set is not closed.
Answer: If you think of the graph of $e^{-x}$, you'll see that $\{e^{-x} \mid x \geq 0\} = (0,1]$, which is not compact, as it is not closed.
Answer: $0\in \{e^{-x}:x\geq 0\} \iff \exists x\geq0:e^{-x}=0$. Since $\forall x: e^{-x}> 0$, we can conclude that 0 is not in the set.


I have a problem about the paginator helper in Cakephp

Question: I follow in example in book.cakephp and I try to search solution for it but not found
this it problem
<code>Warning (512): Method PaginatorHelper::option does not exist    [CORE\cake\libs\view\helper.php, line 154]
Code | Context
Helper::call__() - CORE\cake\libs\view\helper.php, line 154
Overloadable::__call() - CORE\cake\libs\overloadable_php5.php, line 50
PaginatorHelper::option() - APP\views\layouts\default.ctp, line 41
include - APP\views\layouts\default.ctp, line 41
View::_render() - CORE\cake\libs\view\view.php, line 731
View::renderLayout() - CORE\cake\libs\view\view.php, line 489
View::render() - CORE\cake\libs\view\view.php, line 435
Controller::render() - CORE\cake\libs\controller\controller.php, line 909
Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 207
Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 171
[main] - APP\webroot\index.php, line 83
</code>
minimize code User Controller and view
enter link description here
Thank for answer
Answer: It should be (api)
<code>$this->Paginator->options(...
</code>
instead of
<code>$this->Paginator->option(...
</code>
(note the s)
Hope it helps :)
Comment: hey next But it's doesn't work
can you tell solution to debug ajax in cake,pls
Comment: you have to set up both clientside and serverside debuggers.
on the serverside i've used xdebug (with eclipse) and on the clientside: firebug (in firefox) or google chrome's inspector

then you add breakpoints and start debugging
Answer: You don't have the AjaxHelper included in your code.


Adding a GET parameter to URL causes 404 error

Question: I'm trying to install the syntaxhighlightter evolved plugin to my wordpress blog. I've uploaded and activated the plugin, but it did not work. I've looked into the page source code and found out that the plugin style is loaded from the following URL:
http://devermind.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/styles/shCore.css?ver=2.0.320
This causes a 404 error (page not found). The strange thing though is that when I remove the GET parameters, the CSS loads ok:
http://devermind.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/styles/shCore.css
What could be causing this problem and how can I fix this? Unfortunately I don't know how to make wordpress drop the GET parameters when loading the stylesheet.
EDIT: As I just found out, this happens only in Firefox (3.0.11). IE loads both URLs above just fine. Not that this would be of any help though, so any suggestions would be appreciated.
SECOND EDIT: I tried this on my laptop and it works fine with Firefox 3.08. So this really seems to be a browser problem after all.
Comment: http://devermind.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/styles/shCore.css?ver=2.0.320 gives me 200 O...
Comment: You're right - It works fine, but not in Firefox
Comment: FWIW, it works fine with Firefox here.
Comment: Are you using the same Firefox version (3.0.11)? Also, Did you really use the faulty link (the first one above, not the second)?
Comment: Works with Safari 4, confirmed with curl as well.
Answer: Works with my Firefox version 3.0.11 and the link with the ver=2.0.320 parameter.
Did you check with your Firefox and disabling all extensions? Just create a new profile, that should disable them all. Check out the Mozilla knowledge base on how to create a new profile.
Comment: Bullseye! It's working fine when using another Firefox profile. Strangely enough, just launching it in safe mode does not help. Now I'll just have to troubleshoot my regular profile.
Comment: Check your server logs to see what the requested URL is.  This may give you a hint at what's going on in the browser.  I suspect something is encoding the URL and converting "?" to "%3f".
Answer: Just in case someone is still interested in this, in my case the problem was the HTTP proxy Fiddler, which seems to manipulate such URLs out of the box.
Turning off Fiddler capture solves the problem.


Sonar-Use try-with-resources or close this "Stream" in a "finally" clause java8 stream

Question: Sonar qube is giving me the following error
Use try-with-resources or close this "Stream" in a "finally" clause
List<Path> paths = find(Paths.get(nasProps.getUpstreamOutputDirectory() + File.separator + inputSource.concat("_").concat(contentGroup).concat("_").concat(parentId)),
                MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile() && filePath.getFileName().toString().matches(".*\\." + extTxt))
                .collect(toList());
paths.stream().forEach(path -> textFileQueue.add(path));

I dont have much understanding of java8. could you please help me to close the stream
Answer: Assuming find here is Files.find, what should work for you is
final Path startPath = Paths.get(nasProps.getUpstreamOutputDirectory() +
        File.separator +
        inputSource.concat("_").concat(contentGroup).concat("_").concat(parentId));
BiPredicate<Path, BasicFileAttributes> matcher = (filePath, fileAttr) ->
        fileAttr.isRegularFile() && filePath.getFileName().toString().matches(".*\\." + extTxt);

try (Stream<Path> pathStream = Files.find(startPath, Integer.MAX_VALUE, matcher)) {
    pathStream.forEach(path -> textFileQueue.add(path));
} catch (IOException e) {
    e.printStackTrace(); // handle or add to method calling this block
}

The reason why sonarqube is warning here is mentioned in the API note of linked document as well:

This method must be used within a try-with-resources statement or
  similar control structure to ensure that the stream's open directories
  are closed promptly after the stream's operations have completed.
Comment: Additionally, the code can be improved by not dealing with `File.separator` manually and using `+` instead of `concat`, i.e. `Paths.get(nasProps.getUpstreamOutputDirectory(), inputSource + "_" + contentGroup + "_" + parentId)`. Further, the test `.matches(".*\\." + extTxt)` looks very much like the intended operation is `.endsWith("." + extTxt)`.


Find Lipschitz constant for non differentiable function $f(t,y) = t y sin(|\alpha y|)$ $t\in[0, 1], y\in[-\pi,\pi]$

Question: Find a Lipschitz constant (in the y variable) for the following function on the given domain.
$f(t,y) = t y sin(|\alpha y|)$, $t\in[0, 1], y\in[-\pi,\pi]$,
$\alpha>0$ is a parameter and your Lipschitz constant should depend on $\alpha$. Your Lipschitz constant must be valid for all $\alpha$ but it does not need to be the smallest possible.
I know that a function can have lipschitz constant with out being differentiable.
Clearly this one is non differentiable at $y=0$. But i normally have only worked with either differenetiable functions or easier than this one. Any help appreciated.
Thanks
Comment: I dont understand the downvote is that such an ovious questions and would it hurt to give atleast a hint
Answer: There was extra info given as follows
If $f$ is Lipschitz continuous on an interval $I$ it means that there is a constant $L$, such that
$|f(u)-f(v)| \le L|u-v|$, $u,v \in I$
with the following properties:
1) If a function is continuously differentiable and the derivative satisfies $|f'| \le K$ on interval $I$, then $f$ is Lipschitz continuous on $I$ and $K$ can be taken to be a Lipschitz constant for $f$ on interval $I$.
2) If $f(y)$ has Lipschitz constant $L$, then $|f(y)|$ is also Lipschitz continuous on I with Lipschitz constant $L$.
3) If $f(y)$ and $g(y)$ are Lipschitz continuous on a closed interval $I$ with Lipschitz constants $L_1 , L_2$ respectively,
then $f(y)+g(y)$ is Lipschitz continuous on $I$ with Lipschitz constant $L_1+L_2$
4) If $f(y)$ and $g(y)$ are Lipschitz continuous on a closed interval $I$ with Lipschitz constants $L_1 , L_2$ respectively,and the absolute value of each of $f$ and $g$ has a maximum $M_1,M_2$ on $I$ respectively. Then $f(y) g(y)$ is Lipschitz continuous on I with Lipschitz constant is $M_1L_2+M_2L_1$
5) If $f:I_1 \mapsto I_2$ and $g:I_2 \mapsto I_3$ are Lipschitz continuous on a closed interval $I_1,I_2,I_3$ with Lipschitz constants $L_1 , L_2$ respectively, then the composite functions $g(f(y))$ is Lipschitz continuous on $I_1$ with Lipschitz constant $L=L_1L_2$
You can't use 1 as your function is non-differentiable.
However, consider first rule 5:
let $f=|\alpha y|$ then it's $L_1= \alpha$
let $g=sin(x)$ this is differentiable using rule 1 $L_2=1$ now using rule 5   $$g(f(y))=sin|\alpha y|$$ and its Lipschitz constant is $L=L_1L_2=\alpha$
Now, use rule 4:
let $f=ty$ then max $M_1=\pi$, and $L_1=1$
let $g=sin|\alpha y|$ as we already got $L_2= \alpha$  clearly $M_2=1$
Using rule 4, $$L=M_1L_2+M_2L_1=1+\pi \alpha$$
That's the final answer


MySQL stopped working - Error 2002 - Unknown Instance

Question: MySQL server was installed and was running just fine.  Today we can't connect to the database via our websites.  Command line access using mysql -u username -p also fails.  Finally, phpmyadmin fails.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

service mysql restart
stop: Unknown instance:
start: Job failed to start

Error logs (/var/log/mysql/error.log & /var/log/mysql.err & /var/log/mysql.log) are all empty for the past week.  The problem just began today.
I have looked at lots of answers including Ubuntu Error 2002 "Can't connect to local MySql server through socket…" and ERROR 2002 (HY000): Can't connect to local MySQL.
Should I just reinstall mysql server and chalk it up as an unknown error?  (horrible idea, obviously).
What is the right course of action to fix immediately and prevent from reoccurring?
Comment: Make sure you are not out of diskspace. That could kill your mysql. Also, make sure your sock file is deleted. I think my.cnf will tell you where it creates the mysql.sock file.
Comment: have you tried killing off any existing mysql processes with **kill**, and deleting the files under `/var/run/mysqld`?  Or maybe rebooting?
Comment: Rebooting did not help.  I have not tried kill nor delete.  One more point... I need to maintain all mysql databases and data.
Comment: Can you post the result of `ps aux | grep mysql`?
Comment: Added to question
Comment: Indeed I missed that the service was not running. What about `sudo mysqld --verbose --log-error=mysqld.err`? Post any output and/or the content of the `mysqld.err` file. Check also `ls -l /var/lib/mysql` (don't post this) and make sure that all files are owned by `mysql` both as user and group.
Comment: Added to question.  Ownership is mysql:mysql
Comment: Great points.  Diskspace was a concern but still had 10%.
Answer: I changed the bind-address from the IP to 127.0.0.1.  Still not sure why this worked and then all of a sudden stopped.  Here is the solution for others.
sudo nano /etc/mysql/my.cnf

change
bind-address = "127.0.0.1"

And then
sudo service mysql restart

Comment: Just one question why you restarted apache2 after changing my.cnf file?
Comment: Edited - did not restart apache, restarted mysql.  Sorry about the typo.  It is corrected.
Comment: my bind address has always been set to `127.0.0.1` and this is a regular occurance on more than 1 of my servers.


Module ldap_sso lost when updating module to the new version

Question: I am in a process of updating the drupal core and some of the modules (quite deactualized). One of them is ldap module, on this server I have no way to update through the drupal administration page, currently the installed version is ldap 7.x-1.0-beta12, the system will follow me to install the ldap version 7.x-1.0-beta15.
As I mentioned, the only way I have to update is manually, I have deleted the files from the server and I have placed the new ones with the version ldap 7.x-1.0-beta15, when I run the update (update.php) it appears that there is a missing module called ldap_sso, and effectively when I review the contents of the ldap module there is no internal module called ldap_sso, but in the ldap version 7.x-1.0-beta12 if it is.
How can I update the module in this case?
Thanks
Answer: I found the solution,
In the mentioned version of ldap, ldap_sso was removed to a separate project, I proceeded to compare the code in case there was any important difference and the result is that they are the same.
So I proceeded to download:
ldap_sso from https://www.drupal.org/project/ldap_sso/releases/7.x-1.0-beta15 and
ldap from: https://www.drupal.org/project/ldap/releases/7.x-1.0-beta15 (by suggestion of drupal)
Now these modules go separately and I ran the update and it did not give any problem.
regards


Mapping of zero vector under a linear transformation

Question: Let $T$ be a linear transformation from a vector space $V$ to $W$ that are defined over a field $\mathbb F$.
Now, $T(\mathbf0)=\mathbf0$.
Which implies the zero vector in $V$ can't move or be transformed to a new vector in $W$. Why is it so?
Does it also have a geometrical significance?
Comment: zero in the left hand side of the equation is the zero vector of $V$ and the zero vector in the right hand side is the zero vector of $W.$
Answer: Since you're asking for a geometrical interpretation:
First of all, if you have a linear transformation $T$ and use it on all vectors in a vector space $T$, then the mapped vectors will form a vector space again. What's important is that the zero vector is a part of every vector space.
So intuitively, when you map all vectors from a space $V$ to another space $W$, then there must be some vector that gets mapped to the zero vector. (As it has already been pointed out, it follows from the linearity criterion that $T(0)=0$, which means that this vector we are looking for is indeed the zero vector from $V$)
Let's visualise what a transformation $T: \mathbb R^2 \rightarrow \mathbb R^2$ does to our space. In this case, we are not moving from one vector space to another, but rather just mapping $\mathbb R^2$ onto itself. This leads to a distortion of the space.
For example, if we take the matrix
$$B = \begin{pmatrix}1 & 0 \\ 1 & 1\end{pmatrix}$$
and apply it to all vectors on the unit circle, the result will look like this:
Basically, what is happening is that we stretch and turn our space in different directions. Think of drawing a circle on a balloon and then, depending on your transformation, you just stretch out the surface.
It should be clear now, why the zero vector gets mapped to itself: If you stretch something that has $0$ length, then of course it will have no length afterwards either. You could again think of the balloon example and see for yourself that a small dot would not really change that much, no matter how much you stretch the balloon.
Answer: $T(x+0_V)=T(x) + T(0_V)$ for linearity.
But $x+0_V = x$ because $0_V$ is the neutral element in $V$ wrt vector addition.
So it must be $T(x) = T(x) + T(0_V)$.
It follows that $T(0_V)$ must be the neutral element in $W$ wrt vector addition, that is $T(0_V) = 0_W$


Sampling inclusion probability for multiple items

Question: Suppose we sample (uniformly, with replacement) $t$ times a set of $N$ items.
What is the probability $x$ that the sample contains $y$ different items?
This is representative of a real-life scenario I am facing, trying to determine if a certain issue impacts the whole population, or is specific to a certain subset. For example, I have a population of 73 specimens ($N$). One issue has manifested 250 times ($t_1$) on 47 different specimens ($y_1$), while another  has manifested 64 times ($t_2$) on 17 different specimens ($y_2$). Intuitively, issue 2 is much more likely to be specific to a subset of the specimens, but I would like to quantify this before trying to analyse further.
One way forward I struggled with for a while was to use the inclusion probability. There are formulas for including one item $x_k$ or two items $x_{kl}$ (below), but I couldn't generalize for $y$ items.
$$x_k = (1-\frac{1}{N})^t $$
$$x_{kl} = 2(1-\frac{1}{N})^t - (1-\frac{2}{N})^t $$
I also have a little simulation to get some taste of what the distribution would look like.
Any help or direction would be appreciated!
Answer: This is an example of the classical occupancy problem.  Let $1 \leqslant K \leqslant \min(t,N)$ denote the number of different items sampled.  Then the probability mass function for this random variable is the classical occupancy distribution:
$$\mathbb{P}(K=k) = \frac{(N)_k \cdot S(t,k)}{N^t}
\quad \quad \quad \text{for all } 1 \leqslant k \leqslant \min(t,N),$$
where $(N)_k = N(N-1)(N-2) \cdots (N-k+1)$ are the falling factorials and $S(t,k)$ are the Stirling numbers of the second kind.  Letting $E_r \equiv (1-r/N)^t$, the mean and variance of the distribution are:
$$\mathbb{E}(K) = N (1-E_1)
\quad \quad \quad \quad \quad
\mathbb{V}(K) = N [(N-1) E_2 + E_1 - N \cdot E_1^2].$$
The higher-order moments and other broader properties of this distribution are well-known (see e.g., O'Neill 2019).  For large values of $t$ and $N$ you can approximate it well by a normal distribution with identical mean and variance, with approximation accuracy shown in the cited paper.


How do you combine multiple expected values into 1 value?

Question: Let's say you have data with revenue from a car wash. You gain enough data to come up with expected values of revenue based on the color of the car and the style (coupe, sedan, or hatchback). That makes six separate expected values.
How do you combine expected values from these individual attributes into a single expected value? Is it simple addition?
Comment: You likely are referring to *conditional* expectations.  You're right that addition is incorrect.  After all, by simply collecting trivial information about each car--each of the digits of its VIN, for instance--you could obtain dozens or hundreds of "expected values," all of them close to the average revenue per car. Adding them would give you an "expected value" of tens or hundreds times the typical revenue.  If a little data collection always reaped such rewards, many more people would be scientists :-).  What you need is called [tag:multiple-regression].
Comment: @whuber To the extent that it can be answered in a reasonably brief answer I believe your comment covers the most salient points (particularly the guidance toward multiple regression, which I think is a suitable way to guide the OP -- toward widely used and useful but not completely general solutions). I don't even think a completely general answer should be attempted. What concerns me is you said about all there is to say in a good two paragraph answer and people may be discouraged from attempting an answer that doesn't add much to that, leaving us with more open but "unanswered" questions.
Comment: @whuber please consider converting that to an answer.
Comment: @Glen_b I cannot answer it generally and succinctly--except by saying "in general you cannot"--without knowing what these "expected values" actually represent or how they are estimated. One can imagine pricing schemes where the revenues *can* be computed from such partial information.
Answer: To get the total revenue, you add up the revenue from your datasheet. That's all. If you want to predict the revenue based on an imaginary population of cars, with a different distribution of colors and styles, you can take a weighted average of the conditional revenues based on those distributions. This method is related to indirect standardization.


An Error Occurred During Report Processing - Not Enough Memory Or Low Disk Space

Question: Working on Sql Report builder 3.0, all reports performed right but suddenly when I run the report an error window popup which shows the following error.
System.Web.Services.Protocols.SoapException: An internal error occurred on the report server. See the error log for more details.
Microsoft.ReportingServices.Diagnostics.Utilities.InternalCatalogException: An internal error occurred on the report server. See the error log for more details.

 System.IO.FileLoadException: Could not load file or assembly 'Microsoft.ReportingServices.ProcessingCore' or one of its dependencies. There is not enough space on the disk. (Exception from HRESULT: 0x80070070)

   at Microsoft.ReportingServices.WebServer.ReportingService2010Impl.CreateReportEditSession(String Report, String Parent, Byte[] Definition, String& EditSessionID, Warning[]& Warnings)

   at Microsoft.ReportingServices.WebServer.ReportingService2010.CreateReportEditSession(String Report, String Parent, Byte[] Definition, String& EditSessionID, Warning[]& Warnings)

Furthermore, I have given more 2 GB of free space to the log file and it will again surround all the remaining space. As I last viewed only 8.3 MB of free space remaining.
OS: Windows server 2003
Answer: As we have two recovery models.

Simple Recovery Model
Full / Bulk Logged Recovery Model

To my experience in this scenerio, most SQL Servers there is no backup of the transaction log. Full backups or differential backups are common practice, but transaction log backups are really seldom. So the transaction log file grows forever (until the disk is full). In this case the recovery model should be set to "simple". Don't forget to modify the system databases "model" and "tempdb", too.
A backup of the database "tempdb" makes no sense, so the recovery model of this db should always be "simple".
Process:
What i do on my database.
Right Click on properties -> Option ->  Set Recovery Model: Simple.
Then, right click -> Tasks -> Shrink -> Files.
Just thats it will make space.
But for better practise we have to set recovery model: Full, in it log files not grow more and more we have to backup the log files. And to understand this scenario better I suggest you see these free videos.
• SQL Server Backups Demystified
• SQL Server Logging Essentials


A bug in code about write a class that is same array of pointer?

Question: Hello to all i have below code
in PointArray.h
#ifndef POINTARRAY_H_INCLUDED
#define POINTARRAY_H_INCLUDED

template <typename T>
class PointArray
{
private:
    int nSize;
    T *array[];
public:
    PointArray();
    PointArray(const T *points[],const int size);
    PointArray(const PointArray &pv);
    ~PointArray();
    int * get(const int position);//is same to array[position]
    const int * get(const int position) const;//is same to array[position]
    const int getSize() const;//get size of array
};

#endif // POINTARRAY_H_INCLUDED

in PointArray.cpp
#include "PointArray.h"
#include<iostream>
#include <assert.h>
using namespace std;

template<class T>
PointArray<T>::PointArray(const T *points[],const int size)
{
    nSize=size;
    for (int i=0;i<size;i++)
    {
        array[i]=new T;
        *array[i]=*points[i];
    }
}

template<class T>
PointArray<T>::PointArray()
{
    nSize=0;
}

template<class T>
PointArray<T>::PointArray(const PointArray &pv)
{
    nSize=pv.getSize();
    for (int i=0;i<nSize;i++)
    {
        array[i]=new T;
        *array[i]=*(pv.get(i));
    }
}

template<class T>
const int PointArray<T>::getSize() const
{
    return nSize;
}

template<class T>
PointArray<T>::~PointArray()
{
    delete[] array;
    nSize=0;
}

template<class T>
int * PointArray<T>::get(const int position)
{
    assert(position>-1 && position<nSize);
    return array[position];
}

template<class T>
const int * PointArray<T>::get(const int position) const
{
    return array[position];
}

in main.cpp
#include<iostream>
#include "PointArray.h"
#include "PointArray.cpp"
using namespace std;

int main()
{
    int x=22;
    int y=3;
    const int * a[2]={&x,&y};
    PointArray<int> p;
    PointArray<int> p2(a,2);
    PointArray<int> p3(p2);
    cout << p.getSize() << endl;
    cout << p2.getSize() << endl;
    cout << p3.getSize() << endl;
    return 0;
}

above code should print
0
2
2

but output is
9244616
9244600
2

and when i run it again 9244616 , 9244600 is changed.
what is problem?
Comment: INitialize the nSize to 0 in constructor
Comment: @Abhijit Kadam  i dont get your sentence
Comment: I though you are not setting nSize but later saw in constructor body. Usually it is initialed in initialization list as against in body hence missed to notice. Strange it behave as if nSize is not set to proper value.
Comment: @MM-BB: there you go.. though I think you need to revisit your memory allocations and members structure
Comment: What is the size of `array` member? Where is it allocated? Your program invokes undefined behavior
Comment: @MM-BB rewrite your example with just the default constructed object, if that is wrong then you need to understand why. Go from there and use a debugger to understand what is actually happening when it is created - step through it...
Comment: @Tadeusz Kopec what( Where is it allocated)?
Comment: @ Caribou i test it by debuger but very dont get any thing
Comment: @MM-BB My mistake - `array` is not allocated. It is only deallocated in destructor (`delete[] array`). Here is one UB you invoke. Another one is accessing `array` elements. How many elements does it have?
Comment: @MM-BB I have all the original OP code (header, cpp and main) in main.cpp and I get 0 0 2 in vs2012, so it is likely that you are invoking undefined behavior somewhere. UB examples in this : http://stackoverflow.com/questions/367633/what-are-all-the-common-undefined-behaviour-that-a-c-programmer-should-know-ab
Answer: Your problem is
T *array[];

You define an array of pointers with no elements. Then you try to access its (nonexistent) elements which results in undefined behavior.
Another undefined behavior is in destructor
delete[] array;

You should only delete[] things that were new[]-ed. array wasn't. Instead you should loop over all array elements (if there were any) and delete each of them.
The best solution is to use std::vector instead of array. If you insist on using array, change T* array[] to T **array, in constructor allocate memory for it
array = new T*[size];

and in destructor delete all array elements before deleting array itself.
Answer: The problem was that you are including the cpp file in the main and implementing there, template implementations should be done in the h files.
Your h file should be:
 #ifndef POINTARRAY_H_INCLUDED
#define POINTARRAY_H_INCLUDED

template <typename T>
class PointArray
{
private:
    int nSize;
    T *array[];
public:
    PointArray()
    {
        nSize=0;
    }
    PointArray(const T *points[],const int size)
    {
        nSize=size;
        for (int i=0;i<size;i++)
        {
            array[i]=new T;
            *array[i]=*points[i];
        }
    }
    PointArray(const PointArray &pv)
    {
        nSize=pv.getSize();
        for (int i=0;i<nSize;i++)
        {
            array[i]=new T;
            *array[i]=*(pv.get(i));
        }
    }
    ~PointArray()
    {
        delete[] array;
        nSize=0;
    }
    int * get(const int position)
    {
        assert(position>-1 && position<nSize);
        return array[position];
    }
    const int * get(const int position) const
    {
        return array[position];
    }
    const int getSize() const { return nSize;}
};

#endif // POINTARRAY_H_INCLUDED

Code is still kind of hard and the allocations are bad(notice you get heap assert at the end of the process).. but it worked for me:
0
2
2
is printed...
Cheers.
(Notice that:)
delete[] array;

is a memory leak, since your object is not an array per say but you try to define pointers to arrays, you should change it to
T** array

and then at the destructor loop on the array first dimension and then do delete[] array
Comment: Could you clarify your answer a bit, what was wrong with the OP exactly? (Then I'll +1)
Comment: @Alon your answer is for better and code write the code but problem exist
Comment: Edited, thought I was clear, @MM-BB did you try putting it in the headers? it worked for me when I ran it
Comment: @Alon unedited code gives me 0 0 2 on vs2012 - it does however emit warnings
Comment: @Caribou yup it is because of the other thing I stated, the T* member[] is a warning, you cannot define it with zero size, it should be T**
Comment: @MM-BB from vs2012 : warning C4200: nonstandard extension used : zero-sized array in struct/union Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
Comment: @i ran it again and it work but it print  `12 [sig] PointArray 3424 open_stackdumpfile: Dumping stack trace to PointArray.exe.stackdump`
Comment: There's no difference between `typename` and `class` in template parameters.
Comment: That might be the memory allocations issues Ive stated, in the last section I try to give a clue
Comment: @Alon I haven't downvoted btw
Comment: Doesn't matter, re-editted it to a sentence, it solves the problem @DanHulme thanks for feedback, removed that note


e possível passar string da classe string como ponteiro para função?

Question: e possível passar string da classe string como ponteiro para função?
exemplo:
void separaStr(string *modulo, satring *nmodulo, *digito){}

Answer: Presumo que estejas a perguntar se é possivel passar para uma função, um apontador para uma std::string.
Sim, é possível. A assinatura da função seria, por exemplo:
int funcao(std::string *str)

No entanto e por norma, sugeria que, sempre que o argumento da função seja necessário (não possa ser null) e caso a função não guarde um apontador ou de qualquer outra forma altere a propriedade/posse (ownership) do argumento, substituisses o uso de um apontador pelo uso de uma referência.
O uso da referência permite, tal como com o apontador, alterar o valor da string e em termos de sintaxe a diferença no uso do parâmetro seria
str.membro (str é uma referência)

ao invés de
str->membro (str é um apontador)

Um dos beneficios do uso de uma referência é o facto de que a referência claramente expressa a condição de que o parâmetro da função não pode ser nulo.
Num projecto de maior dimensão isto poderá por exemplo, ajudar a facilmente identificar pontos no código onde é necessário verificar se o argumento é nulo e os casos onde efectuar esta validação é desnecessária.
A assinatura da função que recebe uma string passada por referência seria:
int funcao(std::string &str)

ou
int funcao(const std::string &str)

para os casos em que o parâmetro não seja alterado na função.
Uma máxima que tento seguir sempre que programo em C++ é:

Utiliza uma referência sempre que possível e um apontador apenas onde estritamente necessário.
Comment: muito obrigado, eu estive espanando 1bit por um '&'.


KL divergence of multinomial distribution

Question: Consider $q(x)$ be a Multinomial distribution over $\{1, \ldots, k\}$ with parameters $\{\theta_1,\ldots, \theta_k\}$. And p(x) over $\{1,\ldots, k\}$ with distribution $p(x)=\frac{1}{k}$. Then what is the KL divergence?
I know that for a discrete distribution, $KL(p(x)||q(x))=\sum p(x)\log\frac{p(x)}{q(x)}$. But as for the KL divergence on multinomial distributions, is the $x$ becomes a vector $\mathbf{x}$? Then what is the log of the ratio? and will the answer of the KL divergence also be a vector? Thanks so much.
Comment: Consider this note from Wikipedia:  "Note that, in some fields, such as natural language processing, the categorical and multinomial distributions are conflated, and it is common to speak of a "multinomial distribution" when a categorical distribution is actually meant... a categorical distribution is equivalent to a multinomial distribution over a single observation."  Do you think the lack of a trial parameter $N$ suggests this?
Answer: Well, let's review a few helpful definitions that will clarify how to calculate the Kullback-Leibler divergence here.
By definition the summation of the parameters of the mutlinomial distribution is 1; i.e., $$\sum_{m=1}^k\theta_m=1$$,
where $\theta_m$ is the probability of the $m^{th}$ outcome occuring.
The probability mass function (PMF) of the multinomial distribution is $$q(x)=\frac{n!}{\Pi_{m=1}^k(x_m!)}\Pi_{m=1}^k\theta_m^{x_m},$$
where $n$ is the total number of independent experiments executed such that $$\sum_{m=1}^kx_m=n$$.
Now let's also consider another multinomial distribution $p(x)$ as $$p(x)=\frac{n!}{\Pi_{m=1}^k(x_m!)}\Pi_{m=1}^k\left(\frac{1}{k}\right)^{x_m}=\frac{n!}{\Pi_{m=1}^k(x_m!)}\left(\frac{1}{k}\right)^{n}.$$
The resultant Kullback-Leibler divergence may then be calculated in a variety of equivalent statements $$D_{KL}(p(x)||q(x))= \sum_{m=1}^k \left(\frac{1}{k}\log\left(\frac{\frac{1}{k}}{\theta_m}\right)\right)=-\sum_{m=1}^k \left(\frac{1}{k}\log\left(k\theta_m\right)\right)=-\frac{1}{k}\log\left(k^k\Pi_{m=1}^k\theta_m\right)\\=-\frac{1}{k}\left(k\log(k)+\log(\Pi_{m=1}^k\theta_m)\right)=-\log(k)-\frac{1}{k}\sum_{m=1}^k\log(\theta_m)=\log\left(\frac{1}{k}\right)-\sum_{m=1}^k\frac{1}{k}log\left(\theta_m\right)=\sum_{m=1}^k\frac{1}{k}\log\left(\frac{1}{k}\right)-\sum_{m=1}^k\frac{1}{k}log\left(\theta_m\right)=-H\left(p(x)\right)-\mathbb{E}_{p(x)}[log(q(x))]\\=\mathbb{E}_{p(x)}\left[\log\left(\frac{1}{q(x)}\right)\right]-H(p(x)).$$
Notice that no vectors are required to calculate the Kullback-Leibler divergence between two multinomial distributions with the same number of categories.


Forecasting daily sales by handling multiple seasonality and zero sales in R

Question: I am trying to forecast sales for a retail store. The given data has daily sales information and also a dummy flag variable to indicate whether the store was open or not on that day. The sales is 0 when the store is closed. The range of dates is from 2013-01-01 to 2015-07-31 (2 years and 7 months). I have been asked to forecast sales for the next 48 days
store_530_ts <- ts(store_530[,-1], frequency = 7)
summary(store_530$Sales)

Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
0    3024    4338    4457    5681   12476
autoplot(store_530_ts[,1])

The resulting plot is :

The acf plot shows that there is both weekly and daily seasonality (Am I right about this one?)
acf2(store_530_ts[,1])

My Question is what is the best way to capture both the multiple seasonality and the holiday information in a single model?
I tried fitting a regression model with ARIMA errors with "Open" as a explanatory variable. I used lambda=0 to log transform the sales.
But I am getting the below error:
fit<-auto.arima(store_530_ts[, "Sales"], xreg = store_530_ts[, "Open"],
lambda=0)

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
NA/NaN/Inf in 'y

I understand that the error is because of the records with 0 sales. But, It does not feel right to simply ignore the 0 sales records from analysis.
Can someone tell me which is the best way to go about analyzing such time-series and what is that I am doing wrong. I am completely new to time-series analyis and any help would be much appreciated!
Comment: This one seems to me to be about statistics, not programming so I am voting to leave it open.
Answer: You have weekly and annual seasonality, but not daily (as you only see one observation per day).
I would use set the 0s to NAs, and then use a dynamic harmonic regression with Fourier terms for the weekly and annual seasonalities. (See https://otexts.org/fpp2/complexseasonality.html#dynamic-harmonic-regression-with-multiple-seasonal-periods for the details). You need to set seasonal periods to c(7,365). The forecasts can be adjusted to 0 on future days where the store is closed.
It looks like you have some high sales days, possibly associated with promotions. Perhaps use a dummy variable to model them.
Comment: Thank you very much Rob! I really appreciate it! May I ask how did you determine that it has annual seasonality? Also, the dataset I have has already a binary variable to indicate whether a promotion was run on that day or not. So, as per your answer I am thinking of fitting a harmonic regression model and add this binary promotion variable as predictor to the xreg matrix along with the fourier term. Would this work??
Comment: You have a periodic pattern with a peak around time 0, one at time 52, another at time 104. As you have set the frequency to 7, they correspond to peaks that are 52 weeks apart. Yes, adding the dummy to xreg along with fourier terms is what I was suggesting.
Comment: Thanks a lot! I have two more questions: 1. When replacing 0's with NA's, how to handle increasing variance with time? 2.I have another example of a different store where the sales tend to peak on particularly around last two weeks of the year ..say around 23rd dec, 30th dec and the promotion was applied on most of these days. Since this happened for consecutive 2 years, I expect that to happen in the next year also. I am thinking of extracting the week of the year and creating a dummy variable for week 51 and 52. and including fourier and promotion terms. do you have any suggestions?
Comment: This is not a general consulting forum. Please ask new questions if you have them.


How to integrate sendgrid templates with python app?`

Question: I want to integrate the template which I made on the Sendgrid Template Engine with my python application. I read the documentation and the source code on GitHub, but It give me error like this:
HTTP 400; JSON in headers is valid but incompatible
My source code is:
import sendgrid
import json
import os

mail_to = raw_input("Reciever's Mail(Name <email>): ")
subject = raw_input('Subject: ')
mail_from = raw_input("Sender Mail(Name <email>): ")

API_USER = os.getenv('API_USER')
API_KEY = os.getenv('API_KEY')

sg = sendgrid.SendGridClient(API_USER, API_KEY)

plaintext = \
    """
        This mail is only for testing.
    """
htmlbody = \
    """
      Html Data
    """

header_json = {
    'filters': {
        'templates': {
            'setting': {
                'enabled': '1',
                'template_id': '6967292-382-43aa-89dd-41fcd09b3fec'
            }
        }
    }
}

message = sendgrid.Mail(headers={'X-SMTPAPI': header_json})
message.add_to(mail_to)
message.set_subject(subject)
message.set_html(htmlbody)
message.set_text(plaintext)
message.set_from(mail_from)

status, msg = sg.send(message)

print "HTTP STATUS", status

msg = json.loads(msg)

if status == 400:
    print msg

Can someone tell me how to integrate my template with my app?
Answer: Template support is built into the SendGrid-Python library. From the README:
message = sendgrid.Mail()
message.add_filter('templates', 'enable', '1')
message.add_filter('templates', 'template_id', 'TEMPLATE-ALPHA-NUMERIC-ID')

It's also best to let the smtpapi-python library build the SMTPAPI header for you. This is actually transmitted as a POST parameter - not HTTP header. The name comes from before SendGrid's Web API existed and it was actually sent as an SMTP header.


One single balancer-manager page for multiple virtual hosts

Question: We're using currently an Apache 2.4 as load-balancer for a multi-master-cluster of three Tomcats-Servers. We can access the internal IP-Address of this load-balancer with an web-browser and change the settings on the fly (e.g. http://xx.xx.xx.xx/balancer-manager). This works and we're pretty fine with this.
Enabled modules:

proxy.load (proxy.conf used to configure)
proxy_ajp.load
proxy_balancer.load (proxy_balancer.conf used to configure)

But now we will host several (three in this setup) virtual hosts on this Apache, each representing a cluster of three Tomcats itself. Each is accessiable via an URL like http://customer.company.tld/app/ui). So the path is the same for each cluster! Now we're facing two problems:

The page balancer-manager is only reachable via it's virtual host. Therefore we have to access the balancer-manager via random choosen internal hostname (added to ServerAlias) and add this hostname also into the /etc/hosts off our internal computers to be be able to use it.
Furthermore we have to do this for each virtual-host (~cluster). But we want a single page balancer-manager which presents all virtual-hosts and the cluster behind.

Here some sample configuration:
/etc/apache2/site-enabled/foo # we have actually three of them: foo, bar, baz
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName customer-foo.company.tld
    ServerAlias customer-foo-balancer customer-foo.company.tld www.customer-foo.company.tld

    DocumentRoot /var/www

        <Directory />
                Options FollowSymLinks
                AllowOverride None
                Order allow,deny
                deny from all
        </Directory>
        <Directory /var/app/app_static>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
        <Directory /var/www/app_static_res>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

    # load balancer
        <Location /balancer-manager>
                 SetHandler balancer-manager
                 Order deny,allow
                 Deny from all
                 # allows for internal access
                 Allow from 127.0.0.1 ::1 10.1.21.81 10.1.4.9
                 Satisfy all
        </Location>

        ProxyRequests Off
        ProxyVia Off
        ProxyPreserveHost On

        <Proxy balancer://htg>
                ProxySet failonstatus=503
                BalancerMember ajp://10.171.23.120:8010/app lbset=0 route=foo001 loadfactor=40
                BalancerMember ajp://10.171.23.121:8010/app lbset=0 route=foo002 loadfactor=40
                BalancerMember ajp://10.171.23.122:8010/app lbset=0 route=foo003 loadfactor=20
        </Proxy>

        <Proxy balancer://htgservice>
                ProxySet failonstatus=503
                BalancerMember ajp://10.171.23.120:8011/wcs_service route=foo001 loadfactor=40
                BalancerMember ajp://10.171.23.121:8011/wcs_service route=foo002 loadfactor=40
                BalancerMember ajp://10.171.23.122:8011/wcs_service route=foo003 loadfactor=20
        </Proxy>

        ProxyPass        /app balancer://foo stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
        ProxyPassReverse /app balancer://foo

        ProxyPass        /app_service  balancer://fooservice stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
        ProxyPassReverse /app_service  balancer://fooservice

        LogLevel warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

In the past we configured the balancing inside the distinct files proxy.conf and proxy_balancer.conf, which should allow for accessing via the internal IP and show up all clusters and cluster-members on a single page.
But this won't work anymore, the configuration for proxies seem to accept only paths (e.g. app and app_service) not URLs or hostnames. We can't and won't change the paths. Therefore we moved the proxy-configuration inside the virtual hosts.
Thanks for your help!
Answer: What about splitting your config.
Put your balancer definition outside any virtual host and remove the context:
     <Proxy balancer://htg>
            ProxySet failonstatus=503
            BalancerMember ajp://10.171.23.120:8010 lbset=0 route=foo001 loadfactor=40
            BalancerMember ajp://10.171.23.121:8010 lbset=0 route=foo002 loadfactor=40
            BalancerMember ajp://10.171.23.122:8010 lbset=0 route=foo003 loadfactor=20
    </Proxy>

    <Proxy balancer://htgservice>
            ProxySet failonstatus=503
            BalancerMember ajp://10.171.23.120:8011 route=foo001 loadfactor=40
            BalancerMember ajp://10.171.23.121:8011 route=foo002 loadfactor=40
            BalancerMember ajp://10.171.23.122:8011 route=foo003 loadfactor=20
    </Proxy>

In your vhosts reference these balancers:
ProxyPass        /app balancer://htg/app stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
ProxyPassReverse /app balancer://htg/app

ProxyPass        /app_service  balancer://htgservice/wtg_service stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
ProxyPassReverse /app_service  balancer://htgservice/wtg_service

To access your balancer-manager, create an extra management vhost with
 <Location /balancer-manager> in it.
This way, you have one place where all your balancers are defined and one vhost to access the balancer-manager which shows you all your balncers.
Comment: Thanks. I will try that as soon as possible! Till now, I thought this kind of split isn't possible and assumend I can work around this with a weird kind of rewrite.
Comment: I found one drawback. It seems it is not working correctly with stickysessions enabled.
Comment: Perhaps, this is a solution to the sticky session problem: http://stackoverflow.com/a/9951315/252730
Comment: It is also discribed in this httpd bug report: https://issues.apache.org/bugzilla/show_bug.cgi?id=39304
Comment: Hi! I already hit this problem yesterday. The styicksessions must be defined with ProxySet inside the  and it works!
Answer: Thank you Christian! The solution of Christian is correct!
Basically you have to put ProxyPass and ProxyPassReverse inside the configuration for the Virtual-Hosts, and define the Proxy-Directive in the proxy.conf. As Christian already stated in his own comments, it is required use the ProxySet-Directive inside the Proxy-Directive to configure the StickySessions - it won't work with ProxyPass. Bellow is a short, but complete, example:
Sample VirtualHost (one for foo, bar, baz):
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName customer-foo.company.tld
    ServerAlias customer-foo-balancer customer-foo.company.tld www.customer-foo.company.tld

    DocumentRoot /var/www/foo

    <Directory />
        Options FollowSymLinks
        AllowOverride None
        Order allow,deny
        deny from all
    </Directory>
    <Directory /var/www/foo/wcs_static>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>
     <Directory /var/www/foo/wcs_static_res>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>

    # maintenance
    RewriteEngine On

    RewriteCond %{DOCUMENT_ROOT}/wcs_static_res/maintenance.html -f
    RewriteCond %{REQUEST_FILENAME} !/wcs_static_res/maintenance.html
    RewriteCond %{REQUEST_FILENAME} !/wcs_static_res/maintenance-time.png
    RewriteCond %{REMOTE_HOST} !^10\.1\.0\.203
    RewriteRule ^.*$    /wcs_static_res/maintenance.html [L]

    # redirect, SSL itself is handled by another server in front of this loadbalancer
    RedirectMatch (.*)/$  https://customer-foo.company.tld/wcs/ui

    # load balancer

    # web-frontend
    ProxyPass        /wcs balancer://foo
    # stickysession can't be set with ProxyPass in this case, see proxy.conf
    ProxyPassReverse /wcs balancer://foo

    # standalone-clients
    ProxyPass        /wcs_service balancer://fooservice
    ProxyPassReverse /wcs_service balancer://fooservice

    # logging
    LogLevel warn
    ErrorLog ${APACHE_LOG_DIR}/foo_error.log
    CustomLog ${APACHE_LOG_DIR}/foo_access.log combined
</VirtualHost>

proxy.conf (there is only this one):
<IfModule mod_proxy.c>

        ProxyRequests Off
        ProxyVia Off
        ProxyPreserveHost On

        <Proxy balancer://foo>
                # stickysessions must be defined here
                ProxySet failonstatus=503 stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
                BalancerMember ajp://10.171.23.120:8010/wcs lbset=0 route=foo001 loadfactor=40
                BalancerMember ajp://10.171.23.121:8010/wcs lbset=0 route=foo002 loadfactor=40
                BalancerMember ajp://10.171.23.122:8010/wcs lbset=0 route=foo003 loadfactor=20
        </Proxy>
        <Proxy balancer://fooservice>
                ProxySet failonstatus=503 stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
                BalancerMember ajp://10.171.23.120:8011/wcs_service route=foo001 loadfactor=40
                BalancerMember ajp://10.171.23.121:8011/wcs_service route=foo002 loadfactor=40
                BalancerMember ajp://10.171.23.122:8011/wcs_service route=foo003 loadfactor=20
        </Proxy>

        <Proxy balancer://bar>
                ProxySet failonstatus=503 stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
                BalancerMember ajp://10.171.21.110:8010/wcs lbset=0 route=bar001 loadfactor=40
                BalancerMember ajp://10.171.21.111:8010/wcs lbset=0 route=bar002 loadfactor=40
                BalancerMember ajp://10.171.21.112:8010/wcs lbset=0 route=bar003 loadfactor=20
        </Proxy>
        <Proxy balancer://barservice>
                ProxySet failonstatus=503 stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
                BalancerMember ajp://10.171.21.110:8011/wcs_service route=bar001 loadfactor=40
                BalancerMember ajp://10.171.21.111:8011/wcs_service route=bar002 loadfactor=40
                BalancerMember ajp://10.171.21.112:8011/wcs_service route=bar003 loadfactor=20
        </Proxy>

        <Proxy balancer://baz>
                ProxySet failonstatus=503 stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
                BalancerMember ajp://10.171.45.100:8010/wcs lbset=0 route=baz001 loadfactor=40
                BalancerMember ajp://10.171.45.101:8010/wcs lbset=0 route=baz002 loadfactor=40
                BalancerMember ajp://10.171.45.102:8010/wcs lbset=0 route=baz003 loadfactor=20
        </Proxy>

        <Proxy balancer://bazservice>
                ProxySet failonstatus=503 stickysession=JSESSIONID|jsessionid nofailover=Off scolonpathdelim=On
                BalancerMember ajp://10.171.45.100:8011/wcs_service route=baz001 loadfactor=40
                BalancerMember ajp://10.171.45.101:8011/wcs_service route=baz002 loadfactor=40
                BalancerMember ajp://10.171.45.102:8011/wcs_service route=baz003 loadfactor=20
        </Proxy>
</IfModule>

proxy_balancer.conf:
<IfModule mod_proxy_balancer.c>
        <IfModule mod_status.c>
        <Location /balancer-manager>
                SetHandler balancer-manager
                Order deny,allow
                Deny from all
                # store here your internal IPs for direct access
                Allow from 127.0.0.1 ::1
                Satisfy all
        </Location>

</IfModule>

Now you can change the configuration through http://ipofloadbalancer.com/balancer-manager for all servers behind your proxy. No need to hack with /etc/hosts on the clients and the loadbalancer.
Drawback, especially with the Apache 2.4, the interface gets quickly cluttered and you have to scroll much.


Using pointers vs. address-of operator while copying linked lists in C++

Question: I made the following linked list structure and printList function. Both functioned correctly:
struct Node{
    int data;
    Node *np;
};

void printList(Node *x){
    cout << x->data << " ";
    if (x->np != NULL){
        printList(x->np);
    }
    return;
}

Then I decided to write a recursive function to copy over a linked list. One implementation, returning a pointer value, works...whereas the other, returning an address-of didn't work...I can't for the life of me figure out why this is the case:
This works:
Node * copyList(Node *x){

    Node * y = new Node;
    y->data = x->data;
    if (x->np != NULL){
        y->np = copyList(x->np);
    }else{
        y->np = NULL;
    }
    return y;
}

This doesn't work:
Node * copyList(Node *x){
    Node y = {x->data,NULL};
    if (x->np != NULL){
        y.np = copyList(x->np);
   }
   return &y;
}

I'm a little confused as to why. I would assume that given that a pointer essentially refers to a memory address, returning &y would be just as fine...
Comment: Depending on your compiler, if you turn warnings on it should be able to tell you exactly why this isn't working.
Answer: In the second case the Node y object you are creating will go out of scope when the function call will end. The address you are returning will then be not valid.
Answer: As soon as copyList exits, all of its local variables are destroyed; there no longer exists a Node object at the location pointed to by the pointer you return. That memory will likely be used for some other purpose the next time you call a function.
Answer: The first function is also invalid because in general the argument that is x can be equal to NULL. So you have undefined behaviour in statement
y->data = x->data;

A correct function can look like
Node * copyList( const Node *x )
{
    if ( x == NULL )
    {
        return NULL;
    }
    else
    {
        Node *y = new Node { x->data, copyList( x->np ) };
        return y;
    }
}

Or even like
Node * copyList( const Node *x )
{
    return ( x == NULL ) ? NULL : new Node { x->data, copyList( x->np ) };
}

The same problem exists with function printList. It should be defined like
void printList( const Node *x )
{
    if ( x == NULL )
    {
        std::cout << std::endl;
    }
    else
    {
        std::cout << x->data << ' ';
        display( x->np );
    }
}

As for the second function then apart from this error it returns pointer to a local variable that after exiting the function becomes invalid because the local variable will be deleted.


MongoDB.Drive falha ao filtrar com ElemMatch um Array de objeto

Question: Tenho um Document no mongoDB com essa formação:
{
    "_id" : NumberLong(21),
    "_t" : "Detail",
    "DeletionDate" : null,
    "CreationDate" : null,
    "UpdateDate" : null,
    "Accounts" : [
        {
            "Bank" : "123",
            "BankAccount" : "123456"
        }
    ]
}

e estou tentando fazer uma consulta filtrando pela propriedade Bank dentro de Accounts
minhas classes são:
Atualizada em 06/08/2020
IDetail.cs
public Interface IDetails
{

     long? Id { get; set; }

     DateTime? DeletionDate { get; set; }

     DateTime? CreationDate { get; set; }

     DateTime? UpdateDate { get; set; }

     List<SpecificAccount> Accounts;

}

Detail.cs
[BsonDiscriminator("Details")]
public class Details : IDetails
{

     public virtual long? Id { get; set; }

     [BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
     public DateTime? DeletionDate { get; set; }

     [BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
     public DateTime? CreationDate { get; set; }

     [BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
     public DateTime? UpdateDate { get; set; }

     private List<SpecificAccount> _accounts;

     public List<SpecificAccount> Accounts {
         get
         {
             if (_accounts == null)
                 return new List<SpecificAccount>();
             return _accounts;
         }
         set { _accounts = value;  }
     }

}

SpecificAccount.cs

[BsonIgnoreExtraElements]
public class SpecificAccount
{
    public string Bank { get; set; }

    public string BankAccount { get; set; }
}

e a consulta está sendo feita dessa maneira:
...
var filter = Builders<IDetails>.Filter.And( Builders<IDetails>.Filter.Eq("_id", id) );

if (bank != null)
    filter &= Builders<IDetails>.Filter.ElemMatch(x => x.Accounts, y => y.Bank.Equals(bank));

return Collection.Find(filter).FirstOrDefault();

mas estou obtendo uma exceção ao rodar o código em tempo de execução:
Message:
Test method Tests.AccountServiceTests.TestShouldGetAccount threw exception:
System.InvalidOperationException: Unable to determine the serialization information for x => x.Accounts.
Stack Trace:
ExpressionFieldDefinition1.Render(IBsonSerializer1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
ElementMatchFilterDefinition2.Render(IBsonSerializer1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
AndFilterDefinition1.Render(IBsonSerializer1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
MongoCollectionImpl1.CreateFindOperation[TProjection](FilterDefinition1 filter, FindOptions2 options) MongoCollectionImpl1.FindSync[TProjection](IClientSessionHandle session, FilterDefinition1 filter, FindOptions2 options, CancellationToken cancellationToken)
<>c__DisplayClass41_01.<FindSync>b__0(IClientSessionHandle session) MongoCollectionImpl1.UsingImplicitSession[TResult](Func2 func, CancellationToken cancellationToken) MongoCollectionImpl1.FindSync[TProjection](FilterDefinition1 filter, FindOptions2 options, CancellationToken cancellationToken)
FindFluent2.ToCursor(CancellationToken cancellationToken) IAsyncCursorSourceExtensions.FirstOrDefault[TDocument](IAsyncCursorSource1 source, CancellationToken cancellationToken)
IFindFluentExtensions.FirstOrDefault[TDocument,TProjection](IFindFluent`2 find, CancellationToken cancellationToken)
Até achei que podia ser algo no registerClassMap mas não adiantou
BsonClassMap.RegisterClassMap(cm => {
           cm.MapProperty(c => c.Accounts);
});

Alguém pode me dar uma luz?
Comment: direto no mongo seria: **db.getCollection('Detail').find({ Accounts: { $elemMatch: {"Bank" : "123"}} })**
Comment: Bem provável que é por causa daquele _getter_.
Comment: Eu havia esquecido de adicionar a interface IDetail mas no Find estava usando ela. quando removi essa interface e usei somente a classe resolveu o problema.
Answer: Depois de várias tentativas e erros, descobri que quando removia  a interface IDetail.cs do Find e passava a utilizar a Classe Detail.cs a consulta funcionava. ficando a consulta dessa maneira:
...
var filter = Builders.Filter.And( Builders.Filter.Eq("_id", id) );

if (bank != null)
    filter &= Builders.Filter.ElemMatch(x => x.Accounts, y => y.Bank.Equals(bank));

return Collection.Find(filter).FirstOrDefault();


spring JPA query with spel expression for random

Question: I am trying to generate a random number so I can do sampling in a data set in a JPA query. I have read this is totally possible everywhere but I keep getting errors:
code:
@Query("select t from Telemetry t where t.id = :id and t.updateTimestamp between :startTime and :endTime and #{ #T(java.lang.Math).random() } < :samplingRate ")
Iterable<Telemetry> findFor(@Param("id") String org, @Param("startTime") Instant startTime, @Param("endTime") Instant endTime, @Param("samplingRate") Float samplingRate);

error:

org.hibernate.QueryException: unexpected char: '#' [select t from
  com.mz.rad.dao.Telemetry t where t.id = :id and t.updateTimestamp
  between :startTime and :endTime and #{ #T(java.lang.Math).random() } <
  :samplingRate ]

Digging deeper into this I discover this exception:
throw new IllegalArgumentException("Parameter with that position [" + parameterPosition + "] did not exist");

parameter position is 1 or 2 Depending on which syntax I use.
I have tried with with/without hashes. No luck. I am using postgres which has a random() function but it doesn't like me invoking that either (which is my ultimate goal).
This question is about using SPeL, but I just need a random number
Answer: You can try like this :#
select t from Telemetry t where t.id = :id and t.updateTimestamp between :startTime and :endTime and :#{T(java.lang.Math).random()} < :samplingRate

If not then use ?#
select t from Telemetry t where t.id = :id and t.updateTimestamp between :startTime and :endTime and ?#{T(java.lang.Math).random()} < :samplingRate

Comment: I gave you an upvote but found my own answer. Need to pom bump too
Answer: Answer:
@Query("select t from Telemetry t where t.id = :id and t.updateTimestamp between :startTime and :endTime and :#{T(java.lang.Math).random()} < :samplingRate ")

Helps to bump your POM
   <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.10.5.RELEASE</version>
    </dependency>

Comment: Yeah, I got it working .. now it randomly returns ALL or NO result `samplingFactor` times :-/ new question


Key-value mapping of axis/variable labels in ggplot

Question: I often work with dataframes with "R-friendly"/"programmer-friendly" column names, typically with no spaces, and/or abbreviated (lazy to type the full names when doing the analysis). For example:
ir <- data.frame(
   sp=iris$Species,
   sep.len=iris$Sepal.Length,
   sep.wid=iris$Sepal.Width,
   pet.len=iris$Petal.Length,
   pet.wid=iris$Petal.Width
)

When I plot these with ggplot I often want to replace the labels with "user-friendly" column names, e.g.
p <- ggplot(ir, aes(x=sep.len, y=sep.wid, col=sp)) + geom_point() +
  xlab("sepal length") + ylab("sepal width") +
  scale_color_discrete("species")

Question: Is there some way to specify label mappings to pass into ggplot ?
lazy.labels <- c(
  sp     ='species',
  sep.len='sepal length',
  sep.wid='sepal width',
  pet.len='petal length',
  pet.wid='petal width'
)

And do something like
p + labs(lazy.labels)

or even
p + xlab(lazy.labels[..x..]) + ylab(lazy.labels[..y..])

where ..x.., ..y.. is some automagic ggplot variable holding the name of the current X/Y variable? (then I can put these annotations into a convenience function without having to change them for each graph)
This is particularly useful when I make many plots in reports. I can always rename ir with the "user-friendly" columns, but then I have to do a lot of
ggplot(ir, aes(x=`sepal length`, y=`sepal width`, ...

which is a bit cumbersome because of all the spaces.
Comment: Is this something you're looking for https://stackoverflow.com/a/49943976/786542
Comment: Few more: https://stackoverflow.com/a/50930640/786542 & https://stackoverflow.com/a/50522928/786542
Comment: @Tung that's a similar but not the same issue - that's to do with saving the variable name like `x.var <- 'sep.len'` and having ggplot interpret the variable name correctly as `sep.len` not `x.var` (`aes_string` can solve that problem for me).
Comment: I want to change the title to say "axis/variable titles" instead of "labels". Yes, the `labs()` function implies that "labels" is an appropriate word choice, but that function is probably not how most users are introduced to this graph component. When using `scale_*()`, the relevant argument is `name`, while the `labels` argument instead chooses labels for the breaks. When using `theme()`, the relevant argument is `axis.title.*`. I think it is best to use the terminology from `theme()` because it has a set of unique arguments corresponding to a nearly-exhaustive set of graph components.
Answer: I digged into the ggplot object and came up with this: the benefit is that you do not need to know the mapping in advance
library(ggplot2)

ir <- data.frame(
  sp = iris$Species,
  sep.len = iris$Sepal.Length,
  sep.wid = iris$Sepal.Width,
  pet.len = iris$Petal.Length,
  pet.wid = iris$Petal.Width
)

p <- ggplot(ir, aes(x=sep.len, y=sep.wid, col=sp)) +
     geom_point() +
     scale_color_discrete("species")

## for lazy labels

lazy.labels <- c(
  sp     ='species',
  sep.len='sepal length',
  sep.wid='sepal width',
  pet.len='petal length',
  pet.wid='petal width'
)

p$labels <-lapply(p$labels,function(x){as.character(lazy.labels[x])})

Or, using a function:
plot_with_labels <- function(p, l) {
  p$labels <- lapply(p$labels, function(x) { as.character(l[x]) } )
  return(p)
}

plot_with_labels(p, lazy.labels)

Comment: Aha, `p$labels`! Thanks very much!
Comment: as of ggplot2 3.3.5, the function above sets to `NA` labels with the `fallback` attribute. This fixes that: `plot_with_labels_mod <- function(p, l) {

  swap <- function(x) {
    if (is.null(attr(x, "fallback"))) {
      as.character(l[x])
    } else {
      x
    }
  }

  p$labels <- lapply(p$labels, swap)
  return(p)
}`
Answer:
One solution if your graph are always the same is to create labels in advance using a list mapping aesthetics to your new names to replace the default one. Then you can
use labs(lazy.labels).
ir <- data.frame(
  sp = iris$Species,
  sep.len = iris$Sepal.Length,
  sep.wid = iris$Sepal.Width,
  pet.len = iris$Petal.Length,
  pet.wid = iris$Petal.Width
)
library(ggplot2)
# mapping aesthetics names to labels
lazy.labels <- list(
  col = 'species',
  x = 'sepal length',
  y ='sepal width'
)
p <- ggplot(ir, aes(x = sep.len, y = sep.wid, col = sp)) +
  geom_point() +
  labs(lazy.labels)

Created on 2018-07-09 by the reprex package (v0.2.0).
Comment: Thanks! Unfortunately I do not know my labels in advance - I typically will make a lot of plots to explore various effects with different X, Y, colour


Jacobian factor for orthogonal decomposition with respect to vector

Question: Let $z\in S^2$. Fix vector $v\in S^2$. Then we can write $z=\langle z,v\rangle v+ \sqrt{\left(1- \langle z,v\rangle^2\right ) } v^{ \perp} $ where $v^{\perp}\in S^2$ such that $v$ and $v^{\perp}$ are orthogonal. Let $a= \langle z,v\rangle  $
We need to change variable from $z\to (a,v^{ \perp})$, then $dz= J(a,v^{\perp})dadv^{\perp}$. I am interested in finding the jacobian factor $J$. Is there any way to find that? I tried hard. But I could not get it.
Any help or hint will be highly appreciated.
Thanks
Comment: what is $dz$? area form on the sphere? Also, how can $z\mapsto (a,v^\top)$ be a change of variables if $z\in S^2$, $v^\top$ is fixed (as $v$ is fixed) and $a$ is a scalar?
Comment: v^T can be any vector which is perpendicular to $v$. It can vary over circle
Comment: We need to integrate over sphere that measure is $dz$
Comment: You said $v$ is fixed, so how does $v^\perp$ vary with $z$?
Comment: V perp is notation . There is can vary over circle
Answer: Let me work with Euclidean coordinates,
$$
z=(x_1,x_2,x_3)\in S^2.
$$
There certainly exists an orthogonal transformation $L:\mathbb R^3\to\mathbb R^3$ such that $Lv=(1,0,0)$. Fix such an $L$ and denote $Lz=(x_1',x_2',x_3')$.
The coordinate transformation is $z\mapsto (a,t)$ where
$$
a=\langle z,v\rangle=\langle Lz,Lv\rangle = x'_1
$$
and
$$
(\cos t,\sin t)=\frac 1{\sqrt{1-a^2}}(x_2',x_3')=v^\perp.
$$
(Whenever $a\not=1$, i.e. for all $z\not=v$.)
Let us use also the spherical coordinates
$$
x_1'=\cos\theta,\ \
x_2'=\sin\theta\cos\phi,\ \
x_3'=\sin\theta\sin\phi.
$$
Hence, $a=\cos\theta$, $\sin\theta\mathrm d\theta=\mathrm da$, $t=\phi$. The area form $\mathrm dz$ is
$$
\mathrm dz = \sin\theta \mathrm d\phi\,\mathrm d\theta = \mathrm da\, \mathrm dt.
$$
(Here we use that $L$ is an isometry of $\mathbb R^3$ hence it preserves the area form on the sphere $S^2$.)
The coordinates you are describing are just rotated spherical coordinates. I am using the coordinate $t$ to describe the vector $v^\perp$.


WPF Static Background Image

Question: How do you get a static background image in WPF.  With an image set using the imagebrush it always expands or contracts when I resize or extend my window.  How do I make the image static so when I extend a window more of the image is revealed and not resized?  ie - similar to web design css or something like that.
Answer: set ImageBrush property Stretch to None (default is Fill).
<Window.Background>
    <ImageBrush Stretch="None" Source="src"/>
</Window.Background>

Might also need to anchor it to a corner (probably top left) using AlignmentY & AlignmentX.
<Window.Background>
    <ImageBrush Stretch="None"
                Source="src"
                Viewbox="0.25, 0.25, 1, 1"
                AlignmentY="Top"
                AlignmentX="Left"/>
</Window.Background>

Comment: Nope, background still moves when window is resized.
Comment: Got it.  you have to anchor it to a corner (probably the top left) using Alignment and then you can use Viewbox to adjust the image.  Added to answer.

WPF Static Background Image


Returning and Accessing a Multidimensional Array in C++

Question: Function getSalon() returns a two-dimensional boolean array.
bool** getSalon()
{
    return salon;
}

When I try to access it's values that doesn't seems to work.
someObject.getSalon()[i][k];

But if I write it in this way, I get the first element.
someObject.getSalon();

How can I access this arrays' all elements properly?
Comment: Thank you for your help, it helped me to save the problem. The program was closing itself. I made the declaration wrong.
Answer: operator[] has higher precedence than operator..Try this:
(someObject.getSalon())[i][k];


When does Dose Response Function estimation work better than simple regression?

Question: I have been recently asked what is the difference between a Dose Response Function (DRF) estimation (as the one proposed in this link and this paper) and a statistical regression method. I therefore tried to create some toy synthetic examples in order to show some cases in which regression methods fail to estimate the impact of the treatment while DRF estimation had this ability. But I wasn't able to find any.
In particular, I created this notebook to test the two cited methods in comparison to a linear regression (LR) and found the following:

In an unconfounded and without colliders scenario, nothing is as accurate as the regression
DRF estimation methods are biased/not as accurate as I was expecting
If I include a collider in the analysis confusing it as a confounder, then the DRF estimation methods are (still biased but more) robust than a LR

Given those findings, I have a few questions.

What's the point of using those methods if in a scenario without colliders traditional regression methods works better? (Of course, if the user does not think to be in this case he/she can perform an analyses to remove colliders)
Is there any case in which DRF estimation methods are less biased than regression methods?

Feel free to download and play with my notebook, some other notebook examples would be greatly appreciated, thanks
Comment: I'm not sure whether your question is focusing on what you are actually interested in (propensity scores / inverse probability weights?). A dose-response function is simply...a function. You can estimate its parameter values using regression techniques. In fact, logistic regression is often used for this in toxicology: https://en.wikipedia.org/wiki/Dose%E2%80%93response_relationship .
Comment: Hi mkt, thanks for your comment! I am referring to a causal dose response function, as the function that maps continuous treatments T into continuous potential outcomes Y(T). In the Hirano, Imbens paper they refer to this as dose response function so I kept calling it in this way
Answer: First, don't think about the colliders situation. Both methods are equally ill-equipped to handle colliders and none are more robust to conditioning on colliders than others. That is not the sense in which propensity score methods are designed to be more robust. You might have heard that IPW is able to handle post-treatment colliders and regression can't, but that is only in the case of sequential treatment effect estimation with time-varying confounders, which are also colliders. In a single time point study like this, colliders are equally toxic for both methods and the difference you saw is likely due to the specific data-generating model and not to an inherent difference between the two methods that one would expect to hold more broadly.
I'm not exactly sure what you mean by "a statistical regression method". When the estimand is the average dose-response function (ADRF), you can use regression in the form of g-computation to estimate it. You can also use propensity score methods to do so instead, or combine the methods.
G-computation involves fitting a regression model for the outcome given the treatment and covariates. From that model, you generate predictions for outcome under every possible value of the treatment, and for each value, you compute the mean prediction across all units in your sample. That outcome model needs to be correct for the data-generating process in order for the ADRF to be consistently estimated. A problem with this approach is that not only do you need to allow flexibility in the relationship between the treatment and the outcome, but you also need to flexibly model the relationships between the covariates and the outcome and their interactions with treatments. That can lead to huge models that are impossible to fit, even with just a few covariates, and the method is not robust to misspecification of this model. For example, if the relationship between the outcome and the treatment is curvy, and the relationship between the outcome and a covariate is curvy, and the relationship between the outcome and treatment changes based on the covariate in a curvy way, then modeling any of these relationships as linear will yield a biased ADRF, even if you have correctly include all variables required to remove confounding.
Weighting allows you to avoid some of these issues; importantly, you don't need to model the relationship between the covariates and the outcome or the interactions between the covariates and treatment in the outcome model. You do still need to get the relationship between the treatment and outcome correct, but that is much easier than getting the entire data-generating model correct. The tradeoff is that you still need to correctly estimate the weights and you may have a decrement in precision using weights. To model the weights, you need to correctly model the conditional density of the treatment given the covariates. For propensity score methods with a binary treatment, the density is determined by a single parameter, the mean (i.e., the probability of getting treated), so that is much easier to do. Here, you need to correctly model the whole density. Usually we just model the mean of the density and make a strong assumption about its shape (including that its shape is constant), but this can yield bias. An alternative is to use a weighting method that avoids modeling the treatment like distance covariance optimization weights (DCOWs), which directly estimate weights to minimize imbalance without a treatment model.
Your toy examples could not hope to express the realistic situations in which these two approaches would differ. Below is a list of scenarios in which you would expect weighting to perform better than regression-based g-computation:

The outcome model is highly complex and curvy in the covariates (it can be hard to specify a well-fitting outcome model that accounts for this)
The individual dose-response functions vary a lot (weighting averages over the individual dose-repsonse functions without requiring modeling this heterogeneity)
The outcome model involves many covariates (weighting is less sensitive to overfitting the treatment model and it is easier to prevent overfitting without impacting inference than it is to do so in the outcome model)
The treatment is straightforward to model, but not linear (if it were linear, adjusting for linear terms in the outcome model would be enough even if the rest of the model were misspecified)
You have a large sample (g-computation for a large sample can be computationally intensive, and the precision benefits one gets from g-computation are diminished with larger samples)

I think it is noble to want to explore the differences in performance in toy examples that are easy to understand and control, but these methods were designed for more complicated cases. Simulations with simple data-generating models tend to prefer simple methods that make strong (but incidentally correct) assumptions. It is important to consider how these methods were designed to be used in practice, which often involves complex data-generating processes that aren't very revealing on their own. I recommend the simulations in Huling et al. (2023), which introduces the DCOWs and compares them to other weighting methods (and of which I am an author). While we don't compare regression and weighting, we do provide a potentially realistically complex data-generating model that is able to distinguish among many flexible weighting methods.
References
Huling, J. D., Greifer, N., & Chen, G. (2023). Independence Weights for Causal Inference with Continuous Treatments. Journal of the American Statistical Association, 0(0), 1–14. https://doi.org/10.1080/01621459.2023.2213485
Comment: A "naive" estimation works only when the outcoem model is linear. Most models are nonlienar (e.g., logistic regression, Poisson regression, general machine learning models) so you would not expect them to be equal in most cases. A linear model is special in that $E[E[Y|T, X]] = E[Y|T, X = \bar{X}]$. That is not something that should generally assumed to be true.
Comment: [Snowden et al (2011)](https://doi.org/10.1093/aje/kwq472) provides an intro for binary treatments. [Austin (2019)](https://doi.org/10.1002/sim.7615) explains how to use it for estimating the ADRF of a continuous treatment.
Comment: Thank you Noah for the great answer! I still feel like point 2 has not been totally addressed, but probably it's because my question was a bit fuzzy. Can you provide any example in which estimating the DRF using the methods you described leads to correct results while a naive estimation (such as one that estimates $E[Y|T,E[X]]$) does not? I tried to ask this also in this question https://stats.stackexchange.com/questions/642620/example-in-which-eeyt-x-eyt-ex
Comment: Lastly, I get your point that these methods are designed for more complicate scenarios but I also feel like toy examples allow us to bridge the gap between scientific and industrial world, as we could use them to explain causality concepts to people with just a ML background which would be great! Conversely, it will always be fuzzy for them to understand 'why to use causal methods' instead of just fitting a giant neural network.
Comment: Great, thanks a lot! Is there any material that I can use to read something more about g-computation as well?


why compiler says a public static field in interface is "final" although it is not

Question: Please see the below code --
public interface TestInterface {
    public static String NON_CONST_B = "" ;
}

public class Implemented implements TestInterface {
    public static String NON_CONST_C = "" ;
}

public class AutoFinal  {

    public static String NON_CONST_A = "" ;

    public static void main(String args[]) {
        TestInterface.NON_CONST_B = "hello-b" ;
        Implemented.NON_CONST_C = "hello-c";
        AutoFinal.NON_CONST_A = "hello-a" ;
        Implemented obj = new Implemented();
    }
}

However, the compiler complains that TestInterface.NON_CONST_B is final --
AutoFinal.java:6: error: cannot assign a value to final variable NON_CONST_B
        TestInterface.NON_CONST_B = "hello-b" ;
                 ^
1 error

why ?
Comment: All fields in an interface are `public static final` by default just as all methods are `public abstract` by default.
Comment: @PeterLawrey, As of Java 8, methods can have `default` implementation (non-`abstract`). See: https://www.journaldev.com/2389/java-8-features-with-examples#interface-default-static-method. (Also `static` methods are allowed).
Comment: @Mir-Ismaili In Java 1 to 18.3 the default modifiers for methods are `public abstract` but if you add `default` as a keyword to the method from Java 8 you can give it a `public` *default* implementation ;) Java 9 allows `private` methods to be added to interfaces.
Answer: Regarding:
public interface TestInterface {
   public static String NON_CONST_B = "" ;
}

public class AutoFinal  {
   public static void main(String args[]) {
      TestInterface.NON_CONST_B = "hello-b" ;
      // ....
   }
}

However, the compiler complains that TestInterface.NON_CONST_B is final --

But it in fact is final whether you explicitly declare it to be or not since it is declared in an interface. You can't have non-final variables (non-constants) in an interface. It's also public and static whether or not it has been explicitly declared as such.
Per the JLS 9.3 Interface Field (Constant) Declarations:

Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.
Answer: In Java, all variables declared in an interface are public static final by default.
Answer: Variables declared in interface are always public static final by default in java. Interface variables are static because Java interfaces cannot be instantiated in their own right; the value of the variable must be assigned in a static context in which no instance exists. The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned by program code.
Answer: As all answers saying that by default all the variables declared in Interface are static final variables.
FYI, you can not declare a static method in interface. You can find the reason in this SO question.
however, you can declare Inner Class in an interface that can contain static methods and non static and non final variables.